<?php
/**
 * Login Debug Script
 * Run this file to debug login issues
 * Access: http://yourdomain.com/hms/public/debug-login.php
 */

// Initialize system
require_once dirname(__DIR__) . '/config/config.php';
require_once dirname(__DIR__) . '/config/database.php';
require_once dirname(__DIR__) . '/config/helpers.php';

echo "<h1>Login Debug Tool</h1>";

// Test database connection
try {
    $pdo = get_db_connection();
    echo "<p style='color: green'>✓ Database connection successful</p>";
} catch (Exception $e) {
    echo "<p style='color: red'>✗ Database connection failed: " . $e->getMessage() . "</p>";
    exit;
}

// Check if users table exists
$tables = $pdo->query("SHOW TABLES LIKE 'users'")->fetchAll();
if (count($tables) > 0) {
    echo "<p style='color: green'>✓ Users table exists</p>";
} else {
    echo "<p style='color: red'>✗ Users table not found! Did you import the database?</p>";
    exit;
}

// Count users
$user_count = $pdo->query("SELECT COUNT(*) FROM users")->fetchColumn();
echo "<p>Total users in database: <strong>$user_count</strong></p>";

// Check admin user
$admin = $pdo->prepare("SELECT * FROM users WHERE username = ? OR email = ?");
$admin->execute(['admin', 'admin']);
$admin_user = $admin->fetch(PDO::FETCH_ASSOC);

if ($admin_user) {
    echo "<p style='color: green'>✓ Admin user found</p>";
    echo "<pre>";
    echo "User ID: " . $admin_user['user_id'] . "\n";
    echo "Username: " . $admin_user['username'] . "\n";
    echo "Email: " . $admin_user['email'] . "\n";
    echo "First Name: " . $admin_user['first_name'] . "\n";
    echo "Last Name: " . $admin_user['last_name'] . "\n";
    echo "Password Hash: " . substr($admin_user['password_hash'], 0, 30) . "...\n";
    echo "Active: " . ($admin_user['is_active'] ? 'Yes' : 'No') . "\n";
    echo "</pre>";
    
    // Test password verification
    $test_password = 'Admin@123';
    if (password_verify($test_password, $admin_user['password_hash'])) {
        echo "<p style='color: green'>✓ Password verification successful for 'Admin@123'</p>";
    } else {
        echo "<p style='color: red'>✗ Password verification FAILED for 'Admin@123'</p>";
        
        // Generate new hash for testing
        $new_hash = password_hash($test_password, PASSWORD_DEFAULT);
        echo "<p>New hash for 'Admin@123': <code>$new_hash</code></p>";
        echo "<p>Copy this hash and update the database if needed.</p>";
    }
} else {
    echo "<p style='color: red'>✗ Admin user not found!</p>";
}

// Check all users
echo "<h2>All Users</h2>";
$all_users = $pdo->query("SELECT user_id, username, email, is_active FROM users")->fetchAll();
echo "<table border='1' cellpadding='5'>";
echo "<tr><th>ID</th><th>Username</th><th>Email</th><th>Active</th></tr>";
foreach ($all_users as $user) {
    echo "<tr>";
    echo "<td>" . $user['user_id'] . "</td>";
    echo "<td>" . $user['username'] . "</td>";
    echo "<td>" . $user['email'] . "</td>";
    echo "<td>" . ($user['is_active'] ? 'Yes' : 'No') . "</td>";
    echo "</tr>";
}
echo "</table>";

// Check session configuration
echo "<h2>Session Configuration</h2>";
echo "<pre>";
echo "Session save path: " . session_save_path() . "\n";
echo "Session name: " . session_name() . "\n";
echo "Session status: " . session_status() . "\n";
echo "Session cookie params: \n";
print_r(session_get_cookie_params());
echo "</pre>";