<?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 OrderShipped extends Mailable
{
    use Queueable, SerializesModels;

    public function __construct(public Order $order) {}

    public function envelope(): Envelope
    {
        return new Envelope(
            subject: "Your Order Has Been Dispatched — {$this->order->order_number}",
        );
    }

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

    private function buildHtml(): string
    {
        $order    = $this->order;
        $customer = $order->user?->name ?? 'Valued Customer';
        $tracking = $order->tracking_number ?? '';
        $trackUrl = "https://www.royalmail.com/track-your-item#/tracking-results/{$tracking}";

        return "<!DOCTYPE html>
<html>
<head><meta charset='utf-8'><meta name='viewport' content='width=device-width,initial-scale=1'></head>
<body style='margin:0;padding:0;background:#f7f5f0;font-family:Georgia,serif;'>
<table width='100%' cellpadding='0' cellspacing='0' style='background:#f7f5f0;padding:40px 20px;'>
<tr><td align='center'>
<table width='600' cellpadding='0' cellspacing='0' style='max-width:600px;width:100%;'>

  <!-- Header -->
  <tr><td style='background:#065c5e;padding:32px 40px;border-radius:12px 12px 0 0;text-align:center;'>
    <h1 style='margin:0;color:#ffffff;font-family:Georgia,serif;font-size:24px;font-weight:normal;letter-spacing:1px;'>BioSupply UK</h1>
    <p style='margin:8px 0 0;color:rgba(255,255,255,0.7);font-family:Arial,sans-serif;font-size:13px;'>Research Peptides &amp; Compounds</p>
  </td></tr>

  <!-- Body -->
  <tr><td style='background:#ffffff;padding:40px;'>
    <h2 style='margin:0 0 8px;color:#065c5e;font-size:20px;font-weight:normal;'>Your order is on its way 📦</h2>
    <p style='margin:0 0 24px;color:#666;font-family:Arial,sans-serif;font-size:14px;'>Hi {$customer}, your order <strong>{$order->order_number}</strong> has been dispatched.</p>

    " . ($tracking ? "
    <!-- Tracking -->
    <div style='background:#f0f9f7;border:2px solid #065c5e;border-radius:12px;padding:24px;margin-bottom:32px;text-align:center;'>
      <p style='margin:0 0 4px;font-family:Arial,sans-serif;font-size:12px;text-transform:uppercase;letter-spacing:1px;color:#888;'>Tracking number</p>
      <p style='margin:0 0 16px;font-family:\"Courier New\",monospace;font-size:22px;font-weight:bold;color:#065c5e;letter-spacing:2px;'>{$tracking}</p>
      <a href='{$trackUrl}' style='display:inline-block;background:#065c5e;color:#ffffff;font-family:Arial,sans-serif;font-size:14px;font-weight:bold;text-decoration:none;padding:12px 28px;border-radius:8px;'>
        Track on Royal Mail &rarr;
      </a>
    </div>" : "
    <div style='background:#f7f5f0;border-radius:8px;padding:16px 20px;margin-bottom:32px;'>
      <p style='margin:0;font-family:Arial,sans-serif;font-size:14px;color:#888;'>Tracking information will be available shortly.</p>
    </div>") . "

    <p style='margin:0 0 8px;font-family:Arial,sans-serif;font-size:14px;color:#1a1a1a;'>Estimated delivery:</p>
    <p style='margin:0;font-family:Arial,sans-serif;font-size:14px;color:#888;'>1–3 working days from dispatch.</p>
  </td></tr>

  <!-- Research disclaimer -->
  <tr><td style='background:#fff8e1;padding:16px 40px;border-left:3px solid #f9a825;'>
    <p style='margin:0;font-family:Arial,sans-serif;font-size:12px;color:#795548;line-height:1.5;'>
      <strong>Research Use Only:</strong> All products are sold strictly for laboratory research purposes and are not intended for human consumption.
    </p>
  </td></tr>

  <!-- Footer -->
  <tr><td style='padding:24px 40px;text-align:center;border-radius:0 0 12px 12px;'>
    <p style='margin:0;font-family:Arial,sans-serif;font-size:12px;color:#aaa;'>
      &copy; " . date('Y') . " BioSupply UK &bull; <a href='https://biosupplyuk.com' style='color:#065c5e;'>biosupplyuk.com</a>
    </p>
  </td></tr>

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