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

const 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,
});

(async () => {
	const client = await pool.connect();
	try {
		const { rows } = await client.query(`
			SELECT u.email, u.status, u.username,
			       ua.password_hash,
			       ua.is_oauth_only,
			       ua.locked_until,
			       ua.login_attempts
			FROM users u
			JOIN user_auth ua ON ua.user_id = u.id
			WHERE u.email = 'admin@vietprodev.com'
			   OR u.email = 'admin@sso.vietprodev.com'
			ORDER BY u.email;
		`);
		if (rows.length === 0) {
			console.log('No admin user found!');
		} else {
			console.log('Admin user(s):');
			for (const row of rows) {
				console.log(`  email: ${row.email}`);
				console.log(`  status: ${row.status}`);
				console.log(`  username: ${row.username}`);
				console.log(`  password_hash: ${row.password_hash}`);
				console.log(`  is_oauth_only: ${row.is_oauth_only}`);
				console.log(`  locked_until: ${row.locked_until}`);
				console.log(`  login_attempts: ${row.login_attempts}`);
				console.log('---');
			}
		}
	} finally {
		client.release();
		await pool.end();
	}
})();
