import { createClient } from "@/lib/supabase/server"; import { requireUser } from "@/lib/api/auth"; import { ApiError, apiError, apiSuccess, parseJson } from "@/lib/api/errors"; import * as matches from "@/lib/services/matches"; export async function POST( request: Request, { params }: { params: Promise<{ matchId: string }> } ) { try { const { matchId } = await params; const supabase = await createClient(); await requireUser(supabase); const body = await parseJson<{ action: "submit" | "approve" | "set"; teamId?: string; homeScore?: number; awayScore?: number; note?: string; }>(request); switch (body.action) { case "submit": if (body.teamId == null || body.homeScore == null || body.awayScore == null) { throw new ApiError(400, "teamId, homeScore, awayScore required"); } await matches.submitResult( supabase, matchId, body.teamId, body.homeScore, body.awayScore ); break; case "approve": await matches.approveResult(supabase, matchId); break; case "set": if (body.homeScore == null || body.awayScore == null) { throw new ApiError(400, "homeScore, awayScore required"); } await matches.setResultByManager( supabase, matchId, body.homeScore, body.awayScore, body.note ); break; default: throw new ApiError(400, "action must be submit, approve, or set"); } return apiSuccess({ ok: true }); } catch (e) { return apiError(e); } }