import { NextResponse } from 'next/server';

import { withAuth, withPermission } from '@/lib/api/helpers';
import { errorResponse } from '@/lib/errors';
import { checkDocumensoHealth } from '@/lib/services/documenso-client';

/**
 * Admin probe - calls Documenso /api/v1/health using the port's effective
 * config. Used by the "Test connection" button on /admin/documenso.
 */
export const POST = withAuth(
  withPermission('admin', 'manage_settings', async (_req, ctx) => {
    try {
      const result = await checkDocumensoHealth(ctx.portId);
      return NextResponse.json({ data: result });
    } catch (error) {
      return errorResponse(error);
    }
  }),
);
