// ─── Geração de HTML para PDF do Coach (Premium 3 Páginas Compacto) ──────── function truncateText(text: string, max = 500): string { const t = (text || "").trim(); if (!t) return "-"; return t.length > max ? t.slice(0, max - 1) + "…" : t; } function safeStr(v: any, fallback = "-"): string { if (v === null || v === undefined) return fallback; if (typeof v === "string") return v.trim() || fallback; if (typeof v === "number") return Number.isFinite(v) ? String(v) : fallback; return fallback; } export function buildCoachPdfHtml(plan: any): string { const diet = plan.diet || {}; const workout = plan.workout || {}; const analysis = plan.analysis || {}; const quote = plan.motivation_quote || "Disciplina é a ponte entre metas e conquistas."; // --- Data Prep --- const protein = diet.macros?.protein_g ?? "–"; const carbs = diet.macros?.carbs_g ?? "–"; const fats = diet.macros?.fats_g ?? "–"; const water = diet.hydration_liters ?? "–"; const calories = Math.round(diet.total_calories || 0); const somatotype = safeStr(analysis.somatotype); const goal = safeStr(workout.focus); const split = safeStr(workout.split); // Lists const positives = (Array.isArray(analysis.strengths) ? analysis.strengths : []) .map((x: any) => typeof x === "string" ? x : x?.text).filter(Boolean); // Removed slice limit // Map 'weaknesses' to 'improvements' (Prompt returns weaknesses) const improvements = (Array.isArray(analysis.weaknesses) ? analysis.weaknesses : []) .map((x: any) => typeof x === "string" ? x : x?.text).filter(Boolean); const meals: any[] = Array.isArray(diet.meal_plan_example) ? diet.meal_plan_example : []; const supplements: any[] = Array.isArray(diet.supplements) ? diet.supplements : []; const routine: any[] = Array.isArray(workout.routine) ? workout.routine : []; // --- HTML Generators --- const positivesHtml = positives.length ? `` : `

${safeStr(analysis.summary, "Sem detalhes.")}

`; const improvementsHtml = improvements.length ? `` : `

${safeStr(analysis.improvement_summary, "Sem detalhes.")}

`; const mealsHtml = meals.map((meal: any, i: number) => { const options = Array.isArray(meal.options) ? meal.options : []; const opt1 = options[0] || meal.main_option || ""; const opt2 = options[1] || ""; const sub = meal.substitution_suggestion || meal.substitution || ""; let html = `
`; html += `
`; html += `
${meal.name || `Refeição ${i + 1}`}
`; if (meal.time_range) html += `
${meal.time_range}
`; html += `
#${i + 1}
`; html += `
`; if (opt1) html += `
Opção 1: ${truncateText(String(opt1), 500)}
`; if (opt2) html += `
Opção 2: ${truncateText(String(opt2), 500)}
`; if (sub) html += `
Substituição: ${truncateText(String(sub), 300)}
`; html += `
`; return html; }).join(""); const supplementsHtml = supplements.map((sup: any) => { const name = typeof sup === "string" ? sup : sup.name || "Suplemento"; const dosage = typeof sup === "string" ? "" : sup.dosage || ""; const reason = typeof sup === "string" ? "" : sup.reason || ""; // Added reason if available let html = `
`; html += `
💊
${truncateText(String(name), 100)}
`; if (dosage) html += `
${truncateText(String(dosage), 100)}
`; if (reason) html += `
${truncateText(String(reason), 150)}
`; html += `
`; return html; }).join(""); const daysHtml = routine.map((day: any, idx: number) => { const exs: any[] = Array.isArray(day.exercises) ? day.exercises : []; const dayName = day.day || day.name || `Dia ${idx + 1}`; const muscle = day.muscle_group || day.focus || ""; const exLines = exs.map((ex: any) => { if (typeof ex === "string") return `
  • ${ex}
  • `; const name = ex.name || ex.exercise || ""; const sets = ex.sets ?? ""; const reps = ex.reps ?? ""; const technique = ex.technique || ex.notes || ""; const sr = [sets ? `${sets}x` : "", reps].filter(Boolean).join(" "); const left = [name, sr].filter(Boolean).join(" — "); const full = [left, technique].filter(Boolean).join(" • "); return `
  • ${truncateText(full, 500) || "-"}
  • `; }).join(""); return `
    ${dayName}
    ${muscle}
    ${workout.split || "Diff"}
    `; }).join(""); // --- Template Compacto --- return `
    Protocolo Titan • FoodSnap Coach

    01. Diagnóstico

    Biótipo
    ${somatotype}
    Objetivo
    ${goal}
    Calorias
    ${calories}
    Split
    ${split}
    Pontos Fortes
    ${positivesHtml}
    Melhorias
    ${improvementsHtml}

    "O sucesso é a soma de pequenos esforços repetidos dia após dia."

    02. Dieta

    🥗
    PROT: ${protein}
    CARB: ${carbs}
    GORD: ${fats}
    💧 ${water}L
    Refeições
    ${mealsHtml}
    Suplementos
    ${supplementsHtml}

    03. Treino

    🏋️
    ${daysHtml}
    "${truncateText(quote, 100)}"
    `; }