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

    public function __construct(public Order $order) {}

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

    public function content(): Content
    {
        return new Content(
            htmlString: $this->buildHtml(),
            text: 'emails.order-confirmed-text',
        );
    }

    private function buildHtml(): string
    {
        $order    = $this->order;
        $customer = $order->user?->name ?? 'Valued Customer';
        $items    = $order->items ?? collect();

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

        $addr = $order->shippingAddress;
        $addrHtml = $addr
            ? "{$addr->first_name} {$addr->last_name}<br>{$addr->address_line_1}" .
              ($addr->address_line_2 ? "<br>{$addr->address_line_2}" : '') .
              "<br>{$addr->city}, {$addr->postcode}<br>{$addr->country}"
            : '—';

        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;'>Order Confirmed ✓</h2>
    <p style='margin:0 0 24px;color:#666;font-family:Arial,sans-serif;font-size:14px;'>Hi {$customer}, thank you for your order.</p>

    <div style='background:#f7f5f0;border-radius:8px;padding:16px 20px;margin-bottom:24px;'>
      <p style='margin:0;font-family:Arial,sans-serif;font-size:13px;color:#888;'>Order number</p>
      <p style='margin:4px 0 0;font-family:Arial,sans-serif;font-size:18px;font-weight:bold;color:#065c5e;'>{$order->order_number}</p>
    </div>

    <!-- Items -->
    <table width='100%' cellpadding='0' cellspacing='0' style='margin-bottom:24px;'>
      <tr>
        <th style='text-align:left;font-family:Arial,sans-serif;font-size:11px;text-transform:uppercase;letter-spacing:1px;color:#888;padding-bottom:8px;border-bottom:2px solid #f0ede8;'>Product</th>
        <th style='text-align:center;font-family:Arial,sans-serif;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-family:Arial,sans-serif;font-size:11px;text-transform:uppercase;letter-spacing:1px;color:#888;padding-bottom:8px;border-bottom:2px solid #f0ede8;'>Total</th>
      </tr>
      {$itemRows}
    </table>

    <!-- Totals -->
    <table width='100%' cellpadding='0' cellspacing='0' style='margin-bottom:32px;'>
      <tr>
        <td style='font-family:Arial,sans-serif;font-size:13px;color:#888;padding:4px 0;'>Subtotal</td>
        <td style='font-family:Arial,sans-serif;font-size:13px;color:#1a1a1a;text-align:right;padding:4px 0;'>£" . number_format($order->subtotal, 2) . "</td>
      </tr>" .
      ($order->discount_amount > 0 ? "<tr>
        <td style='font-family:Arial,sans-serif;font-size:13px;color:#4caf50;padding:4px 0;'>Discount</td>
        <td style='font-family:Arial,sans-serif;font-size:13px;color:#4caf50;text-align:right;padding:4px 0;'>-£" . number_format($order->discount_amount, 2) . "</td>
      </tr>" : '') . "
      <tr>
        <td style='font-family:Arial,sans-serif;font-size:13px;color:#888;padding:4px 0;'>Shipping</td>
        <td style='font-family:Arial,sans-serif;font-size:13px;color:#1a1a1a;text-align:right;padding:4px 0;'>£" . number_format($order->shipping_cost, 2) . "</td>
      </tr>
      <tr>
        <td style='font-family:Arial,sans-serif;font-size:15px;font-weight:bold;color:#1a1a1a;padding:12px 0 4px;border-top:2px solid #f0ede8;'>Total</td>
        <td style='font-family:Arial,sans-serif;font-size:15px;font-weight:bold;color:#065c5e;text-align:right;padding:12px 0 4px;border-top:2px solid #f0ede8;'>£" . number_format($order->total, 2) . "</td>
      </tr>
    </table>

    <!-- Address -->
    <div style='border-top:1px solid #f0ede8;padding-top:24px;'>
      <p style='margin:0 0 8px;font-family:Arial,sans-serif;font-size:11px;text-transform:uppercase;letter-spacing:1px;color:#888;'>Shipping to</p>
      <p style='margin:0;font-family:Arial,sans-serif;font-size:14px;color:#1a1a1a;line-height:1.6;'>{$addrHtml}</p>
    </div>

    <p style='margin:32px 0 0;font-family:Arial,sans-serif;font-size:13px;color:#888;line-height:1.6;'>
      Your order will be dispatched within 1–2 working days. You will receive a separate email with your tracking number once shipped.
    </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>";
    }
}
