type MealPlanMarkdownInput = {
  label: string;
  entries: Array<{
    day: string;
    mealType: string;
    servings: number;
    note: string | null;
    recipe: { title: string } | null;
  }>;
};

export function mealPlanToMarkdown(plan: MealPlanMarkdownInput): string {
  const lines: string[] = [`# Meal Plan — ${plan.label}`, ""];

  const byDay = new Map<string, typeof plan.entries>();
  for (const entry of plan.entries) {
    const group = byDay.get(entry.day) ?? [];
    group.push(entry);
    byDay.set(entry.day, group);
  }

  for (const [day, entries] of byDay) {
    lines.push(`## ${day}`, "");
    for (const entry of entries) {
      const title = entry.recipe?.title ?? "(no recipe)";
      const note = entry.note ? ` — ${entry.note}` : "";
      lines.push(`- **${entry.mealType}**: ${title} (${entry.servings} servings)${note}`);
    }
    lines.push("");
  }

  return lines.join("\n").trim() + "\n";
}
