<?php
// Connect to the database
require 'includes/db.php'; 

// --- CREDENTIALS CONFIGURATION ---
$new_username = "RushikeshLokhande";
$new_password = "Rushi@7057830490";
// ---------------------------------

try {
    // 1. Encrypt the password using Bcrypt (Standard Security)
    $hashed_password = password_hash($new_password, PASSWORD_DEFAULT);

    // 2. Check if this username already exists
    $stmt = $pdo->prepare("SELECT id FROM admins WHERE username = ?");
    $stmt->execute([$new_username]);
    $user = $stmt->fetch();

    if ($user) {
        // A. If user exists -> UPDATE the password
        $update = $pdo->prepare("UPDATE admins SET password = ? WHERE username = ?");
        $update->execute([$hashed_password, $new_username]);
        echo "<div style='color: green; font-family: sans-serif; padding: 20px;'>";
        echo "<h1>✅ Success!</h1>";
        echo "<p>Password updated for existing user: <strong>$new_username</strong></p>";
    } else {
        // B. If user does not exist -> INSERT new user
        $insert = $pdo->prepare("INSERT INTO admins (username, password) VALUES (?, ?)");
        $insert->execute([$new_username, $hashed_password]);
        echo "<div style='color: green; font-family: sans-serif; padding: 20px;'>";
        echo "<h1>✅ Success!</h1>";
        echo "<p>Created new admin user: <strong>$new_username</strong></p>";
    }

    echo "<p>You can now login at <a href='/admin'>/admin</a></p>";
    echo "<p style='color: red; font-weight: bold;'>⚠️ IMPORTANT: Delete this file (reset_admin.php) from your server now.</p>";
    echo "</div>";

} catch (PDOException $e) {
    echo "<div style='color: red; font-family: sans-serif; padding: 20px;'>";
    echo "<h1>❌ Error</h1>";
    echo "<p>" . $e->getMessage() . "</p>";
    echo "</div>";
}
?>