import { NextResponse } from 'next/server';

import { withAuth, withPermission } from '@/lib/api/helpers';
import { errorResponse, NotFoundError } from '@/lib/errors';
import { getReportRun } from '@/lib/services/report-runs.service';

/**
 * GET /api/v1/reports/runs/[id]
 *   Single-run detail. Status polling target for clients waiting on the
 *   render worker; download URL comes from a sibling endpoint
 *   (`/api/v1/reports/runs/[id]/download` — P3) once status is `complete`.
 */
export const GET = withAuth(
  withPermission('reports', 'view_dashboard', async (_req, ctx, params) => {
    try {
      const id = params.id;
      if (!id) throw new NotFoundError('Report run');
      const row = await getReportRun(id, ctx.portId);
      return NextResponse.json({ data: row });
    } catch (error) {
      return errorResponse(error);
    }
  }),
);
