import { NextResponse } from 'next/server';

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

/**
 * Manually (re)send the finalized signed PDF to the deal's client. Backs
 * the "Send signed copy to client" affordance on the EOI tab + document
 * detail. Same `documents.edit` gate as the reminder endpoint.
 */
export const POST = withAuth(
  withPermission('documents', 'edit', async (_req, ctx, params) => {
    try {
      const result = await sendSignedCopyToClient(params.id!, ctx.portId);
      return NextResponse.json({ data: result });
    } catch (error) {
      return errorResponse(error);
    }
  }),
);
