import { NextRequest, NextResponse } from 'next/server';
import { getInstallationProfileBundle } from '@/features/setup/plugins/plugin-registry';
import { AuthError, requireSystemRole } from '@/lib/auth/session';

export async function GET(request: NextRequest) {
  try {
    await requireSystemRole('super_admin');
    const profileId = request.nextUrl.searchParams.get('profile') ?? 'crm_demo';
    return NextResponse.json({
      success: true,
      time: new Date().toISOString(),
      ...getInstallationProfileBundle(profileId)
    });
  } catch (error) {
    if (error instanceof AuthError) {
      return NextResponse.json({ message: error.message }, { status: error.status });
    }

    return NextResponse.json({ message: 'Unable to load setup installation profiles' }, { status: 500 });
  }
}
