import { NextResponse } from "next/server"; import { z } from "zod"; import { deletePlaySession, endPlaySession, getPlaySessionDetail } from "@/lib/db/queries/play-sessions"; const patchSchema = z.object({ status: z.enum(["completed", "aborted"]) }); export async function GET(_req: Request, { params }: { params: Promise<{ id: string }> }) { const { id } = await params; const detail = await getPlaySessionDetail(Number(id)); if (!detail) return NextResponse.json({ error: "not found" }, { status: 404 }); return NextResponse.json(detail); } export async function PATCH(req: Request, { params }: { params: Promise<{ id: string }> }) { const { id } = await params; const parsed = patchSchema.safeParse(await req.json().catch(() => null)); if (!parsed.success) { return NextResponse.json({ error: "status must be completed or aborted" }, { status: 400 }); } const updated = await endPlaySession(Number(id), parsed.data); if (!updated) return NextResponse.json({ error: "not found" }, { status: 404 }); return NextResponse.json({ playSession: updated }); } export async function DELETE(_req: Request, { params }: { params: Promise<{ id: string }> }) { const { id } = await params; try { await deletePlaySession(Number(id)); return NextResponse.json({ ok: true }); } catch (err) { const message = err instanceof Error ? err.message : String(err); if (message.includes("FOREIGN KEY") || message.includes("SQLITE_CONSTRAINT")) { return NextResponse.json( { error: "cannot delete a session that a saved recording still references" }, { status: 409 }, ); } throw err; } }