const db = require('../src/db');

async function verify() {
    try {
        console.log('🔍 System Verification Started...');

        // 1. Check Record Counts
        const userCount = await db.query('SELECT COUNT(*) as count FROM users');
        const alertCount = await db.query('SELECT COUNT(*) as count FROM alerts');
        const opCount = await db.query('SELECT COUNT(*) as count FROM operations');

        console.log(`📊 Stats: Users(${userCount.rows[0].count}), Alerts(${alertCount.rows[0].count}), Operations(${opCount.rows[0].count})`);

        if (userCount.rows[0].count === 0) throw new Error('Data Inconsistency: 0 users found!');

        // 2. Check Schema Persistence
        const userCols = await db.query('DESCRIBE users');
        const hasFocusArea = userCols.rows.some(r => r.Field === 'focus_area');
        console.log(`🛠️ Schema: focus_area column present: ${hasFocusArea}`);

        const opCols = await db.query('DESCRIBE operations');
        const hasIconUrl = opCols.rows.some(r => r.Field === 'icon_url');
        console.log(`🛠️ Schema: icon_url column present: ${hasIconUrl}`);

        // 3. Data Integrity Check (Sample Users)
        const sampleUsers = await db.query('SELECT id, name, email, focus_area FROM users LIMIT 3');
        console.table(sampleUsers.rows);

        // 4. Operation Integrity
        const sampleOps = await db.query('SELECT id, title, icon_url FROM operations LIMIT 3');
        console.table(sampleOps.rows);

        console.log('✅ System Verification PASSED! The new dump is compatible with the latest schema changes.');
    } catch (error) {
        console.error('❌ Verification FAILED:', error.message);
    } finally {
        process.exit();
    }
}

verify();
