
import { pool } from './db.js';
import bcrypt from 'bcrypt';
import dotenv from 'dotenv';
dotenv.config();

const PASSWORD_PEPPER = process.env.PASSWORD_PEPPER || '';

async function verify() {
    console.log('🔍 Verifying SuperAdmin credentials...');
    console.log(`🌶️  Using Pepper: "${PASSWORD_PEPPER}"`);

    try {
        const res = await pool.query("SELECT password_hash FROM users WHERE identifier = 'superadmin'");
        if (res.rows.length === 0) {
            console.error('❌ SuperAdmin user not found in DB!');
            return;
        }

        const hash = res.rows[0].password_hash;
        const password = 'Admin@2025!';
        const passwordWithPepper = password + PASSWORD_PEPPER;

        const match = await bcrypt.compare(passwordWithPepper, hash);

        if (match) {
            console.log('✅ SUCCESS: Database hash matches (Password + Pepper)');
            console.log('👉 If login fails on the server, the server likely has the WRONG pepper or NO pepper.');
        } else {
            console.error('❌ FAILURE: Database hash does NOT match local logic.');

            // Try without pepper
            const matchNoPepper = await bcrypt.compare(password, hash);
            if (matchNoPepper) {
                console.warn('⚠️  WARNING: Hash matches password WITHOUT pepper. The seeder ignored the pepper.');
            } else {
                console.error('❌ Hash matches neither.');
            }
        }
    } catch (err) {
        console.error('Error:', err);
    } finally {
        await pool.end();
    }
}

verify();
