<?php
// File: password_reset.php
// This is a one-time use tool to securely reset the main admin password.
// Place this file in your main project directory and run it once.
// **DELETE THIS FILE AFTER USE.**

require_once 'db.php';

echo "<!DOCTYPE html><html lang='en'><head><meta charset='UTF-8'><title>Admin Password Reset</title>";
echo "<style>
        body { font-family: sans-serif; background-color: #f4f4f4; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; }
        .container { background-color: #fff; padding: 30px; border-radius: 8px; box-shadow: 0 4px 8px rgba(0,0,0,0.1); text-align: center; }
        .success { color: #28a745; border: 1px solid #c3e6cb; background-color: #d4edda; padding: 15px; border-radius: 5px; }
        .error { color: #721c24; border: 1px solid #f5c6cb; background-color: #f8d7da; padding: 15px; border-radius: 5px; }
        code { background-color: #e9ecef; padding: 2px 4px; border-radius: 3px; }
      </style>";
echo "</head><body><div class='container'>";

// The new password to set
$new_password = 'password123';
// Encrypt the new password using the modern PASSWORD_DEFAULT algorithm
$hashed_password = password_hash($new_password, PASSWORD_DEFAULT);
// The ID of the admin user to update (almost always 1)
$admin_id = 1;

// Use a prepared statement to prevent any chance of SQL injection
$stmt = $conn->prepare("UPDATE users SET password = ? WHERE id = ?");

if ($stmt === false) {
    echo "<div class='error'><strong>Error preparing statement:</strong> " . htmlspecialchars($conn->error) . "</div>";
} else {
    // Bind the parameters (s for string, i for integer)
    $stmt->bind_param("si", $hashed_password, $admin_id);

    // Execute the statement and check for success
    if ($stmt->execute()) {
        if ($stmt->affected_rows > 0) {
            echo "<h1>Password Reset Successful!</h1>";
            echo "<div class='success'>";
            echo "The password for the user with ID <code>" . $admin_id . "</code> has been successfully reset.<br>";
            echo "You can now log in with:<br><br>";
            echo "<strong>Username:</strong> <code>admin</code><br>";
            echo "<strong>Password:</strong> <code>" . htmlspecialchars($new_password) . "</code><br><br>";
            echo "<strong style='color: #dc3545;'>IMPORTANT: For security, delete this file (<code>password_reset.php</code>) from your server immediately!</strong>";
            echo "</div>";
        } else {
            echo "<h1>Password Reset Failed</h1>";
            echo "<div class='error'><strong>No user found with ID <code>" . $admin_id . "</code>.</strong><br>Please check your <code>users</code> table in phpMyAdmin to ensure the main admin account has an ID of 1.</div>";
        }
    } else {
        echo "<h1>Password Reset Failed</h1>";
        echo "<div class='error'><strong>Error executing statement:</strong> " . htmlspecialchars($stmt->error) . "</div>";
    }

    // Close the statement
    $stmt->close();
}

// Close the connection
$conn->close();

echo "</div></body></html>";
?>

