import { escapeHtml } from "@/lib/sanitize";

const SITE_URL = "https://www.ropts.com";
const PRIMARY = "#0D1F8C";
const ACCENT = "#C89B3C";
const INK = "#1E293B";
const SLATE = "#6B7280";
const BORDER = "#E8EDF5";

/** Shared HTML shell — inline styles only, since email clients strip <style> blocks and external CSS. */
function baseLayout(title: string, bodyHtml: string): string {
  return `<!doctype html>
<html>
  <body style="margin:0;padding:0;background:#F7F9FC;font-family:Georgia,'Times New Roman',serif;">
    <table role="presentation" width="100%" cellpadding="0" cellspacing="0" style="background:#F7F9FC;padding:32px 16px;">
      <tr>
        <td align="center">
          <table role="presentation" width="560" cellpadding="0" cellspacing="0" style="background:#FFFFFF;border:1px solid ${BORDER};border-radius:12px;overflow:hidden;">
            <tr>
              <td style="padding:28px 32px;border-bottom:1px solid ${BORDER};">
                <img src="${SITE_URL}/brand/logo.png" alt="ROPTS Venture Capital" height="28" style="display:block;height:28px;width:auto;" />
              </td>
            </tr>
            <tr>
              <td style="padding:32px;font-family:Helvetica,Arial,sans-serif;color:${INK};">
                <h1 style="margin:0 0 16px;font-family:Georgia,'Times New Roman',serif;font-size:22px;font-weight:600;color:${PRIMARY};">${escapeHtml(title)}</h1>
                ${bodyHtml}
              </td>
            </tr>
            <tr>
              <td style="padding:20px 32px;border-top:1px solid ${BORDER};font-family:Helvetica,Arial,sans-serif;font-size:12px;color:${SLATE};">
                ROPTS Venture Capital is a fundraising advisory and connectivity firm. ROPTS does not directly invest
                in every organization it works with, and this message does not constitute an offer, solicitation, or
                guarantee of investment.
              </td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
  </body>
</html>`;
}

function paragraph(text: string): string {
  return `<p style="margin:0 0 16px;font-size:15px;line-height:1.6;color:${INK};">${text}</p>`;
}

function accentDivider(): string {
  return `<div style="height:2px;width:48px;background:${ACCENT};margin:0 0 20px;"></div>`;
}

// ---- Applicant / subscriber confirmations ----

export function founderApplicationConfirmationEmail(organizationName: string): string {
  const name = escapeHtml(organizationName);
  return baseLayout(
    "Application received.",
    `${accentDivider()}
    ${paragraph(`Thanks for applying to raise with ROPTS on behalf of <strong>${name}</strong>.`)}
    ${paragraph(
      "Our advisory team reviews every application within three business days. You'll hear from us either way — with specific feedback if we decline. This message confirms receipt only; it is not a decision on your application and does not guarantee an introduction or investment."
    )}`
  );
}

export function investorApplicationConfirmationEmail(organizationName: string): string {
  const name = escapeHtml(organizationName);
  return baseLayout(
    "Registration received.",
    `${accentDivider()}
    ${paragraph(`Thanks for registering <strong>${name}</strong>'s funding interest with ROPTS.`)}
    ${paragraph(
      "Our advisory team reviews every registration within three business days and matches it against inbound organization mandates as they arise. This message confirms receipt only — registering does not commit you to funding any specific organization we introduce, and does not guarantee a match."
    )}`
  );
}

export function contactConfirmationEmail(name: string): string {
  const safeName = escapeHtml(name);
  return baseLayout(
    "Message received.",
    `${accentDivider()}
    ${paragraph(`Hi ${safeName}, thanks for reaching out.`)}
    ${paragraph("We'll respond via this email address within three business days.")}`
  );
}

export function newsletterConfirmationEmail(): string {
  return baseLayout(
    "You're subscribed.",
    `${accentDivider()}
    ${paragraph("You'll receive dated funding-landscape reports as they publish.")}`
  );
}

// ---- Internal business notifications ----

export interface NotificationField {
  label: string;
  value: string;
}

export function internalNotificationEmail(formLabel: string, fields: NotificationField[]): string {
  const rows = fields
    .map(
      (f) => `<tr>
        <td style="padding:8px 0;font-size:13px;color:${SLATE};width:180px;vertical-align:top;">${escapeHtml(f.label)}</td>
        <td style="padding:8px 0;font-size:14px;color:${INK};white-space:pre-wrap;">${escapeHtml(f.value)}</td>
      </tr>`
    )
    .join("");

  return baseLayout(
    `New ${escapeHtml(formLabel)} submission`,
    `${accentDivider()}
    <table role="presentation" width="100%" cellpadding="0" cellspacing="0" style="border-top:1px solid ${BORDER};">
      ${rows}
    </table>`
  );
}
