const mysql = require('mysql2/promise');
const axios = require('axios');

async function testSuperAdminCompleteAccess() {
  try {
    console.log('🔍 Testing Super Admin Complete Access...');
    
    // Test login API
    try {
      console.log('\n🔑 Testing Login API...');
      const loginResponse = await axios.post('http://localhost:5000/api/auth/login', {
        email: 's_admin@gmail.com',
        password: 'admin123'
      });

      console.log('✅ Login Response Status:', loginResponse.status);
      
      if (loginResponse.data && loginResponse.data.data) {
        const userData = loginResponse.data.data.user;
        console.log('\n👤 Super Admin User Data:');
        console.log(`  ID: ${userData.id}`);
        console.log(`  Name: ${userData.name}`);
        console.log(`  Email: ${userData.email}`);
        console.log(`  Role: "${userData.role}"`);
        console.log(`  Role ID: ${userData.role_id}`);
        console.log(`  Permissions Count: ${userData.permissions?.length || 0}`);
        
        // Check if role is correct
        console.log('\n🔍 Role Check:');
        console.log(`  Expected: "Super Admin"`);
        console.log(`  Actual: "${userData.role}"`);
        console.log(`  Match: ${userData.role === 'Super Admin' ? '✅ YES' : '❌ NO'}`);
        
        // Check critical permissions
        const criticalPerms = [
          'dashboard.view',
          'settings.access',
          'admin.access',
          'user.read',
          'user.manage_roles',
          'role.create',
          'permission.create'
        ];
        
        console.log('\n🎯 Critical Permissions Check:');
        criticalPerms.forEach(perm => {
          const hasPermission = userData.permissions?.includes(perm);
          console.log(`  ${hasPermission ? '✅' : '❌'} ${perm}`);
        });

        // Check dashboard permissions
        const dashboardPerms = userData.permissions?.filter(p => p.includes('dashboard')) || [];
        console.log(`\n📊 Dashboard Permissions (${dashboardPerms.length}):`);
        dashboardPerms.forEach(perm => {
          console.log(`  ✅ ${perm}`);
        });

        // Check settings permissions
        const settingsPerms = userData.permissions?.filter(p => p.includes('settings')) || [];
        console.log(`\n⚙️ Settings Permissions (${settingsPerms.length}):`);
        settingsPerms.forEach(perm => {
          console.log(`  ✅ ${perm}`);
        });

        console.log('\n🎉 Super Admin Login Test Results:');
        console.log(`✅ Login: SUCCESS`);
        console.log(`✅ Role: ${userData.role === 'Super Admin' ? 'CORRECT' : 'INCORRECT'}`);
        console.log(`✅ Permissions: ${userData.permissions?.length || 0} loaded`);
        console.log(`✅ Critical Perms: ${criticalPerms.filter(p => userData.permissions?.includes(p)).length}/${criticalPerms.length}`);

      } else {
        console.log('❌ No user data in response');
      }

    } catch (apiError) {
      console.log('❌ API Error:', apiError.response?.data?.message || apiError.message);
      if (apiError.response?.status === 500) {
        console.log('💡 Backend server might not be running. Please start it with: npm run dev');
      }
    }

  } catch (error) {
    console.error('❌ Error:', error.message);
  }
}

testSuperAdminCompleteAccess();
