import { NextResponse } from 'next/server';

import { withAuth, withPermission } from '@/lib/api/helpers';
import { errorResponse, NotFoundError } from '@/lib/errors';
import { listDealDocumentsForBerth } from '@/lib/services/documents.service';

/**
 * GET /api/v1/berths/[id]/interest-documents (renamed from
 * `/deal-documents` in the 2026-05-14 terminology sweep - canonical
 * noun is "interest").
 *
 * Lists documents attached to interests currently linked to this berth.
 * Same permission gate as the berth page itself (berths.view).
 */
export const GET = withAuth(
  withPermission('berths', 'view', async (_req, ctx, params) => {
    try {
      const berthId = params.id;
      if (!berthId) throw new NotFoundError('Berth');
      const data = await listDealDocumentsForBerth(ctx.portId, berthId);
      return NextResponse.json({ data });
    } catch (error) {
      return errorResponse(error);
    }
  }),
);
