import { auth } from "@/app/(auth)/auth"; import { getChatById, getPendingVideoMessagesByChatId } from "@/lib/db/queries"; export const dynamic = "force-dynamic"; export const revalidate = 0; export async function GET(request: Request) { try { const session = await auth(); if (!session?.user?.id) { return new Response(JSON.stringify({ error: "Unauthorized" }), { status: 401, headers: { "Content-Type": "application/json" }, }); } const { searchParams } = new URL(request.url); const chatId = searchParams.get("chatId"); if (!chatId) { return new Response(JSON.stringify({ error: "Missing chatId" }), { status: 400, headers: { "Content-Type": "application/json" }, }); } const selectedChat = await getChatById({ id: chatId }); if (!selectedChat || selectedChat.userId !== session.user.id) { return new Response(JSON.stringify({ error: "Forbidden" }), { status: 403, headers: { "Content-Type": "application/json" }, }); } const pendingMessages = await getPendingVideoMessagesByChatId({ chatId }); const tasks = pendingMessages.map((msg) => { const metadata = (msg.videoMetadata as Record) || {}; return { messageId: msg.id, taskId: typeof metadata.taskId === "string" ? metadata.taskId : null, provider: typeof metadata.provider === "string" ? metadata.provider : null, model: typeof metadata.model === "string" ? metadata.model : null, mode: typeof metadata.mode === "string" ? metadata.mode : null, status: typeof metadata.status === "string" ? metadata.status : "queued", startedAt: metadata.startedAt ?? null, updatedAt: metadata.updatedAt ?? null, }; }); return new Response( JSON.stringify({ chatId, count: tasks.length, tasks, }), { status: 200, headers: { "Content-Type": "application/json", "Cache-Control": "no-store, max-age=0", }, } ); } catch (error) { console.error("[Video Tasks API] GET error:", error); return new Response(JSON.stringify({ error: "Internal server error" }), { status: 500, headers: { "Content-Type": "application/json" }, }); } }