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

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

    console.log('🔍 Complete System Check...\n');
    
    // 1. Check Super Admin user
    const [users] = await connection.execute('SELECT id, name, email, role_id, is_active FROM users WHERE role_id = 1 LIMIT 1');
    console.log('1️⃣ Super Admin User:');
    if (users.length === 0) {
      console.log('❌ NO Super Admin user found!');
    } else {
      const user = users[0];
      console.log(`✅ Found: ${user.name} (${user.email})`);
      console.log(`   ID: ${user.id}, Role ID: ${user.role_id}, Active: ${user.is_active ? 'Yes' : 'No'}`);
    }

    // 2. Check Super Admin role
    console.log('\n2️⃣ Super Admin Role:');
    const [roles] = await connection.execute('SELECT * FROM roles WHERE id = 1');
    if (roles.length === 0) {
      console.log('❌ NO Super Admin role found!');
    } else {
      const role = roles[0];
      console.log(`✅ Name: ${role.name}, Level: ${role.level}, Active: ${role.is_active ? 'Yes' : 'No'}`);
    }

    // 3. Check permissions count
    console.log('\n3️⃣ Permissions Count:');
    const [permCount] = await connection.execute('SELECT COUNT(*) as total FROM permissions');
    console.log(`✅ Total permissions in database: ${permCount[0].total}`);

    // 4. Check role_permissions for Super Admin
    console.log('\n4️⃣ Super Admin Role-Permission Mappings:');
    const [rpCount] = await connection.execute('SELECT COUNT(*) as total FROM role_permissions WHERE role_id = 1');
    console.log(`✅ Total mappings for Super Admin: ${rpCount[0].total}`);

    // 5. Check specific critical permissions
    console.log('\n5️⃣ Critical Permissions Check:');
    const criticalPerms = [
      'dashboard.view',
      'settings.access',
      'user.read',
      'admin.access'
    ];
    
    for (const perm of criticalPerms) {
      const [result] = await connection.execute(`
        SELECT p.name 
        FROM permissions p
        JOIN role_permissions rp ON p.id = rp.permission_id
        WHERE rp.role_id = 1 AND p.name = ?
      `, [perm]);
      console.log(`${result.length > 0 ? '✅' : '❌'} ${perm}`);
    }

    // 6. Check if backend server is running
    console.log('\n6️⃣ Backend Server Check:');
    console.log('⚠️ Please check if backend server is running on http://localhost:5000');

    // 7. Check if frontend is running
    console.log('\n7️⃣ Frontend Check:');
    console.log('⚠️ Please check if frontend is running');

    console.log('\n📋 Summary:');
    console.log(`✅ Database connection: OK`);
    console.log(`✅ Super Admin user exists: ${users.length > 0 ? 'YES' : 'NO'}`);
    console.log(`✅ Super Admin role exists: ${roles.length > 0 ? 'YES' : 'NO'}`);
    console.log(`✅ Permissions count: ${permCount[0].total}`);
    console.log(`✅ Role-permission mappings: ${rpCount[0].total}`);

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

checkCompleteSystem();
