<?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 login credentials...\n\n";

// Create/update admin@uole.du.pk account
$email = "admin@uole.du.pk";
$full_name = "System Administrator";
$password = "admin123";
$role = "system_admin";

// Check if user exists
$check = $conn->query("SELECT id FROM users WHERE email = '$email'");
if($check->num_rows == 0) {
    echo "Creating admin account with email: $email\n";
    $hashed_password = password_hash($password, PASSWORD_DEFAULT);
    $sql = "INSERT INTO users (full_name, email, password, role, cnic, phone) 
            VALUES ('$full_name', '$email', '$hashed_password', '$role', '1111111111111', '11111111111')";
    
    if ($conn->query($sql) === TRUE) {
        echo "Admin account created successfully.\n";
        echo "Email: $email\n";
        echo "Password: $password\n";
        echo "Role: $role\n";
    } else {
        echo "Error creating admin account: " . $conn->error . "\n";
    }
} else {
    echo "Admin account already exists with email: $email\n";
    echo "Updating password...\n";
    $hashed_password = password_hash($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";
    } else {
        echo "Error updating admin account: " . $conn->error . "\n";
    }
}

// Also create/update in admins table
$check_admins = $conn->query("SELECT id FROM admins WHERE email = '$email'");
if($check_admins->num_rows == 0) {
    echo "\nCreating admin record in admins table...\n";
    $sql_admins = "INSERT INTO admins (username, email, password, role) 
                    VALUES ('admin_uole', '$email', '$hashed_password', '$role')";
    
    if ($conn->query($sql_admins) === TRUE) {
        echo "Admin record created in admins table.\n";
    } else {
        echo "Error creating admin record: " . $conn->error . "\n";
    }
} else {
    echo "\nUpdating admin record in admins table...\n";
    $sql_admins = "UPDATE admins SET password = '$hashed_password', role = '$role' WHERE email = '$email'";
    
    if ($conn->query($sql_admins) === TRUE) {
        echo "Admin record updated in admins table.\n";
    } else {
        echo "Error updating admin record: " . $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
    if(password_verify($password, $row['password'] ?? '')) {
        echo "Password verification: SUCCESS\n";
    } else {
        echo "Password verification: FAILED\n";
    }
}

$conn->close();
?>
