import { NextRequest, NextResponse } from 'next/server';
import { getSetupReadinessReport } from '@/features/setup/server/readiness.service';
import { AuthError, requireSystemRole } from '@/lib/auth/session';

export async function GET(request: NextRequest) {
  try {
    const session = await requireSystemRole('super_admin');
    const profileId = request.nextUrl.searchParams.get('profile') ?? 'crm_demo';
    const report = await getSetupReadinessReport({
      organizationId: session.user.activeOrganizationId ?? null,
      profileId
    });
    return NextResponse.json(report);
  } catch (error) {
    if (error instanceof AuthError) {
      return NextResponse.json({ message: error.message }, { status: error.status });
    }

    return NextResponse.json({ message: 'Unable to run setup readiness checks' }, { status: 500 });
  }
}
