<?php

namespace App\Mail;

use App\Models\Order;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

class NewOrderAdmin extends Mailable
{
    use Queueable, SerializesModels;

    public function __construct(public Order $order) {}

    public function envelope(): Envelope
    {
        return new Envelope(
            subject: "New Order — {$this->order->order_number} (£" . number_format($this->order->total, 2) . ")",
        );
    }

    public function content(): Content
    {
        return new Content(
            htmlString: $this->buildHtml(),
        );
    }

    private function buildHtml(): string
    {
        $order    = $this->order;
        $customer = $order->user?->name ?? $order->guest_email ?? 'Guest';
        $email    = $order->user?->email ?? $order->guest_email ?? '—';
        $items    = $order->items ?? collect();
        $addr     = $order->shippingAddress;

        $itemRows = '';
        foreach ($items as $item) {
            $itemRows .= "<tr>
                <td style='padding:8px 0;border-bottom:1px solid #f0ede8;font-size:13px;color:#1a1a1a;'>{$item->product_name}" .
                ($item->variant_name ? " ({$item->variant_name})" : '') . "</td>
                <td style='padding:8px 0;border-bottom:1px solid #f0ede8;font-size:13px;color:#888;text-align:center;'>{$item->quantity}</td>
                <td style='padding:8px 0;border-bottom:1px solid #f0ede8;font-size:13px;text-align:right;'>£" . number_format($item->total_price, 2) . "</td>
            </tr>";
        }

        $adminUrl = env('FRONTEND_URL', 'http://localhost:3000') . '/admin/orders/' . $order->id;

        return "<!DOCTYPE html>
<html>
<head><meta charset='utf-8'></head>
<body style='margin:0;padding:0;background:#f4f6f8;font-family:Arial,sans-serif;'>
<table width='100%' cellpadding='0' cellspacing='0' style='padding:32px 20px;'>
<tr><td align='center'>
<table width='560' cellpadding='0' cellspacing='0' style='max-width:560px;width:100%;'>

  <tr><td style='background:#1a2e2e;padding:20px 32px;border-radius:8px 8px 0 0;'>
    <p style='margin:0;color:#8ac;font-size:11px;text-transform:uppercase;letter-spacing:2px;'>BioSupply UK Admin</p>
    <h1 style='margin:4px 0 0;color:#ffffff;font-size:18px;font-weight:600;'>New Order Received</h1>
  </td></tr>

  <tr><td style='background:#ffffff;padding:32px;'>

    <table width='100%' cellpadding='0' cellspacing='0' style='margin-bottom:24px;background:#f4f6f8;border-radius:8px;'>
      <tr>
        <td style='padding:16px;'>
          <p style='margin:0 0 2px;font-size:11px;color:#888;text-transform:uppercase;letter-spacing:1px;'>Order</p>
          <p style='margin:0;font-size:20px;font-weight:bold;color:#065c5e;'>{$order->order_number}</p>
        </td>
        <td style='padding:16px;text-align:right;'>
          <p style='margin:0 0 2px;font-size:11px;color:#888;text-transform:uppercase;letter-spacing:1px;'>Total</p>
          <p style='margin:0;font-size:20px;font-weight:bold;color:#1a1a1a;'>£" . number_format($order->total, 2) . "</p>
        </td>
      </tr>
    </table>

    <table width='100%' cellpadding='0' cellspacing='0' style='margin-bottom:24px;'>
      <tr>
        <td style='width:50%;vertical-align:top;padding-right:16px;'>
          <p style='margin:0 0 6px;font-size:11px;color:#888;text-transform:uppercase;letter-spacing:1px;'>Customer</p>
          <p style='margin:0;font-size:14px;color:#1a1a1a;'>{$customer}</p>
          <p style='margin:2px 0 0;font-size:13px;color:#888;'>{$email}</p>
        </td>
        <td style='width:50%;vertical-align:top;'>
          <p style='margin:0 0 6px;font-size:11px;color:#888;text-transform:uppercase;letter-spacing:1px;'>Ship to</p>
          <p style='margin:0;font-size:13px;color:#1a1a1a;line-height:1.5;'>" .
          ($addr ? "{$addr->first_name} {$addr->last_name}<br>{$addr->city}, {$addr->postcode}" : '—') .
          "</p>
        </td>
      </tr>
    </table>

    <table width='100%' cellpadding='0' cellspacing='0' style='margin-bottom:24px;'>
      <tr>
        <th style='text-align:left;font-size:11px;text-transform:uppercase;letter-spacing:1px;color:#888;padding-bottom:8px;border-bottom:2px solid #f0ede8;'>Item</th>
        <th style='text-align:center;font-size:11px;text-transform:uppercase;letter-spacing:1px;color:#888;padding-bottom:8px;border-bottom:2px solid #f0ede8;'>Qty</th>
        <th style='text-align:right;font-size:11px;text-transform:uppercase;letter-spacing:1px;color:#888;padding-bottom:8px;border-bottom:2px solid #f0ede8;'>Total</th>
      </tr>
      {$itemRows}
    </table>

    <div style='text-align:center;padding-top:8px;'>
      <a href='{$adminUrl}' style='display:inline-block;background:#065c5e;color:#ffffff;font-size:14px;font-weight:bold;text-decoration:none;padding:12px 28px;border-radius:8px;'>
        View Order in Admin &rarr;
      </a>
    </div>

  </td></tr>

  <tr><td style='padding:16px 32px;text-align:center;'>
    <p style='margin:0;font-size:11px;color:#aaa;'>BioSupply UK Admin Notification</p>
  </td></tr>

</table>
</td></tr>
</table>
</body>
</html>";
    }
}
