<?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 "Fixing admin@uol.edu.pk role issue...\n\n";

$email = "admin@uol.edu.pk";

// Check current user data
echo "=== Current User Data ===\n";
$check = $conn->query("SELECT id, email, role, full_name FROM users WHERE email = '$email'");
if($check->num_rows > 0) {
    $row = $check->fetch_assoc();
    echo "ID: {$row['id']}\n";
    echo "Email: {$row['email']}\n";
    echo "Role: '" . $row['role'] . "' (empty)\n";
    echo "Full Name: {$row['full_name']}\n";
} else {
    echo "User not found!\n";
    exit();
}

// Fix the role
echo "\n=== Fixing Role ===\n";
$sql = "UPDATE users SET role = 'system_admin' WHERE email = '$email'";
if ($conn->query($sql) === TRUE) {
    echo "Role updated to 'system_admin'\n";
} else {
    echo "Error updating role: " . $conn->error . "\n";
}

// Verify the fix
echo "\n=== Verification ===\n";
$verify = $conn->query("SELECT id, email, role, full_name FROM users WHERE email = '$email'");
if($verify->num_rows > 0) {
    $row = $verify->fetch_assoc();
    echo "ID: {$row['id']}\n";
    echo "Email: {$row['email']}\n";
    echo "Role: '{$row['role']}'\n";
    echo "Full Name: {$row['full_name']}\n";
    
    // Test login query
    echo "\n=== Testing Login Query ===\n";
    $test_sql = "SELECT id, full_name, password, role FROM users WHERE email = '$email' AND role IN ('admin', 'system_admin')";
    $test_result = $conn->query($test_sql);
    echo "Query: $test_sql\n";
    echo "Results found: " . $test_result->num_rows . "\n";
    
    if ($test_result->num_rows == 1) {
        echo "SUCCESS: Login query will find this user\n";
    } else {
        echo "FAILED: Login query still won't find this user\n";
    }
}

$conn->close();
?>
