function buildEmailTemplate(bodyText, data, isDE) { const esc = (v) => String(v ?? "") .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """) .replace(/'/g, "'"); const fullName = [data.first_name, data.last_name].filter(Boolean).join(" ") || ""; const greeting = fullName ? isDE ? `Liebe/Lieber ${esc(fullName)}` : `Hello ${esc(fullName)}` : isDE ? "Hallo" : "Hello"; const risk = data.risk_level || "ASSESSED"; const scenarios = (data.scenarios || "").split(",").map((s) => s.trim()).filter(Boolean); const scenarioLabels = { total_collapse: isDE ? "Totaler Kollaps" : "Total Collapse", partial_collapse: isDE ? "Partieller Kollaps" : "Partial Collapse", hyperinflation: isDE ? "Hyperinflation" : "Hyperinflation", supply_shock: isDE ? "Versorgungsengpass" : "Supply Side Shock", food_crisis: isDE ? "Lebensmittelkrise" : "Food Crisis", bank_crisis: isDE ? "Banken- / Finanzkrise" : "Bank / Financial Crisis", power_outage: isDE ? "Laengerer Stromausfall" : "Extended Power Outage", water_failure: isDE ? "Wasserversorgungsausfall" : "Water Supply Failure", war: isDE ? "Krieg / Bewaffneter Konflikt" : "War / Armed Conflict" }; const scenarioBadges = scenarios.map((s) => `${esc(scenarioLabels[s] || s)}` ).join(""); const formatText = (text) => { return esc(text).split(/\n\n+/).filter((p) => p.trim()).map((p) => { if (p.startsWith("* ") || p.startsWith("- ") || p.startsWith("•") || p.includes("\n•")) { const items = p.split("\n").filter((l) => l.trim()).map((l) => { const clean = l.trim().replace(/^(\*|-|•)\s*/, ""); const linked = clean.replace(/(https?:\/\/[^\s<]+)/g, `${isDE ? "Ansehen" : "View"}`); const bolded = linked.replace(/\*\*(.+?)\*\*/g, '$1'); return `
  • ${bolded}
  • `; }).join(""); return ``; } const linked = p.replace(/(https?:\/\/[^\s<]+)/g, `${isDE ? "Ansehen" : "View"}`); const bolded = linked.replace(/\*\*(.+?)\*\*/g, '$1'); return `

    ${bolded}

    `; }).join(""); }; const body = formatText(bodyText); const restoreQuery = data.report_id && data.report_token ? `?r=${encodeURIComponent(data.report_id)}&t=${encodeURIComponent(data.report_token)}` : ""; const reportUrl = `https://plan-b.now/${restoreQuery}#results-section`; const analysisUrl = `https://plan-b.now/${restoreQuery}#narrative-section`; const recommendationsUrl = `https://plan-b.now/${restoreQuery}#recs-anchor`; const budgetUrl = `https://plan-b.now/${restoreQuery}#panel-budget`; const timelineUrl = `https://plan-b.now/${restoreQuery}#panel-timeline`; const proteinUrl = `https://plan-b.now/${restoreQuery}#protein-offer-section`; return ` ${isDE ? "Dein Plan-B Notfallplan" : "Your Plan-B Preparedness Plan"}
    Plan-B
    ${isDE ? "Persoenlicher Vorsorgeplan" : "Personal Preparedness Plan"}

    ${esc(getRiskLabel(risk, isDE))} ${isDE ? "Bericht" : "Readiness Report"}

    ${greeting},

    ${isDE ? "Risikoprofil" : "Risk Profile"}
    ${esc(getRiskLabel(risk, isDE))}
    ${scenarioBadges ? `
    ${scenarioBadges}
    ` : ""}
    ${body}
    ${isDE ? "Meinen Bericht oeffnen" : "Open My Report"}
    ${isDE ? "Direkt zu deinem Bericht" : "Jump back to your report"}
    ${isDE ? "Analyse" : "Analysis"} ${isDE ? "Empfehlungen" : "Recommendations"}
    ${isDE ? "Budget" : "Budget"} ${isDE ? "Zeitplan" : "Timeline"}
    ${data.protein_security === "uncertain" ? `
    ${isDE ? "Proteinquelle sichern" : "Secure protein source"}
    ` : ""}
    ${isDE ? "Dein Profil" : "Your Profile"}
    ${erow(isDE ? "Name" : "Name", esc(fullName))} ${erow(isDE ? "Wohnort" : "Location", esc([data.city, data.country].filter(Boolean).join(", ")))} ${erow(isDE ? "Haushalt" : "Household", esc(householdLabel(data.household_size, isDE)))} ${erow(isDE ? "Budget" : "Budget", data.budget_eur ? `EUR ${esc(data.budget_eur)}` : "")} ${erow(isDE ? "Szenarien" : "Scenarios", esc(scenarios.map((s) => scenarioLabels[s] || s).join(", ")))}

    ${isDE ? 'Du erhaeltst diese E-Mail, weil du das Plan-B Assessment abgeschlossen hast.' : 'You are receiving this because you completed the Plan-B assessment.'} ${isDE ? "Abmelden" : "Unsubscribe"}. ${isDE ? "Deine Daten werden nie verkauft." : "Your data is never sold."}

    `; }