<?php
namespace App\Core;

class Mailer
{
    public static function orderNotification(array $order, array $items): void
    {
        $to = config('app.email_admin');
        self::send($to, "Nova Encomenda {$order['order_number']}", self::adminTpl($order, $items));
        self::send($order['email'], "Encomenda {$order['order_number']} recebida", self::clientTpl($order));
    }

    private static function send(string $to, string $subject, string $html): void
    {
        $h  = "MIME-Version: 1.0\r\nContent-Type: text/html; charset=UTF-8\r\n";
        $h .= "From: " . config('app.name') . " <noreply@galeria.pt>\r\n";
        @\mail($to, $subject, $html, $h);
    }

    private static function adminTpl(array $o, array $items): string
    {
        $rows = '';
        foreach ($items as $i)
            $rows .= "<tr><td style='padding:6px 8px;border-bottom:1px solid #e5e7eb;'>{$i['filename']}</td>
                <td style='padding:6px 8px;border-bottom:1px solid #e5e7eb;text-align:center;'>{$i['quantity']}</td>
                <td style='padding:6px 8px;border-bottom:1px solid #e5e7eb;text-align:right;'>" . money($i['subtotal']) . "</td></tr>";

        return "<!DOCTYPE html><html><body style='font-family:system-ui,sans-serif;max-width:560px;margin:0 auto;padding:20px;'>
        <div style='background:#18181b;padding:20px 24px;border-radius:8px 8px 0 0;'>
            <h2 style='color:#fff;margin:0;font-size:18px;'>📸 Nova Encomenda — {$o['order_number']}</h2>
        </div>
        <div style='background:#f9fafb;padding:24px;border-radius:0 0 8px 8px;border:1px solid #e5e7eb;'>
            <table style='width:100%;border-collapse:collapse;margin-bottom:20px;font-size:14px;'>
                <tr><td style='color:#6b7280;padding:3px 0;width:30%;'>Cliente</td><td style='font-weight:600;'>{$o['customer_name']}</td></tr>
                <tr><td style='color:#6b7280;padding:3px 0;'>Telefone</td><td>{$o['phone']}</td></tr>
                <tr><td style='color:#6b7280;padding:3px 0;'>Email</td><td>{$o['email']}</td></tr>
                <tr><td style='color:#6b7280;padding:3px 0;'>Evento</td><td>{$o['event_name']}</td></tr>
                <tr><td style='color:#6b7280;padding:3px 0;'>Envio</td><td>" . self::ship($o['shipping_method']) . "</td></tr>
                " . ($o['address'] ? "<tr><td style='color:#6b7280;padding:3px 0;'>Morada</td><td>" . \nl2br(\htmlspecialchars($o['address'])) . "</td></tr>" : "") . "
            </table>
            <table style='width:100%;border-collapse:collapse;font-size:13px;'>
                <thead><tr style='background:#e5e7eb;'><th style='padding:7px 8px;text-align:left;'>Foto</th><th style='padding:7px 8px;text-align:center;'>Qtd</th><th style='padding:7px 8px;text-align:right;'>Total</th></tr></thead>
                <tbody>$rows</tbody>
            </table>
            <table style='width:100%;border-collapse:collapse;margin-top:8px;font-size:14px;'>
                <tr><td style='padding:4px 8px;color:#6b7280;'>Subtotal</td><td style='padding:4px 8px;text-align:right;'>" . money($o['subtotal']) . "</td></tr>
                " . ($o['shipping_cost']>0 ? "<tr><td style='padding:4px 8px;color:#6b7280;'>Envio</td><td style='padding:4px 8px;text-align:right;'>" . money($o['shipping_cost']) . "</td></tr>" : "") . "
                " . ($o['photos_free']>0 ? "<tr><td style='padding:4px 8px;color:#16a34a;'>Oferta</td><td style='padding:4px 8px;text-align:right;color:#16a34a;'>+" . $o['photos_free'] . " fotos</td></tr>" : "") . "
                <tr style='background:#18181b;'><td style='padding:10px 8px;color:#fff;font-weight:700;'>TOTAL A COBRAR</td><td style='padding:10px 8px;color:#fff;font-weight:700;text-align:right;font-size:16px;'>" . money($o['total']) . "</td></tr>
            </table>
            <p style='background:#eff6ff;padding:10px 12px;border-radius:6px;margin-top:16px;font-size:13px;color:#1e40af;'>💵 Pagamento presencial na entrega</p>
        </div></body></html>";
    }

    private static function clientTpl(array $o): string
    {
        return "<!DOCTYPE html><html><body style='font-family:system-ui,sans-serif;max-width:560px;margin:0 auto;padding:20px;'>
        <div style='background:#18181b;padding:20px 24px;border-radius:8px 8px 0 0;'>
            <h2 style='color:#fff;margin:0;font-size:18px;'>✅ Encomenda recebida!</h2>
        </div>
        <div style='background:#f9fafb;padding:24px;border-radius:0 0 8px 8px;border:1px solid #e5e7eb;'>
            <p>Olá <strong>{$o['customer_name']}</strong>,</p>
            <p>A sua encomenda <strong>{$o['order_number']}</strong> foi recebida com sucesso.</p>
            <p style='background:#eff6ff;padding:12px;border-radius:6px;font-size:14px;color:#1e40af;margin:16px 0;'>
                💵 O pagamento de <strong>" . money($o['total']) . "</strong> será efetuado presencialmente na entrega das fotografias.
            </p>
            <p style='background:#fefce8;padding:10px 12px;border-radius:6px;font-size:13px;color:#854d0e;'>⏳ Estado: <strong>Aguarda aprovação</strong></p>
        </div></body></html>";
    }

    private static function ship(string $m): string
    {
        return match($m) { 'ctt'=>'Envio CTT','internacional'=>'Envio Internacional',default=>'Entrega aos Noivos' };
    }
}
