import { NextResponse } from "next/server" import { z } from "zod" import { requireUser } from "@/lib/authz" import { promoteRecipe } from "@/lib/recipe-promotion" /** * Promote a saved recipe into the collection so it can be rated, or onto the * wishlist to try later. * * All the logic lives in src/lib/recipe-promotion.ts, shared with the MCP tool. */ const bodySchema = z.object({ target: z.enum(["drink", "wishlist"]), }) export async function POST( request: Request, { params }: { params: { id: string } } ) { const session = await requireUser() if (session instanceof NextResponse) return session const parsed = bodySchema.safeParse(await request.json().catch(() => null)) if (!parsed.success) { return NextResponse.json( { error: "target must be \"drink\" or \"wishlist\"" }, { status: 400 } ) } const result = await promoteRecipe( session.user.id, params.id, parsed.data.target ) if (result.kind === "not_found") { return NextResponse.json({ error: "Not found" }, { status: 404 }) } if (result.kind === "drink") { return NextResponse.json( { target: "drink", drink: result.drink, existing: result.existing }, // 200 rather than 201 when it was already linked - nothing was created. { status: result.existing ? 200 : 201 } ) } return NextResponse.json( { target: "wishlist", item: result.item, existing: result.existing }, { status: result.existing ? 200 : 201 } ) }