import { NextRequest, NextResponse } from "next/server"; import { auth } from "@/auth"; import { db } from "@/lib/db"; export async function GET(req: NextRequest) { const session = await auth(); if (!session?.user) { return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); } const projectId = req.nextUrl.searchParams.get("projectId"); if (!projectId) { return NextResponse.json({ error: "projectId required" }, { status: 400 }); } const shots = await db.shot.findMany({ where: { projectId, versions: { some: { shotId: { not: null }, mimeType: { startsWith: "video/" }, }, }, }, orderBy: [{ episode: "asc" }, { scene: "asc" }, { shotNumber: "asc" }], select: { id: true, shotCode: true, episode: true, status: true, thumbnailUrl: true, versions: { where: { isLatest: true, mimeType: { startsWith: "video/" }, }, take: 1, select: { id: true, versionNumber: true, fileUrl: true, thumbnailUrl: true, posterUrl: true, fps: true, approvalStatus: true, }, }, }, }); // Only return shots that actually have a latest video version const playlist = shots .map((shot) => ({ id: shot.id, shotCode: shot.shotCode, episode: shot.episode, status: shot.status, thumbnailUrl: shot.thumbnailUrl, latestVersion: shot.versions[0] ?? null, })) .filter((shot) => shot.latestVersion !== null); return NextResponse.json({ shots: playlist }); }