export default defineEventHandler(async (event) => {
  console.log('[api/admin/registration-config.get] =========================');
  console.log('[api/admin/registration-config.get] GET /api/admin/registration-config - Get registration configuration');
  
  try {
    // Validate session and require admin privileges
    const sessionManager = createSessionManager();
    const cookieHeader = getCookie(event, 'monacousa-session') ? getHeader(event, 'cookie') : undefined;
    const session = sessionManager.getSession(cookieHeader);
    
    if (!session?.user) {
      throw createError({
        statusCode: 401,
        statusMessage: 'Authentication required'
      });
    }

    if (session.user.tier !== 'admin') {
      throw createError({
        statusCode: 403,
        statusMessage: 'Admin privileges required'
      });
    }

    console.log('[api/admin/registration-config.get] Authorized admin:', session.user.email);

    // Force reload and get registration configuration
    const { getRegistrationConfig, reloadAdminConfig } = await import('~/server/utils/admin-config');
    
    // Force reload configuration to ensure we have the latest settings
    try {
      await reloadAdminConfig();
    } catch (error) {
      console.warn('[api/admin/registration-config.get] Failed to reload config, using cached version:', error);
    }
    
    const config = getRegistrationConfig();
    
    return {
      success: true,
      data: config
    };

  } catch (error: any) {
    console.error('[api/admin/registration-config.get] ❌ Error getting registration config:', error);
    throw error;
  }
});
