<?php
/**
 * Reset Super Admin Password
 * Access this file in your browser to reset the superadmin password
 */

require_once __DIR__ . '/config/app.php';
require_once __DIR__ . '/core/Database.php';
require_once __DIR__ . '/core/Security.php';

// Check if form was submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $newPassword = $_POST['password'] ?? '';
    
    if (strlen($newPassword) < 8) {
        $error = "Password must be at least 8 characters long";
    } else {
        // Generate new password hash
        $hash = Security::hashPassword($newPassword);
        
        // Update or insert superadmin
        $stmt = db()->prepare("SELECT id FROM superadmins WHERE email = ?");
        $stmt->execute(['superadmin@tellerpro.com']);
        $admin = $stmt->fetch();
        
        if ($admin) {
            $stmt = db()->prepare("UPDATE superadmins SET password = ?, is_active = 1 WHERE id = ?");
            $stmt->execute([$hash, $admin['id']]);
            $success = "Password updated successfully!";
        } else {
            $stmt = db()->prepare("INSERT INTO superadmins (name, email, password, is_active) VALUES (?, ?, ?, 1)");
            $stmt->execute(['Platform Admin', 'superadmin@tellerpro.com', $hash]);
            $success = "Superadmin user created successfully!";
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Reset Super Admin Password</title>
    <style>
        body { font-family: Arial, sans-serif; max-width: 500px; margin: 50px auto; padding: 20px; background: #f5f5f5; }
        .container { background: white; padding: 30px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
        h1 { margin-top: 0; color: #333; }
        .form-group { margin-bottom: 20px; }
        label { display: block; margin-bottom: 5px; font-weight: bold; }
        input { width: 100%; padding: 10px; border: 1px solid #ddd; border-radius: 4px; box-sizing: border-box; }
        button { background: #007bff; color: white; padding: 12px 20px; border: none; border-radius: 4px; cursor: pointer; font-size: 16px; }
        button:hover { background: #0056b3; }
        .success { background: #d4edda; color: #155724; padding: 10px; border-radius: 4px; margin-bottom: 20px; }
        .error { background: #f8d7da; color: #721c24; padding: 10px; border-radius: 4px; margin-bottom: 20px; }
        .info { background: #d1ecf1; color: #0c5460; padding: 10px; border-radius: 4px; margin-bottom: 20px; }
    </style>
</head>
<body>
    <div class="container">
        <h1>Reset Super Admin Password</h1>
        
        <?php if (isset($success)): ?>
            <div class="success"><?= htmlspecialchars($success) ?></div>
        <?php endif; ?>
        
        <?php if (isset($error)): ?>
            <div class="error"><?= htmlspecialchars($error) ?></div>
        <?php endif; ?>
        
        <div class="info">
            After resetting, login at: <strong><?= APP_URL ?>/superadmin/login</strong><br>
            Email: <strong>superadmin@tellerpro.com</strong>
        </div>
        
        <form method="POST">
            <div class="form-group">
                <label for="password">New Password (min 8 characters):</label>
                <input type="password" id="password" name="password" required minlength="8">
            </div>
            <button type="submit">Reset Password</button>
        </form>
    </div>
</body>
</html>
