import { institution } from "../config/institution.ts";
import { branding } from "../config/branding.ts";
import { emailConfig } from "../config/email.ts";
const esc = (value: string) =>
  value.replace(
    /[&<>"']/g,
    (char) =>
      ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" })[
        char
      ]!,
  );
const frame = (content: string) =>
  `<!doctype html><html lang="pt"><body style="margin:0;background:#f5f2ee;font-family:Helvetica,Arial,sans-serif;color:#23282a"><div style="max-width:620px;margin:auto;padding:32px 20px"><div style="background:#fff;border-radius:18px;padding:32px;border-top:5px solid ${branding.primaryColor}"><p style="margin:0 0 22px;color:${branding.primaryColor};font-weight:700;letter-spacing:.08em;font-size:12px">${esc(institution.name)}</p>${content}</div><p style="font-size:12px;color:#697174;line-height:1.5">${esc(emailConfig.footer)}</p></div></body></html>`;

export function familyConfirmationEmail(input: {
  name: string;
  publicNumber: string;
  response: string;
}) {
  return {
    subject: `Inscrição recebida - ${input.publicNumber}`,
    html: frame(
      `<h1 style="font-size:26px;margin:0 0 16px">Recebemos a inscrição.</h1><p style="line-height:1.65">A inscrição de <strong>${esc(input.name)}</strong> para <strong>${esc(input.response)}</strong> foi submetida com sucesso.</p><div style="background:#f7f4f0;border-radius:12px;padding:18px;margin:22px 0"><span style="font-size:12px;color:#697174">REFERÊNCIA</span><br><strong style="font-size:21px">${esc(input.publicNumber)}</strong></div><p style="line-height:1.65">A ficha submetida segue em anexo. Guarde-a juntamente com a referência. A equipa entrará em contacto caso seja necessária informação adicional.</p><p style="margin-top:26px">${esc(institution.name)}</p>`,
    ),
  };
}
export function internalNotificationEmail(input: {
  name: string;
  publicNumber: string;
  response: string;
  adminUrl: string;
}) {
  return {
    subject: `Nova inscrição - ${input.publicNumber}`,
    html: frame(
      `<h1 style="font-size:24px;margin:0 0 16px">Nova inscrição recebida</h1><p><strong>${esc(input.name)}</strong><br>${esc(input.response)}<br>Referência: ${esc(input.publicNumber)}</p><p style="margin-top:24px"><a href="${esc(input.adminUrl)}" style="display:inline-block;background:${branding.primaryColor};color:#fff;text-decoration:none;padding:13px 20px;border-radius:9px;font-weight:700">Consultar candidatura</a></p>`,
    ),
  };
}

export function documentRequestEmail(input: {
  name: string;
  publicNumber: string;
  message: string;
}) {
  return {
    subject: `Documentos necessários - ${input.publicNumber}`,
    html: frame(
      `<h1 style="font-size:24px;margin:0 0 16px">Precisamos de documentação adicional</h1><p style="line-height:1.65">Exmo.(a) responsável pela inscrição de <strong>${esc(input.name)}</strong>,</p><div style="background:#f7f4f0;border-radius:12px;padding:18px;margin:22px 0;white-space:pre-line">${esc(input.message)}</div><p>Referência da inscrição: <strong>${esc(input.publicNumber)}</strong>.</p><p>Para proteger os dados pessoais, entregue os documentos pelos meios indicados pela secretaria e não responda a este email com anexos.</p>`,
    ),
  };
}
