import { sendSiteSmtpMail } from "@/lib/site-smtp-mail";

function escapeHtml(value: string) {
  return value
    .replaceAll("&", "&amp;")
    .replaceAll("<", "&lt;")
    .replaceAll(">", "&gt;")
    .replaceAll('"', "&quot;")
    .replaceAll("'", "&#039;");
}

export async function sendSponsorInquiryEmail(data: {
  name: string;
  email: string;
  company?: string | null;
  phone?: string | null;
  message?: string | null;
}) {
  const company = data.company?.trim() || "—";
  const phone = data.phone?.trim() || "—";
  const message = data.message?.trim() || "";

  const subject = `Novo interesse de patrocínio — ${data.name}`;
  const text = [
    "Novo pedido de patrocínio (Parceiros)",
    `Nome: ${data.name}`,
    `E-mail: ${data.email}`,
    `Empresa: ${company}`,
    `Telefone: ${phone}`,
    message ? "Mensagem:" : null,
    message || null,
  ]
    .filter(Boolean)
    .join("\n");

  const html = `
    <div style="font-family: Arial, Helvetica, sans-serif; color: #111;">
      <h2 style="margin: 0 0 12px 0;">Novo pedido de patrocínio</h2>
      <p style="margin: 0 0 8px 0;"><b>Nome:</b> ${escapeHtml(data.name)}</p>
      <p style="margin: 0 0 8px 0;"><b>E-mail:</b> ${escapeHtml(data.email)}</p>
      <p style="margin: 0 0 8px 0;"><b>Empresa:</b> ${escapeHtml(company)}</p>
      <p style="margin: 0 0 8px 0;"><b>Telefone:</b> ${escapeHtml(phone)}</p>
      ${
        message
          ? `<p style="margin: 12px 0 0 0;"><b>Mensagem:</b></p><p style="margin: 8px 0 0 0; white-space: pre-wrap;">${escapeHtml(
              message,
            )}</p>`
          : `<p style="margin: 12px 0 0 0; color: #555;">Mensagem não informada.</p>`
      }
      <hr style="border: none; border-top: 1px solid #eee; margin: 16px 0;" />
      <p style="margin: 0; color: #666; font-size: 12px;">Enviado pelo site fluminensedefeira.com.br</p>
    </div>
  `;

  await sendSiteSmtpMail({
    to: "torcedor@fluminensedefeira.com.br",
    subject,
    text,
    html,
    replyTo: data.email,
    fromName: "Fluminense de Feira — Parcerias",
  });
}

