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

async function main() {
  try {
    const users = await pool.query('SELECT id, username, role FROM users');
    console.log('Current users in DB:', users.rows);
    
    const hash = await bcrypt.hash('admin123', 10);
    const existing = users.rows.find(u => u.username === 'admin');
    
    if (!existing) {
      await pool.query(
        'INSERT INTO users (username, password_hash, role) VALUES ($1, $2, $3)',
        ['admin', hash, 'owner']
      );
      console.log('Created user "admin" with password "admin123" (role: owner)');
    } else {
      await pool.query(
        'UPDATE users SET password_hash = $1 WHERE username = $2',
        [hash, 'admin']
      );
      console.log('Successfully updated password for user "admin" to "admin123"');
    }
  } catch (err) {
    console.error('Error resetting password:', err);
  } finally {
    await pool.end();
    process.exit(0);
  }
}

main();
