import { NextRequest, NextResponse } from "next/server"; import { z } from "zod"; import { createRenderExport, listExportsForQueue } from "@/lib/render-pipeline/exports"; function isAuthorized(req: NextRequest): boolean { const apiKey = process.env.API_SECRET_KEY; if (!apiKey) return false; const authHeader = req.headers.get("authorization") ?? ""; if (authHeader.startsWith("Bearer ")) return authHeader.slice(7) === apiKey; return (req.headers.get("x-api-key") ?? "") === apiKey; } const createExportSchema = z.object({ shotId: z.string().min(1), projectId: z.string().min(1), manifest: z.unknown(), submittedById: z.string().optional().nullable(), submittedByName: z.string().optional().nullable(), taskId: z.string().optional().nullable(), }); export async function GET(req: NextRequest) { if (!isAuthorized(req)) { return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); } const exports = await listExportsForQueue(); return NextResponse.json({ exports }); } export async function POST(req: NextRequest) { if (!isAuthorized(req)) { return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); } try { const body = await req.json(); const parsed = createExportSchema.parse(body); const result = await createRenderExport(parsed); return NextResponse.json(result, { status: 201 }); } catch (error) { if (error instanceof z.ZodError) { return NextResponse.json({ error: "Validation error", details: error.errors }, { status: 422 }); } return NextResponse.json({ error: error instanceof Error ? error.message : "Failed to queue export" }, { status: 400 }); } }