<?php

namespace App\Mail;

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

class BackInStock extends Mailable
{
    use Queueable, SerializesModels;

    public function __construct(
        public Product $product,
        public ?ProductVariant $variant = null,
    ) {}

    public function envelope(): Envelope
    {
        return new Envelope(
            subject: "Back in Stock: {$this->product->name}" . ($this->variant ? " — {$this->variant->name}" : ''),
        );
    }

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

    private function buildHtml(): string
    {
        $name = htmlspecialchars($this->product->name);
        $variant = $this->variant ? htmlspecialchars($this->variant->name) : null;
        $url = rtrim(env('FRONTEND_URL', 'http://localhost:3000'), '/') . '/products/' . $this->product->slug;
        $image = $this->product->images[0] ?? null;

        $variantRow = $variant ? "<p style='margin:0 0 8px;color:#6b7280;font-size:14px;'>Variant: <strong>{$variant}</strong></p>" : '';
        $imageTag = $image ? "<img src='{$image}' alt='{$name}' style='width:120px;height:120px;object-fit:cover;border-radius:8px;margin-bottom:16px;'>" : '';

        return <<<HTML
<!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:#f9fafb;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',sans-serif;">
  <table width="100%" cellpadding="0" cellspacing="0" style="background:#f9fafb;padding:40px 0;">
    <tr><td align="center">
      <table width="600" cellpadding="0" cellspacing="0" style="background:#ffffff;border-radius:12px;overflow:hidden;box-shadow:0 1px 3px rgba(0,0,0,0.1);max-width:600px;width:100%;">

        <!-- Header -->
        <tr>
          <td style="background:#0d9488;padding:32px 40px;text-align:center;">
            <h1 style="margin:0;color:#ffffff;font-size:24px;font-weight:700;letter-spacing:-0.5px;">BioSupply UK</h1>
            <p style="margin:8px 0 0;color:#99f6e4;font-size:14px;">Laboratory Research Supplies</p>
          </td>
        </tr>

        <!-- Body -->
        <tr>
          <td style="padding:40px;">
            <div style="text-align:center;margin-bottom:24px;">
              <div style="display:inline-block;background:#d1fae5;border-radius:50%;width:64px;height:64px;line-height:64px;font-size:32px;margin-bottom:16px;">🎉</div>
              <h2 style="margin:0 0 8px;color:#111827;font-size:22px;font-weight:700;">Good news — it's back!</h2>
              <p style="margin:0;color:#6b7280;font-size:15px;">A product you were waiting for is now available.</p>
            </div>

            <!-- Product card -->
            <div style="background:#f9fafb;border:1px solid #e5e7eb;border-radius:10px;padding:24px;text-align:center;margin-bottom:32px;">
              {$imageTag}
              <h3 style="margin:0 0 8px;color:#111827;font-size:18px;font-weight:600;">{$name}</h3>
              {$variantRow}
              <p style="margin:0;color:#0d9488;font-size:15px;font-weight:600;">Now In Stock</p>
            </div>

            <!-- CTA -->
            <div style="text-align:center;margin-bottom:32px;">
              <a href="{$url}" style="display:inline-block;background:#0d9488;color:#ffffff;text-decoration:none;padding:14px 32px;border-radius:8px;font-size:16px;font-weight:600;">
                Shop Now
              </a>
              <p style="margin:12px 0 0;color:#9ca3af;font-size:13px;">Stock is limited — don't miss out.</p>
            </div>

            <!-- Disclaimer -->
            <div style="background:#fef9c3;border-left:4px solid #f59e0b;padding:14px 16px;border-radius:4px;margin-bottom:24px;">
              <p style="margin:0;color:#92400e;font-size:13px;line-height:1.5;">
                All products are for laboratory research purposes only and are not intended for human consumption. You must be 18 or older to purchase.
              </p>
            </div>

          </td>
        </tr>

        <!-- Footer -->
        <tr>
          <td style="background:#f9fafb;padding:24px 40px;text-align:center;border-top:1px solid #e5e7eb;">
            <p style="margin:0;color:#9ca3af;font-size:12px;">
              You received this email because you signed up for back-in-stock notifications on BioSupply UK.<br>
              © 2025 BioSupply UK. All rights reserved.
            </p>
          </td>
        </tr>

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