require('dotenv').config({ path: __dirname + '/../../.env' });
const bcrypt = require('bcryptjs');

const pool = require('pg').Pool;

const db_pool = new pool({
	host: process.env.DB_HOST || 'localhost',
	port: process.env.DB_PORT || 5432,
	database: process.env.DB_NAME || 'vietprodev_sso',
	user: process.env.DB_USER || 'postgres',
	password: process.env.DB_PASSWORD,
});

const TEST_PASSWORDS = [
	'Vietpro@2026',
	'VietPro@2026',
	'Vietpro@123',
	'VietPro@123',
	'Admin@123',
	'Admin@2026',
	'Admin123!',
];

(async () => {
	const client = await db_pool.connect();
	try {
		const { rows } = await client.query(`
			SELECT u.email, u.status, ua.password_hash
			FROM users u
			JOIN user_auth ua ON ua.user_id = u.id
			WHERE u.email IN ('admin@vietprodev.com', 'admin@sso.vietprodev.com')
			ORDER BY u.email;
		`);

		for (const user of rows) {
			console.log(`\n=== ${user.email} ===`);
			console.log(`Status: ${user.status}`);
			console.log(`Hash: ${user.password_hash}`);

			let matched = false;
			for (const pwd of TEST_PASSWORDS) {
				const result = await bcrypt.compare(pwd, user.password_hash);
				if (result) {
					console.log(`✅ MATCHES: "${pwd}"`);
					matched = true;
					break;
				}
			}
			if (!matched) {
				console.log('❌ No password match found among test list');
			}
		}
	} finally {
		client.release();
		await db_pool.end();
	}
})();
