import { NextResponse } from 'next/server';

import { withAuth, withPermission, withRateLimit } from '@/lib/api/helpers';
import { errorResponse } from '@/lib/errors';
import { getExportDownloadUrl } from '@/lib/services/gdpr-export.service';

/**
 * Returns a fresh signed URL for an existing GDPR export. Staff use this
 * from the admin UI; the email path embeds its own signed URL.
 */
export const GET = withAuth(
  withPermission(
    'admin',
    'manage_settings',
    withRateLimit('exports', async (req, ctx, params) => {
      try {
        const url = await getExportDownloadUrl(params.exportId!, ctx.portId);
        return NextResponse.json({ data: { url } });
      } catch (error) {
        return errorResponse(error);
      }
    }),
  ),
);
