import { renderTemplate } from "./templates-db";
import { EMAIL_TEMPLATE_DEFAULTS } from "./template-defaults";

// Back-compat helpers for callers that haven't migrated to sendTemplatedEmail.
// These render the built-in defaults synchronously so the callsite can still
// pass the result into sendEmail().

function defaultBySlug(slug: string) {
  const t = EMAIL_TEMPLATE_DEFAULTS.find((d) => d.slug === slug);
  if (!t) throw new Error(`Unknown template slug: ${slug}`);
  return { subject: t.subject, body: t.body };
}

export type AdminNewApplicationProps = {
  applicantName: string;
  applicantEmail: string;
  institution: string;
  country: string;
  motivation: string;
  reviewUrl: string;
};

export function adminNewApplicationEmail(p: AdminNewApplicationProps) {
  const { subject, body } = renderTemplate(defaultBySlug("admin-new-application"), p);
  return { subject, text: body, templateSlug: "admin-new-application" };
}

export function applicantApprovedEmail(name: string, loginUrl: string) {
  const { subject, body } = renderTemplate(defaultBySlug("applicant-approved"), {
    name,
    loginUrl,
  });
  return { subject, text: body, templateSlug: "applicant-approved" };
}

export function applicantRejectedEmail(name: string, notes: string | null) {
  const notesBlock = notes ? `Reviewer notes:\n${notes}\n\n` : "";
  const { subject, body } = renderTemplate(defaultBySlug("applicant-rejected"), {
    name,
    notes: notes ?? "",
    notesBlock,
  });
  return { subject, text: body, templateSlug: "applicant-rejected" };
}
