import { performAction, queueLeg } from '@/lib/game/actions'; // POST /api/planes/:planeId/queue (v0.25.0, ISS-043) — body { destIcao: string }. // Appends a NOT-yet-started leg to the plane's flight queue; works whether the // plane is grounded or flying (pre-building a route). Nothing auto-starts here — // queued legs chain on landing when fuel covers them. 400 missing/non-string // destIcao, unknown fields, or an ICAO not in the airport dataset; 404 unknown // plane id. On success returns the full state snapshot (plane.queue updated). export async function POST(req: Request, ctx: { params: Promise<{ planeId: string }> }) { const { planeId } = await ctx.params; let body: unknown; try { const text = await req.text(); body = text ? JSON.parse(text) : {}; } catch { return Response.json({ error: 'invalid JSON body' }, { status: 400 }); } if (typeof body !== 'object' || body === null || Array.isArray(body)) { return Response.json({ error: 'body must be a JSON object' }, { status: 400 }); } const input = body as Record; for (const key of Object.keys(input)) { if (key !== 'destIcao') { return Response.json({ error: `unknown field "${key}"` }, { status: 400 }); } } if (typeof input.destIcao !== 'string' || input.destIcao.length === 0) { return Response.json({ error: 'destIcao must be a non-empty string' }, { status: 400 }); } const destIcao = input.destIcao; return performAction(req, (inst) => queueLeg(inst, planeId, destIcao)); }