<?php
$host = "localhost";
$user = "root";
$password = "";
$dbname = "admission_system";

$conn = new mysqli($host, $user, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

echo "Updating admin@uol.edu.pk password to admin@123...\n\n";

// Update admin@uol.edu.pk account password
$email = "admin@uol.edu.pk";
$new_password = "admin@123";
$role = "system_admin";

// Check if user exists
$check = $conn->query("SELECT id, role, password FROM users WHERE email = '$email'");
if($check->num_rows > 0) {
    echo "Found existing admin account with email: $email\n";
    $row = $check->fetch_assoc();
    echo "Current role: {$row['role']}\n";
    
    // Update password and role
    $hashed_password = password_hash($new_password, PASSWORD_DEFAULT);
    $sql = "UPDATE users SET password = '$hashed_password', role = '$role' WHERE email = '$email'";
    
    if ($conn->query($sql) === TRUE) {
        echo "Password and role updated successfully.\n";
        echo "New password: $new_password\n";
        echo "Role: $role\n";
    } else {
        echo "Error updating admin account: " . $conn->error . "\n";
    }
} else {
    echo "Admin account not found with email: $email\n";
    echo "Creating new admin account...\n";
    $hashed_password = password_hash($new_password, PASSWORD_DEFAULT);
    $sql = "INSERT INTO users (full_name, email, password, role, cnic, phone) 
            VALUES ('System Administrator', '$email', '$hashed_password', '$role', '3333333333333', '33333333333')";
    
    if ($conn->query($sql) === TRUE) {
        echo "Admin account created successfully.\n";
        echo "Email: $email\n";
        echo "Password: $new_password\n";
        echo "Role: $role\n";
    } else {
        echo "Error creating admin account: " . $conn->error . "\n";
    }
}

// Verify the account
echo "\n=== Verification ===\n";
$result = $conn->query("SELECT email, role FROM users WHERE email = '$email'");
if($result->num_rows > 0) {
    $row = $result->fetch_assoc();
    echo "Account verified:\n";
    echo "Email: {$row['email']}\n";
    echo "Role: {$row['role']}\n";
    
    // Test password
    $verify_result = $conn->query("SELECT password FROM users WHERE email = '$email'");
    $pass_row = $verify_result->fetch_assoc();
    if(password_verify($new_password, $pass_row['password'])) {
        echo "Password verification: SUCCESS\n";
    } else {
        echo "Password verification: FAILED\n";
    }
    
    echo "\nUpdated login credentials:\n";
    echo "Email: admin@uol.edu.pk\n";
    echo "Password: admin@123\n";
    echo "Role: system_admin\n";
} else {
    echo "Account not found!\n";
}

$conn->close();
?>
