/**
 * Quotation Email Builder
 *
 * Shared HTML template for quotation emails sent during creation and resend.
 * Keeps the email layout in one place to avoid drift between routes.
 */

export interface QuotationEmailData {
  quotationNumber: string;
  customerName: string;
  currency: string;
  total: number;
  discountAmount?: number;
  taxAmount?: number;
  validUntil?: number | string | null;
  items: Array<{
    description: string;
    quantity: number;
    unitPrice: number;
  }>;
  /** Primary CTA link (payment URL or portal URL) */
  ctaUrl: string;
  /** Whether the CTA is a payment link (true) or a view-only portal link (false) */
  hasPaymentLink: boolean;
}

/**
 * Build the quotation email HTML.
 */
export function buildQuotationEmailHtml(data: QuotationEmailData): string {
  const {
    quotationNumber,
    customerName,
    currency,
    total,
    discountAmount = 0,
    taxAmount = 0,
    validUntil,
    items,
    ctaUrl,
    hasPaymentLink,
  } = data;

  const itemRows = items
    .map(
      (item) =>
        `<tr>
          <td style="padding:8px;border-bottom:1px solid #eee">${item.description}</td>
          <td style="padding:8px;border-bottom:1px solid #eee;text-align:center">${item.quantity}</td>
          <td style="padding:8px;border-bottom:1px solid #eee;text-align:right">${item.unitPrice.toFixed(2)}</td>
          <td style="padding:8px;border-bottom:1px solid #eee;text-align:right">${(item.quantity * item.unitPrice).toFixed(2)}</td>
        </tr>`,
    )
    .join("");

  const validUntilLine = validUntil
    ? `This quotation is valid until ${new Date(validUntil).toLocaleDateString("en-GB")}.`
    : "";

  return `
    <div style="font-family:Arial,sans-serif;max-width:600px;margin:0 auto;padding:20px">
      <h2 style="color:#1a1a1a">Quotation ${quotationNumber}</h2>
      <p>Dear ${customerName || "Customer"},</p>
      <p>Please find your quotation details below:</p>

      <table style="width:100%;border-collapse:collapse;margin:16px 0">
        <thead>
          <tr style="background:#f8f8f8">
            <th style="padding:8px;text-align:left;border-bottom:2px solid #ddd">Description</th>
            <th style="padding:8px;text-align:center;border-bottom:2px solid #ddd">Qty</th>
            <th style="padding:8px;text-align:right;border-bottom:2px solid #ddd">Unit Price</th>
            <th style="padding:8px;text-align:right;border-bottom:2px solid #ddd">Total</th>
          </tr>
        </thead>
        <tbody>${itemRows}</tbody>
      </table>

      ${discountAmount > 0 ? `<p style="color:#666">Discount: -${currency} ${discountAmount.toFixed(2)}</p>` : ""}
      ${taxAmount > 0 ? `<p style="color:#666">Tax: ${currency} ${taxAmount.toFixed(2)}</p>` : ""}

      <p style="font-size:18px;font-weight:bold;color:#1a1a1a">
        Total: ${currency} ${total.toFixed(2)}
      </p>

      <a href="${ctaUrl}" style="display:inline-block;background-color:#16a34a;color:white;padding:14px 28px;text-decoration:none;border-radius:8px;font-weight:bold;margin:16px 0;font-size:16px">
        ${hasPaymentLink ? "Pay Now" : "View Quotation"}
      </a>
      ${!hasPaymentLink ? '<p style="color:#999;font-size:12px">Payment link unavailable — Stripe keys not configured.</p>' : ""}

      <p style="color:#666;font-size:14px;margin-top:24px">
        ${validUntilLine}
        If you have any questions, please contact us.
      </p>
    </div>
  `;
}

/**
 * Build the email subject line for a quotation.
 */
export function buildQuotationEmailSubject(
  quotationNumber: string,
  title: string,
): string {
  return `Quotation ${quotationNumber} — ${title}`;
}
