import { NextResponse } from "next/server";

import { requireApiAccess } from "../../../../../../lib/api/auth";
import { getExportStoreFromRuntime } from "../../../../../../lib/workflow/export-store";

interface RouteContext {
  params: Promise<{ jobId: string; fileName: string }>;
}

export async function GET(request: Request, context: RouteContext) {
  const access = requireApiAccess(request);
  if (!access.ok) {
    return access.response;
  }

  const { jobId, fileName } = await context.params;
  const exportStore = getExportStoreFromRuntime();
  const response = await exportStore.readJobExport(jobId, fileName);
  if (!response) {
    return NextResponse.json({ error: "Export file not found" }, { status: 404 });
  }

  return response;
}
