import { formatQuantity, hasQuantity } from "@/lib/fractions";

type PantryMarkdownInput = {
  items: Array<{ rawName: string; quantity: string | null; unit: string | null; expiresAt: string | null }>;
};

export function pantryToMarkdown(pantry: PantryMarkdownInput): string {
  const lines: string[] = ["# Pantry", ""];

  for (const item of pantry.items) {
    const qty = [hasQuantity(item.quantity) ? formatQuantity(parseFloat(item.quantity!)) : null, item.unit].filter(Boolean).join(" ");
    const expiry = item.expiresAt ? ` (expires ${new Date(item.expiresAt).toLocaleDateString()})` : "";
    lines.push(`- ${qty ? `${qty} ` : ""}${item.rawName}${expiry}`);
  }

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