/**
 * Birthday Announcement Email Templates
 *
 * Variants (templateId):
 * - classic       → Classic warm card with gold accents
 * - confetti      → Vibrant confetti party style
 * - spotlight     → Purple spotlight / modern gradient
 * - formal_id     → Indonesian formal letter style
 * - bilingual     → English + Indonesian bilingual
 * - minimal       → Clean minimal typographic design
 * - fireworks     → Dark festive fireworks theme
 * - floral        → Soft pastel floral garden theme
 * - retro         → Retro 80s neon style
 * - random        → Select randomly from all variants each call
 *
 * Notes:
 * - Includes SAPPS_META HTML comment at the very top so Email History resend can infer company_id.
 */

const ALL_TEMPLATE_IDS = [
	"classic",
	"confetti",
	"spotlight",
	"formal_id",
	"bilingual",
	"minimal",
	"fireworks",
	"floral",
	"retro",
];

function escapeHtml(str) {
	return String(str ?? "")
		.replace(/&/g, "&amp;")
		.replace(/</g, "&lt;")
		.replace(/>/g, "&gt;")
		.replace(/"/g, "&quot;")
		.replace(/'/g, "&#039;");
}

function buildListItems(names = []) {
	const safeNames = (names || []).map((n) => escapeHtml(n)).filter(Boolean);
	if (safeNames.length === 0) return "<li>Employee</li>";
	return safeNames.map((n) => `<li><b>${n}</b></li>`).join("\n");
}

function buildNamesBadges(names = [], badgeStyle = "") {
	const safeNames = (names || []).map((n) => escapeHtml(n)).filter(Boolean);
	if (safeNames.length === 0) return `<span style="${badgeStyle}">Employee</span>`;
	return safeNames.map((n) => `<span style="${badgeStyle}">${n}</span>`).join(" &nbsp; ");
}

function metaComment(meta) {
	try {
		return `<!-- SAPPS_META: ${JSON.stringify(meta)} -->`;
	} catch {
		return "<!-- SAPPS_META: {} -->";
	}
}

function resolveTemplateId(templateId) {
	const t = String(templateId || "classic").toLowerCase();
	if (t === "random") {
		const idx = Math.floor(Math.random() * ALL_TEMPLATE_IDS.length);
		return ALL_TEMPLATE_IDS[idx];
	}
	return t;
}

function subjectFor({ templateId, companyName, headline }) {
	const c = companyName ? ` | ${companyName}` : "";
	const t = resolveTemplateId(templateId);
	switch (t) {
		case "confetti":
			return `🎉 Happy Birthday, ${headline}!${c}`;
		case "spotlight":
			return `🌟 Employee Spotlight: Birthday Today${c}`;
		case "formal_id":
			return `📋 Pengumuman Ulang Tahun Karyawan${c}`;
		case "bilingual":
			return `🎂 Birthday / Selamat Ulang Tahun — ${headline}${c}`;
		case "minimal":
			return `Birthday Today — ${headline}${c}`;
		case "fireworks":
			return `🎆 Celebrate! It's ${headline}'s Birthday!${c}`;
		case "floral":
			return `🌸 Wishing a Beautiful Birthday to ${headline}${c}`;
		case "retro":
			return `🕹️ BIRTHDAY ALERT: ${headline}${c}`;
		default:
			return `🎂 Birthday Announcement — ${headline}${c}`;
	}
}

// ─── TEMPLATE: CLASSIC ────────────────────────────────────────────────────────
function htmlClassic({ companyName, names, systemNote }) {
	const listHtml = buildListItems(names);
	const safeCompany = escapeHtml(companyName || "");
	const safeNote = escapeHtml(systemNote || "This is an automated notification. Please do not reply.");

	return `
<div style="margin:0;padding:0;background:#f9f5ea;font-family:'Georgia',serif">
  <table width="100%" cellpadding="0" cellspacing="0" style="max-width:620px;margin:30px auto;border-radius:12px;overflow:hidden;box-shadow:0 4px 20px rgba(0,0,0,.12)">
    <!-- header -->
    <tr>
      <td style="background:linear-gradient(135deg,#f5a623 0%,#e07b00 100%);padding:36px 32px;text-align:center">
        <div style="font-size:56px;line-height:1">🎂</div>
        <h1 style="margin:12px 0 4px;color:#fff;font-size:26px;letter-spacing:.5px">Happy Birthday!</h1>
        ${safeCompany ? `<p style="margin:0;color:rgba(255,255,255,.85);font-size:13px">${safeCompany}</p>` : ""}
      </td>
    </tr>
    <!-- body -->
    <tr>
      <td style="background:#fffdf5;padding:32px 36px">
        <p style="margin:0 0 12px;color:#5a4000;font-size:15px">Today is a very special day! We are celebrating the birthday of:</p>
        <ul style="margin:0 0 20px 0;padding-left:20px;color:#8b5e00;font-size:16px">
          ${listHtml}
        </ul>
        <p style="margin:0 0 24px;color:#5a4000;font-size:15px">
          May this special day be filled with joy, laughter, and wonderful memories.
          Wishing you a fantastic year ahead! 🥳
        </p>
        <div style="text-align:center;margin-bottom:8px">
          <span style="font-size:32px">🎈 🎁 🌟</span>
        </div>
      </td>
    </tr>
    <!-- footer -->
    <tr>
      <td style="background:#f0e6c8;padding:14px 32px;text-align:center">
        <p style="margin:0;color:#9a7a30;font-size:11px">${safeNote}</p>
      </td>
    </tr>
  </table>
</div>`;
}

// ─── TEMPLATE: CONFETTI ───────────────────────────────────────────────────────
function htmlConfetti({ companyName, names, systemNote }) {
	const listHtml = buildListItems(names);
	const safeCompany = escapeHtml(companyName || "");
	const safeNote = escapeHtml(systemNote || "This is an automated notification. Please do not reply.");

	return `
<div style="margin:0;padding:0;background:#fff;font-family:'Arial',sans-serif">
  <table width="100%" cellpadding="0" cellspacing="0" style="max-width:620px;margin:30px auto;border-radius:16px;overflow:hidden;box-shadow:0 6px 30px rgba(0,0,0,.15)">
    <tr>
      <td style="background:linear-gradient(135deg,#ff6b6b,#feca57,#48dbfb,#ff9ff3);padding:40px 32px;text-align:center">
        <div style="font-size:60px;line-height:1;filter:drop-shadow(0 4px 6px rgba(0,0,0,.2))">🎉</div>
        <h1 style="margin:14px 0 6px;color:#fff;font-size:30px;text-shadow:0 2px 6px rgba(0,0,0,.25)">IT'S PARTY TIME!</h1>
        ${safeCompany ? `<p style="margin:0;background:rgba(255,255,255,.2);display:inline-block;padding:4px 14px;border-radius:20px;color:#fff;font-size:13px">${safeCompany}</p>` : ""}
      </td>
    </tr>
    <tr>
      <td style="background:#fffefe;padding:32px 36px">
        <p style="margin:0 0 8px;color:#444;font-size:15px;text-align:center">🎊 <b>Today we celebrate</b> 🎊</p>
        <ul style="margin:16px 0;padding-left:20px;color:#e4432d;font-size:16px;font-weight:bold">
          ${listHtml}
        </ul>
        <p style="margin:16px 0 0;color:#555;font-size:14px;text-align:center">
          Let's make today epic! Drop a GIF, send a message, and let the birthday person know they are awesome. 🚀
        </p>
        <div style="margin:20px auto;text-align:center;font-size:36px">🎆🎇✨</div>
      </td>
    </tr>
    <tr>
      <td style="background:linear-gradient(90deg,#ff6b6b,#feca57);padding:12px 32px;text-align:center">
        <p style="margin:0;color:#fff;font-size:11px;text-shadow:0 1px 3px rgba(0,0,0,.2)">${safeNote}</p>
      </td>
    </tr>
  </table>
</div>`;
}

// ─── TEMPLATE: SPOTLIGHT ──────────────────────────────────────────────────────
function htmlSpotlight({ companyName, names, systemNote }) {
	const listHtml = buildListItems(names);
	const safeCompany = escapeHtml(companyName || "");
	const safeNote = escapeHtml(systemNote || "This is an automated notification. Please do not reply.");
	const badgeStyle =
		"display:inline-block;background:rgba(255,255,255,.18);color:#fff;border-radius:20px;padding:4px 16px;font-size:15px;margin:4px";
	const badgesHtml = buildNamesBadges(names, badgeStyle);

	return `
<div style="margin:0;padding:0;background:#1a1a2e;font-family:'Helvetica Neue',sans-serif">
  <table width="100%" cellpadding="0" cellspacing="0" style="max-width:620px;margin:30px auto;border-radius:16px;overflow:hidden;box-shadow:0 8px 40px rgba(0,0,0,.5)">
    <tr>
      <td style="background:radial-gradient(ellipse at top,#7b2ff7 0%,#1a1a2e 70%);padding:48px 32px;text-align:center">
        <div style="font-size:56px;line-height:1">🌟</div>
        <h1 style="margin:14px 0 6px;color:#fff;font-size:24px;letter-spacing:2px;text-transform:uppercase">Employee Spotlight</h1>
        <p style="margin:4px 0 16px;color:#b39ddb;font-size:13px;letter-spacing:1px">BIRTHDAY TODAY</p>
        <div style="margin:10px 0">${badgesHtml}</div>
        ${safeCompany ? `<p style="margin:16px 0 0;color:#9575cd;font-size:12px;letter-spacing:1px">${safeCompany}</p>` : ""}
      </td>
    </tr>
    <tr>
      <td style="background:#16213e;padding:32px 36px">
        <ul style="margin:0 0 20px;padding-left:20px;color:#ce93d8;font-size:15px">
          ${listHtml}
        </ul>
        <p style="margin:0;color:#90caf9;font-size:14px;line-height:1.7">
          Today shines a little brighter because of you. Drop a birthday message in the team chat and let's cheer them on! 🎉
        </p>
      </td>
    </tr>
    <tr>
      <td style="background:#0f3460;padding:12px 32px;text-align:center">
        <p style="margin:0;color:#7986cb;font-size:11px">${safeNote}</p>
      </td>
    </tr>
  </table>
</div>`;
}

// ─── TEMPLATE: FORMAL_ID ──────────────────────────────────────────────────────
function htmlFormalId({ companyName, names, systemNote }) {
	const listHtml = buildListItems(names);
	const safeCompany = escapeHtml(companyName || "");
	const safeNote = escapeHtml(systemNote || "This is an automated notification. Please do not reply.");
	const today = new Date().toLocaleDateString("id-ID", { day: "numeric", month: "long", year: "numeric" });

	return `
<div style="margin:0;padding:0;background:#f7f7f7;font-family:'Times New Roman',serif">
  <table width="100%" cellpadding="0" cellspacing="0" style="max-width:620px;margin:30px auto;background:#fff;border-radius:8px;overflow:hidden;border:1px solid #ddd;box-shadow:0 2px 12px rgba(0,0,0,.08)">
    <tr>
      <td style="background:#1a237e;padding:20px 32px;text-align:center">
        <h2 style="margin:0;color:#fff;font-size:18px;letter-spacing:1px">PEMBERITAHUAN RESMI</h2>
        ${safeCompany ? `<p style="margin:4px 0 0;color:#9fa8da;font-size:13px">${safeCompany}</p>` : ""}
      </td>
    </tr>
    <tr>
      <td style="padding:32px 40px">
        <p style="margin:0 0 6px;color:#333;font-size:13px">📅 ${today}</p>
        <p style="margin:0 0 20px;color:#333;font-size:13px">Kepada Yth.,<br/>Seluruh Karyawan</p>
        <h3 style="border-bottom:2px solid #1a237e;padding-bottom:8px;color:#1a237e;font-size:16px">📋 Pengumuman Ulang Tahun Karyawan</h3>
        <p style="color:#333;font-size:14px;line-height:1.8">
          Dengan hormat, bersama ini kami sampaikan bahwa pada hari ini merayakan hari ulang tahun:
        </p>
        <ul style="margin:8px 0 20px;padding-left:24px;color:#1a237e;font-size:15px;font-weight:bold;line-height:2">
          ${listHtml}
        </ul>
        <p style="color:#333;font-size:14px;line-height:1.8;">
          Kami atas nama manajemen dan seluruh rekan kerja mengucapkan selamat ulang tahun dan mendoakan semoga
          panjang umur, sehat selalu, sukses dalam karier dan kehidupan pribadi.
        </p>
        <p style="margin-top:24px;color:#333;font-size:14px">Hormat kami,</p>
        <p style="margin:4px 0 0;color:#1a237e;font-size:14px;font-weight:bold">${safeCompany || "Management"}</p>
      </td>
    </tr>
    <tr>
      <td style="background:#e8eaf6;padding:12px 32px;text-align:center;border-top:1px solid #c5cae9">
        <p style="margin:0;color:#7986cb;font-size:11px">${safeNote}</p>
      </td>
    </tr>
  </table>
</div>`;
}

// ─── TEMPLATE: BILINGUAL ──────────────────────────────────────────────────────
function htmlBilingual({ companyName, names, systemNote }) {
	const listHtml = buildListItems(names);
	const safeCompany = escapeHtml(companyName || "");
	const safeNote = escapeHtml(systemNote || "This is an automated notification. Please do not reply.");

	return `
<div style="margin:0;padding:0;background:#f3f4f6;font-family:'Segoe UI',Arial,sans-serif">
  <table width="100%" cellpadding="0" cellspacing="0" style="max-width:620px;margin:30px auto;border-radius:14px;overflow:hidden;box-shadow:0 4px 24px rgba(0,0,0,.1)">
    <tr>
      <td style="background:linear-gradient(135deg,#11998e 0%,#38ef7d 100%);padding:36px 32px;text-align:center">
        <div style="font-size:52px">🎂 🎉</div>
        <h1 style="margin:12px 0 4px;color:#fff;font-size:22px">Birthday Announcement</h1>
        <p style="margin:0;color:rgba(255,255,255,.8);font-size:13px">Pengumuman Ulang Tahun</p>
        ${safeCompany ? `<p style="margin:10px 0 0;color:rgba(255,255,255,.7);font-size:12px">${safeCompany}</p>` : ""}
      </td>
    </tr>
    <tr>
      <td style="background:#fff;padding:0">
        <table width="100%" cellpadding="0" cellspacing="0">
          <tr>
            <td width="50%" style="padding:28px 24px;border-right:1px solid #e5e7eb;vertical-align:top">
              <p style="margin:0 0 8px;color:#6b7280;font-size:12px;font-weight:600;letter-spacing:1px;text-transform:uppercase">English</p>
              <p style="margin:0 0 12px;color:#374151;font-size:14px">Today we celebrate:</p>
              <ul style="padding-left:18px;color:#047857;font-size:14px;">${listHtml}</ul>
              <p style="margin:12px 0 0;color:#374151;font-size:13px">Wishing you a wonderful birthday filled with happiness and success!</p>
            </td>
            <td width="50%" style="padding:28px 24px;vertical-align:top">
              <p style="margin:0 0 8px;color:#6b7280;font-size:12px;font-weight:600;letter-spacing:1px;text-transform:uppercase">Indonesia</p>
              <p style="margin:0 0 12px;color:#374151;font-size:14px">Hari ini kita merayakan:</p>
              <ul style="padding-left:18px;color:#047857;font-size:14px;">${listHtml}</ul>
              <p style="margin:12px 0 0;color:#374151;font-size:13px">Semoga hari ulang tahun ini penuh kebahagiaan dan kesuksesan!</p>
            </td>
          </tr>
        </table>
      </td>
    </tr>
    <tr>
      <td style="background:#d1fae5;padding:12px 32px;text-align:center">
        <p style="margin:0;color:#065f46;font-size:11px">${safeNote}</p>
      </td>
    </tr>
  </table>
</div>`;
}

// ─── TEMPLATE: MINIMAL ────────────────────────────────────────────────────────
function htmlMinimal({ companyName, names, systemNote }) {
	const listHtml = buildListItems(names);
	const safeCompany = escapeHtml(companyName || "");
	const safeNote = escapeHtml(systemNote || "This is an automated notification. Please do not reply.");
	const today = new Date().toLocaleDateString("en-US", { weekday: "long", month: "long", day: "numeric" });

	return `
<div style="margin:0;padding:0;background:#fafafa;font-family:'Helvetica Neue',Helvetica,sans-serif">
  <table width="100%" cellpadding="0" cellspacing="0" style="max-width:560px;margin:40px auto;background:#fff;border-radius:8px;overflow:hidden;border:1px solid #eee">
    <tr>
      <td style="padding:40px 48px 32px">
        <p style="margin:0 0 4px;color:#9ca3af;font-size:12px;letter-spacing:2px;text-transform:uppercase">${today}</p>
        <h1 style="margin:8px 0;color:#111827;font-size:28px;font-weight:300;letter-spacing:-.5px">🎂 Birthday</h1>
        ${safeCompany ? `<p style="margin:0 0 28px;color:#6b7280;font-size:12px">${safeCompany}</p>` : `<div style="margin-bottom:28px"></div>`}
        <ul style="margin:0 0 24px;padding-left:20px;color:#374151;font-size:16px;line-height:2.2">
          ${listHtml}
        </ul>
        <p style="margin:0;color:#6b7280;font-size:14px;border-left:3px solid #111827;padding-left:12px;font-style:italic">
          Take a moment today to wish them well.
        </p>
      </td>
    </tr>
    <tr>
      <td style="padding:16px 48px;border-top:1px solid #f3f4f6">
        <p style="margin:0;color:#d1d5db;font-size:11px">${safeNote}</p>
      </td>
    </tr>
  </table>
</div>`;
}

// ─── TEMPLATE: FIREWORKS ──────────────────────────────────────────────────────
function htmlFireworks({ companyName, names, systemNote }) {
	const listHtml = buildListItems(names);
	const safeCompany = escapeHtml(companyName || "");
	const safeNote = escapeHtml(systemNote || "This is an automated notification. Please do not reply.");

	return `
<div style="margin:0;padding:0;background:#0d0d1a;font-family:'Arial Black',Arial,sans-serif">
  <table width="100%" cellpadding="0" cellspacing="0" style="max-width:620px;margin:30px auto;border-radius:16px;overflow:hidden;border:2px solid #2d1b69;box-shadow:0 0 40px rgba(120,60,255,.4)">
    <tr>
      <td style="background:linear-gradient(180deg,#1a0533 0%,#0d0d1a 100%);padding:36px 32px;text-align:center">
        <div style="font-size:60px;line-height:1;filter:drop-shadow(0 0 12px gold)">🎆</div>
        <h1 style="margin:16px 0 6px;color:#ffd700;font-size:26px;letter-spacing:2px;text-shadow:0 0 20px rgba(255,215,0,.6)">HAPPY BIRTHDAY!</h1>
        ${safeCompany ? `<p style="margin:0;color:#9d7fd4;font-size:13px;letter-spacing:1px">${safeCompany}</p>` : ""}
      </td>
    </tr>
    <tr>
      <td style="background:#0d0d1a;padding:32px 36px;border-top:1px solid #2d1b69">
        <p style="margin:0 0 12px;color:#c084fc;font-size:14px">🎇 Today's celebration is for:</p>
        <ul style="margin:0 0 20px;padding-left:20px;color:#fde68a;font-size:16px;font-weight:bold;line-height:2">
          ${listHtml}
        </ul>
        <div style="background:linear-gradient(135deg,rgba(120,60,255,.15),rgba(255,215,0,.1));border:1px solid rgba(255,215,0,.2);border-radius:8px;padding:16px;text-align:center">
          <p style="margin:0;color:#e9d5ff;font-size:14px;">
            Let the fireworks begin! 🎆 A new year of adventures awaits—<br/>
            may every moment be brighter than the last! ✨
          </p>
        </div>
        <div style="text-align:center;margin-top:20px;font-size:36px">🎆 🎇 🌟 🎆</div>
      </td>
    </tr>
    <tr>
      <td style="background:#1a0533;padding:12px 32px;text-align:center;border-top:1px solid #2d1b69">
        <p style="margin:0;color:#6b21a8;font-size:11px">${safeNote}</p>
      </td>
    </tr>
  </table>
</div>`;
}

// ─── TEMPLATE: FLORAL ─────────────────────────────────────────────────────────
function htmlFloral({ companyName, names, systemNote }) {
	const listHtml = buildListItems(names);
	const safeCompany = escapeHtml(companyName || "");
	const safeNote = escapeHtml(systemNote || "This is an automated notification. Please do not reply.");

	return `
<div style="margin:0;padding:0;background:#fdf2f8;font-family:'Georgia',serif">
  <table width="100%" cellpadding="0" cellspacing="0" style="max-width:620px;margin:30px auto;border-radius:16px;overflow:hidden;box-shadow:0 4px 24px rgba(219,39,119,.12);border:1px solid #fbcfe8">
    <tr>
      <td style="background:linear-gradient(135deg,#f9a8d4 0%,#fde68a 50%,#a7f3d0 100%);padding:40px 32px;text-align:center">
        <div style="font-size:56px;line-height:1">🌸</div>
        <h1 style="margin:14px 0 6px;color:#831843;font-size:24px;font-style:italic">A Blooming Birthday!</h1>
        ${safeCompany ? `<p style="margin:0;color:#be185d;font-size:13px">${safeCompany}</p>` : ""}
      </td>
    </tr>
    <tr>
      <td style="background:#fff;padding:32px 36px">
        <p style="margin:0 0 12px;color:#9d174d;font-size:15px;text-align:center">🌺 Celebrating today's birthday star(s):</p>
        <ul style="margin:0 0 20px;padding-left:20px;color:#be185d;font-size:16px;font-style:italic;line-height:2.2">
          ${listHtml}
        </ul>
        <div style="background:linear-gradient(135deg,#fce7f3,#fdf2f8);border-left:4px solid #f9a8d4;padding:16px;border-radius:0 8px 8px 0">
          <p style="margin:0;color:#831843;font-size:14px;font-style:italic;line-height:1.8">
            "Life is a garden, dig it. 🌷"<br/>
            May your day be as beautiful as a garden in full bloom, filled with love and laughter!
          </p>
        </div>
        <div style="text-align:center;margin-top:20px;font-size:32px">🌸 🌺 🌷 💐 🌻</div>
      </td>
    </tr>
    <tr>
      <td style="background:#fce7f3;padding:12px 32px;text-align:center;border-top:1px solid #fbcfe8">
        <p style="margin:0;color:#db2777;font-size:11px">${safeNote}</p>
      </td>
    </tr>
  </table>
</div>`;
}

// ─── TEMPLATE: RETRO ──────────────────────────────────────────────────────────
function htmlRetro({ companyName, names, systemNote }) {
	const listHtml = buildListItems(names);
	const safeCompany = escapeHtml(companyName || "");
	const safeNote = escapeHtml(systemNote || "This is an automated notification. Please do not reply.");
	const badgeStyle =
		"display:inline-block;background:#ff00ff;color:#fff;border-radius:4px;padding:3px 12px;font-size:13px;margin:3px;font-family:'Courier New',monospace;letter-spacing:1px;border:1px solid #ff69b4;box-shadow:2px 2px 0 #00ffff";
	const badgesHtml = buildNamesBadges(names, badgeStyle);

	return `
<div style="margin:0;padding:0;background:#0a0a0a;font-family:'Courier New',Courier,monospace">
  <table width="100%" cellpadding="0" cellspacing="0" style="max-width:620px;margin:30px auto;border-radius:0;overflow:hidden;border:3px solid #ff00ff;box-shadow:0 0 20px #ff00ff,0 0 40px #00ffff,inset 0 0 20px rgba(255,0,255,.1)">
    <tr>
      <td style="background:#0a0a0a;padding:36px 32px;text-align:center;border-bottom:2px solid #ff00ff">
        <div style="font-size:52px;filter:drop-shadow(0 0 10px #ff00ff)">🎮</div>
        <h1 style="margin:14px 0 4px;background:linear-gradient(90deg,#ff00ff,#00ffff,#ff00ff);-webkit-background-clip:text;-webkit-text-fill-color:transparent;font-size:28px;letter-spacing:4px;text-transform:uppercase">BIRTHDAY!</h1>
        <p style="margin:0;color:#00ffff;font-size:11px;letter-spacing:3px;text-shadow:0 0 8px #00ffff">⚡ SYSTEM BROADCAST ⚡</p>
        ${safeCompany ? `<p style="margin:10px 0 0;color:#ff69b4;font-size:12px;letter-spacing:2px">${safeCompany}</p>` : ""}
      </td>
    </tr>
    <tr>
      <td style="background:#0a0a0a;padding:32px 36px">
        <p style="margin:0 0 16px;color:#00ffff;font-size:13px;letter-spacing:1px">► PLAYER(S) LEVELING UP TODAY:</p>
        <div style="margin:0 0 20px">${badgesHtml}</div>
        <ul style="color:#ff69b4;font-size:14px;line-height:2;padding-left:16px">${listHtml}</ul>
        <div style="background:rgba(255,0,255,.05);border:1px solid #ff00ff;padding:16px;margin-top:8px">
          <p style="margin:0;color:#00ffff;font-size:13px;letter-spacing:.5px">
            🕹️ ACHIEVEMENT UNLOCKED: Another year of awesomeness!<br/>
            📡 Send birthday wishes via all channels. Party mode: ACTIVATED!
          </p>
        </div>
        <div style="text-align:center;margin-top:20px;font-size:32px;filter:drop-shadow(0 0 6px #ff00ff)">🎮 👾 🕹️ 👾 🎮</div>
      </td>
    </tr>
    <tr>
      <td style="background:#0a0a0a;padding:12px 32px;text-align:center;border-top:2px solid #ff00ff">
        <p style="margin:0;color:#444;font-size:11px">${safeNote}</p>
      </td>
    </tr>
  </table>
</div>`;
}

// ─── DISPATCHER ───────────────────────────────────────────────────────────────
function htmlFor({ templateId, companyName, names, systemNote }) {
	const opts = { companyName, names, systemNote };
	const t = resolveTemplateId(templateId);
	switch (t) {
		case "confetti":
			return htmlConfetti(opts);
		case "spotlight":
			return htmlSpotlight(opts);
		case "formal_id":
			return htmlFormalId(opts);
		case "bilingual":
			return htmlBilingual(opts);
		case "minimal":
			return htmlMinimal(opts);
		case "fireworks":
			return htmlFireworks(opts);
		case "floral":
			return htmlFloral(opts);
		case "retro":
			return htmlRetro(opts);
		case "classic":
		default:
			return htmlClassic(opts);
	}
}

function build({ company_id, companyName, birthdayEmployees, templateId, eventDate }) {
	const names = (birthdayEmployees || []).map((e) => e?.employee_name).filter(Boolean);
	const headline = names.length === 1 ? names[0] : `${names.length} employee(s)`;

	// Resolve random before storing in meta so it is deterministic in the email
	const resolvedTemplateId = resolveTemplateId(templateId || "classic");

	const meta = {
		type: "BIRTHDAY_ANNOUNCEMENT",
		company_id: company_id ?? null,
		template_id: resolvedTemplateId,
		event_date: eventDate || null,
	};

	const subject = subjectFor({ templateId: resolvedTemplateId, companyName, headline }).trim();
	const html = `${metaComment(meta)}\n${htmlFor({ templateId: resolvedTemplateId, companyName, names })}`;

	return { subject, html };
}

module.exports = {
	build,
	ALL_TEMPLATE_IDS,
};
