// ─── 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 = `"O sucesso é a soma de pequenos esforços repetidos dia após dia."