import { NextResponse } from 'next/server';

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

export const POST = withAuth(
  withPermission('documents', 'view', async (_req, ctx, params) => {
    try {
      const draft = await composeSignedDocEmail(params.id!, ctx.portId);
      return NextResponse.json({ data: draft });
    } catch (error) {
      return errorResponse(error);
    }
  }),
);
