from fastapi import APIRouter, HTTPException, Request from app.services.planner import planner from app.services.repository import repo from app.services.store import JsonStore router = APIRouter(prefix="/api/planner", tags=["planner"]) store = JsonStore() @router.post("/generate") async def generate_plan(request: Request): payload = await request.json() week_start = str(payload.get("week_start") or "").strip() if not week_start: raise HTTPException(status_code=400, detail="week_start is required.") anchors = payload.get("anchors") or [] if isinstance(anchors, str): anchors = [line.strip() for line in anchors.splitlines() if line.strip()] target_meals = max(3, min(7, int(payload.get("target_meals") or 5))) plan = planner.generate(week_start=week_start, anchors=list(anchors), target_meals=target_meals) record = {"id": store.new_id("mealplan"), **plan, "created_at": store.now()} repo.create_meal_plan(record) return {"status": "ok", "plan": record}