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

async function verifySuperAdminAccess() {
  try {
    const connection = await mysql.createConnection({
      host: 'localhost',
      user: 'root',
      password: '',
      database: 'cms_db'
    });

    console.log('🔍 Verifying Super Admin Access...');
    
    // Get Super Admin role
    const [roles] = await connection.execute('SELECT * FROM roles WHERE name = ?', ['Super Admin']);
    const superAdminRole = roles[0];
    
    // Get all permissions for Super Admin
    const [permissions] = await connection.execute(`
      SELECT p.name 
      FROM permissions p 
      JOIN role_permissions rp ON p.id = rp.permission_id 
      WHERE rp.role_id = ?
      ORDER BY p.name
    `, [superAdminRole.id]);
    
    const permissionNames = permissions.map(p => p.name);
    
    // Check specific permissions needed for Dashboard
    const requiredPermissions = [
      'dashboard.view',
      'dashboard.analytics', 
      'dashboard.widget.contacts',
      'dashboard.widget.conversations',
      'dashboard.widget.online',
      'dashboard.widget.messages',
      'dashboard.widget.security',
      'user.manage_roles',
      'user.read',
      'audit_log.read',
      'security.view',
      'conversation.read',
      'message.read',
      'message.create',
      'conversation.create',
      'system_settings.read'
    ];
    
    console.log('\n🎯 Required Dashboard Permissions Check:');
    let allPresent = true;
    requiredPermissions.forEach(perm => {
      const hasPermission = permissionNames.includes(perm);
      console.log(`  ${hasPermission ? '✅' : '❌'} ${perm}`);
      if (!hasPermission) allPresent = false;
    });
    
    console.log(`\n📊 Total permissions: ${permissionNames.length}`);
    console.log(`🎯 Required permissions: ${requiredPermissions.length}`);
    console.log(`✅ All required permissions present: ${allPresent ? 'YES' : 'NO'}`);
    
    if (allPresent) {
      console.log('\n🎉 Super Admin has FULL ACCESS to all dashboard features!');
    } else {
      console.log('\n⚠️ Some permissions are missing. Need to add them.');
    }
    
    await connection.end();
  } catch (error) {
    console.error('❌ Error:', error.message);
  }
}

verifySuperAdminAccess();
