<?php
/**
 * Update all user passwords
 * DELETE AFTER USE!
 */

require_once dirname(__DIR__) . '/config/config.php';
require_once dirname(__DIR__) . '/config/database.php';

echo "<h1>Updating User Passwords</h1>";

try {
    $pdo = get_db_connection();
    
    // List of users to update
    $users = [
        ['superadmin', 'superadmin@ruhondo.com', 'Super', 'Admin'],
        ['admin', 'admin@ruhondo.com', 'System', 'Administrator'],
        ['fdmanager', 'fdmanager@ruhondo.com', 'Front', 'Desk Manager'],
        ['fdagent1', 'fdagent1@ruhondo.com', 'John', 'Doe'],
        ['fdagent2', 'fdagent2@ruhondo.com', 'Jane', 'Smith'],
        ['posmanager', 'posmanager@ruhondo.com', 'POS', 'Manager'],
        ['cashier1', 'cashier1@ruhondo.com', 'Peter', 'Cashier'],
        ['cashier2', 'cashier2@ruhondo.com', 'Mary', 'Johnson']
    ];
    
    $password = 'Admin@123';
    $hash = password_hash($password, PASSWORD_DEFAULT, ['cost' => 12]);
    
    echo "Password hash for '$password': <code>$hash</code><br><br>";
    
    $stmt = $pdo->prepare("UPDATE users SET password_hash = ? WHERE username = ?");
    
    foreach ($users as $user) {
        $stmt->execute([$hash, $user[0]]);
        if ($stmt->rowCount() > 0) {
            echo "<p style='color: green'>✓ Updated: {$user[0]}</p>";
        } else {
            // User doesn't exist, insert them
            $insert = $pdo->prepare("INSERT INTO users (username, email, password_hash, first_name, last_name, role_id, is_active) 
                                     VALUES (?, ?, ?, ?, ?, ?, 1)");
            $insert->execute([$user[0], $user[1], $hash, $user[2], $user[3], 1]);
            echo "<p style='color: orange'>✓ Created: {$user[0]}</p>";
        }
    }
    
    echo "<hr>";
    echo "<p><strong>All users updated with password: $password</strong></p>";
    echo "<p><a href='login.php'>Go to Login</a></p>";
    
} catch (Exception $e) {
    echo "<p style='color: red'>Error: " . $e->getMessage() . "</p>";
}