import { projectsMailbox, sendTrackedMail, siteUrl, type MailProject } from "@/lib/mail";
import {
  batDecisionEmail,
  internalRequestNotificationEmail,
  pyramidComEmailLayout,
  requestReceiptEmail,
} from "@/lib/email-templates";

type NewRequestMail = MailProject & {
  reference: string;
  name: string;
  email: string;
  company?: string;
  projectLabel: string;
  details: string[];
};

export async function sendNewRequestEmails(input: NewRequestMail) {
  const details = input.details.map((detail, index) => {
    const separator = detail.indexOf(":");
    return separator > 0
      ? { label: detail.slice(0, separator).trim(), value: detail.slice(separator + 1).trim() }
      : { label: `Information ${index + 1}`, value: detail };
  });
  const adminMail = internalRequestNotificationEmail({
    siteUrl: siteUrl(),
    reference: input.reference,
    clientName: input.name,
    clientEmail: input.email,
    company: input.company,
    projectLabel: input.projectLabel,
    details,
  });
  const clientMail = requestReceiptEmail({
    siteUrl: siteUrl(),
    name: input.name,
    reference: input.reference,
    projectLabel: input.projectLabel,
    details,
  });
  return Promise.all([
    sendTrackedMail({
      ...input,
      template: "new-request-admin",
      to: projectsMailbox(),
      subject: `Nouvelle demande ${input.reference} — ${input.projectLabel}`,
      text: adminMail.text,
      html: adminMail.html,
    }),
    sendTrackedMail({
      ...input,
      template: "new-request-client",
      to: input.email,
      subject: `Votre demande ${input.reference} a bien été reçue`,
      text: clientMail.text,
      html: clientMail.html,
    }),
  ]);
}

export async function sendClientDecisionEmails(input: MailProject & {
  reference: string;
  name: string;
  email: string;
  projectLabel: string;
  status: "accepted" | "changes_requested";
  note?: string;
}) {
  const accepted = input.status === "accepted";
  const clientMail = batDecisionEmail({
    siteUrl: siteUrl(),
    name: input.name,
    reference: input.reference,
    projectLabel: input.projectLabel,
    status: input.status,
    note: input.note,
  });
  const adminMail = pyramidComEmailLayout({
    siteUrl: siteUrl(),
    title: accepted ? `BAT signé · ${input.reference}` : `Correction demandée · ${input.reference}`,
    intro: accepted
      ? "Le client vient de signer et valider son BAT."
      : "Le client vient de demander une modification de son BAT.",
    details: [
      { label: "Client", value: `${input.name} · ${input.email}` },
      { label: "Projet", value: input.projectLabel },
      { label: "Référence", value: input.reference },
      ...(input.note ? [{ label: "Message", value: input.note }] : []),
    ],
    action: { label: "Ouvrir l’administration", href: `${siteUrl()}/admin` },
  });
  return Promise.all([
    sendTrackedMail({
      ...input,
      template: accepted ? "bat-accepted-admin" : "bat-changes-admin",
      to: projectsMailbox(),
      subject: `${accepted ? "BAT signé" : "Correction demandée"} · ${input.reference}`,
      text: adminMail.text,
      html: adminMail.html,
    }),
    sendTrackedMail({
      ...input,
      template: accepted ? "bat-accepted-client" : "bat-changes-client",
      to: input.email,
      subject: `${accepted ? "Validation enregistrée" : "Demande de correction reçue"} · ${input.reference}`,
      text: clientMail.text,
      html: clientMail.html,
    }),
  ]);
}
