<?php
// backend-php/public/reset_admin_password.php

// This script resets the admin password to 'admin123'
// Usage: Upload this file to your server and visit /reset_admin_password.php

require_once __DIR__ . '/../src/Utils/Env.php';
require_once __DIR__ . '/../src/Config/Database.php';

use App\Utils\Env;
use App\Config\Database;

// Load Env
Env::load(__DIR__ . '/../../.env');

try {
    $db = Database::getInstance()->getConnection();
    
    $email = 'admin@example.com';
    $newPassword = 'admin123';
    $hash = password_hash($newPassword, PASSWORD_BCRYPT);
    
    $stmt = $db->prepare("UPDATE admin_users SET password_hash = ? WHERE email = ?");
    $result = $stmt->execute([$hash, $email]);
    
    if ($result && $stmt->rowCount() > 0) {
        echo "<h1>Success</h1>";
        echo "<p>Password for <strong>$email</strong> has been reset to: <strong>$newPassword</strong></p>";
        echo "<p>Please delete this file after use.</p>";
    } else {
        echo "<h1>Error</h1>";
        echo "<p>Could not update password. Either the user does not exist or the password is already set to this value.</p>";
        // Try to insert if not exists
        $stmt = $db->prepare("SELECT id FROM admin_users WHERE email = ?");
        $stmt->execute([$email]);
        if (!$stmt->fetch()) {
             $stmt = $db->prepare("INSERT INTO admin_users (email, password_hash, role) VALUES (?, ?, 'superadmin')");
             $stmt->execute([$email, $hash]);
             echo "<p>Created new admin user: $email / $newPassword</p>";
        }
    }
    
} catch (Exception $e) {
    echo "<h1>Error</h1>";
    echo "<p>" . $e->getMessage() . "</p>";
}
