export default defineEventHandler(async (event) => {
  console.log('🔍 Session check requested at:', new Date().toISOString());
  
  try {
    const sessionManager = createSessionManager();
    const cookieHeader = getHeader(event, 'cookie');
    
    console.log('🍪 Cookie header present:', !!cookieHeader);
    console.log('🍪 Cookie header length:', cookieHeader?.length || 0);
    
    const session = sessionManager.getSession(cookieHeader);

    if (!session) {
      console.log('❌ No valid session found');
      return {
        authenticated: false,
        user: null,
        groups: [],
      };
    }

    console.log('✅ Valid session found for user:', session.user.email);
    console.log('🎯 User tier:', session.user.tier);
    console.log('📋 User groups:', session.user.groups);
    console.log('👤 User data:', {
      id: session.user.id,
      email: session.user.email,
      name: session.user.name,
      firstName: session.user.firstName,
      lastName: session.user.lastName,
      username: session.user.username
    });

    return {
      authenticated: true,
      user: session.user,
      groups: session.user.groups || [],
    };
  } catch (error) {
    console.error('❌ Session check error:', error);
    return {
      authenticated: false,
      user: null,
      groups: [],
    };
  }
});
