<?php

namespace App\Services;

use App\Models\Emplette;
use App\Models\Order;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;

class AdminAlertMailService
{
    public function sendNewOrder(Order $order): void
    {
        $this->send(
            'Nouvelle commande client',
            [
                'Une nouvelle commande a ete creee.',
                'Commande: ' . $order->number . ' (ID ' . $order->id . ')',
                'Client ID: ' . $order->user_id,
                'Montant total: ' . number_format((float) $order->total, 2, '.', ' ') . ' XOF',
                'Statut paiement: ' . (string) $order->payment_status,
            ]
        );
    }

    public function sendNewEmpletteRequest(Emplette $emplette): void
    {
        $this->send(
            'Nouvelle demande d\'emplette',
            [
                'Une nouvelle demande d\'emplette a ete soumise.',
                'Emplette ID: ' . $emplette->id,
                'Client ID: ' . $emplette->user_id,
                'Statut: ' . (string) $emplette->status,
                'Total estime: ' . number_format((float) $emplette->estimated_total, 2, '.', ' ') . ' ' . (string) $emplette->currency,
            ]
        );
    }

    public function sendEmpletteAccepted(Emplette $emplette): void
    {
        $this->send(
            'Emplette acceptee par le client',
            [
                'Le client a accepte le devis d\'une emplette.',
                'Emplette ID: ' . $emplette->id,
                'Client ID: ' . $emplette->user_id,
                'Statut: ' . (string) $emplette->status,
                'Total final: ' . number_format((float) $emplette->final_total, 2, '.', ' ') . ' ' . (string) $emplette->currency,
            ]
        );
    }

    public function sendEmplettePayment(Emplette $emplette, Order $order): void
    {
        $this->send(
            'Paiement emplette confirme',
            [
                'Le paiement d\'une emplette a ete confirme.',
                'Emplette ID: ' . $emplette->id,
                'Commande: ' . $order->number . ' (ID ' . $order->id . ')',
                'Client ID: ' . $emplette->user_id,
                'Montant paye: ' . number_format((float) $order->total, 2, '.', ' ') . ' XOF',
                'Reference paiement: ' . ((string) $order->payment_reference !== '' ? (string) $order->payment_reference : '-'),
            ]
        );
    }

    /** @param array<int, string> $lines */
    private function send(string $subject, array $lines): void
    {
        $to = trim((string) config('mail.admin_alert_address'));
        if ($to === '') {
            return;
        }

        $body = implode("\n", $lines);

        try {
            Mail::raw($body, static function ($message) use ($to, $subject): void {
                $message->to($to)->subject($subject);
            });
        } catch (\Throwable $e) {
            Log::warning('Admin alert mail failed', [
                'to' => $to,
                'subject' => $subject,
                'error' => $e->getMessage(),
            ]);
        }
    }
}

