const bcrypt = require('bcryptjs');
const { Pool } = require('pg');

const pool = new Pool({
    user: 'postgres',
    host: '127.0.0.1',
    database: 'car',
    password: 'madhu1402',
    port: 5432
});

async function fixPassword() {
    try {
        // Generate a proper bcrypt hash for 'admin123'
        const hash = await bcrypt.hash('admin123', 10);
        console.log('✅ Generated bcrypt hash:', hash);

        // Update the admin user password
        const update = await pool.query(
            "UPDATE users SET password = $1 WHERE email = 'admin@carrental.com' RETURNING id, email, role",
            [hash]
        );
        console.log('✅ Updated user:', update.rows[0]);

        // Verify the hash works correctly
        const row = await pool.query("SELECT password FROM users WHERE email = 'admin@carrental.com'");
        const isMatch = await bcrypt.compare('admin123', row.rows[0].password);
        console.log('✅ bcrypt.compare("admin123") =>', isMatch ? 'PASS ✅ - Login will work!' : 'FAIL ❌');

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

fixPassword();
