import { NextResponse } from 'next/server';

import { withAuth, withPermission } from '@/lib/api/helpers';
import { getDownloadUrl } from '@/lib/services/reports.service';
import { errorResponse } from '@/lib/errors';

export const GET = withAuth(
  withPermission('reports', 'export', async (_req, ctx, params) => {
    try {
      const reportId = params.id ?? '';
      const result = await getDownloadUrl(reportId, ctx.portId);
      return NextResponse.json(result);
    } catch (error) {
      return errorResponse(error);
    }
  }),
);
