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

// Database configuration
const dbConfig = {
  host: 'localhost',
  user: 'root',
  password: '',
  database: 'cms_db',
  port: 3306
};

async function testRole16Dashboard() {
  let connection;
  
  try {
    console.log('🧪 Testing Role ID 16 Dashboard Configuration...\n');
    
    connection = await mysql.createConnection(dbConfig);
    
    // Check role_id 16 details
    console.log('📋 Role ID 16 Details:');
    const [role] = await connection.execute(`
      SELECT id, name, description, level 
      FROM roles 
      WHERE id = 16
    `);
    
    if (role.length === 0) {
      console.log('❌ Role ID 16 not found!');
      return;
    }
    
    console.log(`   ID: ${role[0].id}`);
    console.log(`   Name: ${role[0].name}`);
    console.log(`   Description: ${role[0].description}`);
    console.log(`   Level: ${role[0].level}`);
    
    // Check dashboard widget permissions for role_id 16
    console.log('\n🎯 Dashboard Widget Permissions for Role ID 16:');
    const [dashboardPermissions] = await connection.execute(`
      SELECT 
        p.name,
        p.description,
        p.resource,
        p.action
      FROM permissions p
      JOIN role_permissions rp ON p.id = rp.permission_id
      WHERE rp.role_id = 16 
      AND p.name IN ('dashboard.widget.total_contacts', 'dashboard.widget.active_conversations', 'dashboard.widget.online_contacts', 'dashboard.widget.user_management')
      ORDER BY p.name
    `);
    
    console.log(`   Found ${dashboardPermissions.length} dashboard widget permissions:`);
    dashboardPermissions.forEach(perm => {
      console.log(`      ✅ ${perm.name}: ${perm.description}`);
    });
    
    // Check if other roles have these permissions (they shouldn't)
    console.log('\n🚫 Other Roles with Dashboard Widget Permissions:');
    const [otherRoles] = await connection.execute(`
      SELECT DISTINCT r.id, r.name, r.level, COUNT(p.id) as permission_count
      FROM roles r
      JOIN role_permissions rp ON r.id = rp.role_id
      JOIN permissions p ON rp.permission_id = p.id
      WHERE r.id != 16 
      AND p.name IN ('dashboard.widget.total_contacts', 'dashboard.widget.active_conversations', 'dashboard.widget.online_contacts', 'dashboard.widget.user_management')
      GROUP BY r.id, r.name, r.level
      ORDER BY r.level DESC
    `);
    
    if (otherRoles.length === 0) {
      console.log('   ✅ SUCCESS: No other roles have these dashboard widget permissions');
    } else {
      console.log('   ⚠️  Other roles that have these permissions:');
      otherRoles.forEach(role => {
        console.log(`      - Role ID ${role.id}: ${role.name} (Level: ${role.level}) - ${role.permission_count} permissions`);
      });
    }
    
    // Check analytics permissions for role_id 16
    console.log('\n📊 Analytics Permissions for Role ID 16:');
    const [analyticsPermissions] = await connection.execute(`
      SELECT 
        p.name,
        p.description
      FROM permissions p
      JOIN role_permissions rp ON p.id = rp.permission_id
      WHERE rp.role_id = 16 
      AND (p.name LIKE '%analytics%' OR p.name LIKE '%dashboard.analytics%')
      ORDER BY p.name
    `);
    
    if (analyticsPermissions.length === 0) {
      console.log('   ✅ SUCCESS: Role ID 16 has NO analytics permissions (Analytics section will be hidden)');
    } else {
      console.log('   ⚠️  Role ID 16 has analytics permissions:');
      analyticsPermissions.forEach(perm => {
        console.log(`      - ${perm.name}: ${perm.description}`);
      });
    }
    
    // Summary
    console.log('\n📝 Dashboard Configuration Summary for Role ID 16:');
    console.log(`   ✅ Role: ${role[0].name} (ID: ${role[0].id})`);
    console.log(`   ✅ Dashboard Widgets: ${dashboardPermissions.length}/4 (Total Contacts, Active Conversations, Online Contacts, User Management)`);
    console.log(`   ✅ Analytics Section: ${analyticsPermissions.length === 0 ? 'HIDDEN' : 'VISIBLE'}`);
    console.log(`   ✅ Quick Actions: HIDDEN (hardcoded in frontend)`);
    console.log(`   ✅ Dashboard Title: "Navy Admin Dashboard"`);
    
    if (dashboardPermissions.length === 4 && otherRoles.length === 0) {
      console.log('\n🎉 SUCCESS: Role ID 16 Dashboard is correctly configured!');
      console.log('   - Only 4 specific widgets will be shown');
      console.log('   - Analytics and Quick Actions will be hidden');
      console.log('   - No other roles have access to these widgets');
    } else {
      console.log('\n⚠️  Dashboard configuration needs attention');
    }
    
  } catch (error) {
    console.error('❌ Error:', error.message);
  } finally {
    if (connection) {
      await connection.end();
    }
  }
}

testRole16Dashboard();
