import { NextResponse } from "next/server";

import { getAccessToken } from "@/lib/auth/session";
import { getRenderJob } from "@/lib/render-jobs";

export const runtime = "nodejs";

interface RouteContext {
  params: { jobId: string };
}

export async function GET(_request: Request, context: RouteContext) {
  const { jobId } = context.params;

  if (!jobId) {
    return NextResponse.json({ error: "jobId required" }, { status: 400 });
  }

  const token = await getAccessToken();
  if (!token) {
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  }

  const job = await getRenderJob(jobId, token);
  if (!job) {
    return NextResponse.json({ error: "Job not found" }, { status: 404 });
  }

  return NextResponse.json({
    status: job.status,
    progress: job.progress,
    outputUrl: job.output_url,
    progressMessage: job.progress_message,
    errorMessage: job.error_message,
    previewB64: job.preview_b64 ?? null,
  });
}
