import { NextResponse } from 'next/server';

import { withAuth, withPermission } from '@/lib/api/helpers';
import { errorResponse } from '@/lib/errors';
import { exportBerthPdf } from '@/lib/services/record-export';

export const POST = withAuth(
  withPermission('berths', 'view', async (req, ctx, params) => {
    try {
      const pdfBytes = await exportBerthPdf(params.id!, ctx.portId);
      return new NextResponse(Buffer.from(pdfBytes), {
        status: 200,
        headers: {
          'Content-Type': 'application/pdf',
          'Content-Disposition': 'attachment; filename="berth-spec.pdf"',
        },
      });
    } catch (error) {
      return errorResponse(error);
    }
  }),
);
