<?php
session_start();

echo "<h2>Login Debug Information</h2>";

echo "<h3>1. Session Information:</h3>";
echo "<pre>";
echo "Session ID: " . session_id() . "\n";
echo "Session started: " . (session_status() === PHP_SESSION_ACTIVE ? "Yes" : "No") . "\n";
echo "CSRF Token exists: " . (isset($_SESSION['csrf_token']) ? "Yes" : "No") . "\n";
if (isset($_SESSION['csrf_token'])) {
    echo "CSRF Token: " . substr($_SESSION['csrf_token'], 0, 30) . "...\n";
}
echo "Login attempts: " . (isset($_SESSION['login_attempts']) ? $_SESSION['login_attempts'] : "Not set") . "\n";
echo "</pre>";

echo "<h3>2. POST Data (if submitted):</h3>";
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    echo "<pre>";
    echo "Form posted: Yes\n";
    echo "Login button clicked: " . (isset($_POST['login']) ? "Yes" : "No") . "\n";
    echo "Email: " . (isset($_POST['email']) ? $_POST['email'] : "Not set") . "\n";
    echo "Password: " . (isset($_POST['password']) ? "[PROVIDED]" : "Not set") . "\n";
    echo "CSRF Token from POST: " . (isset($_POST['csrf_token']) ? substr($_POST['csrf_token'], 0, 30) . "..." : "Not set") . "\n";

    if (isset($_POST['csrf_token']) && isset($_SESSION['csrf_token'])) {
        echo "Tokens match: " . ($_POST['csrf_token'] === $_SESSION['csrf_token'] ? "YES ✅" : "NO ❌") . "\n";
        if ($_POST['csrf_token'] !== $_SESSION['csrf_token']) {
            echo "\nTOKEN MISMATCH DETAILS:\n";
            echo "Session token: " . $_SESSION['csrf_token'] . "\n";
            echo "POST token: " . $_POST['csrf_token'] . "\n";
        }
    }
    echo "</pre>";
} else {
    echo "<p>No form submission detected</p>";
}

echo "<h3>3. Test Form:</h3>";
if (!isset($_SESSION['csrf_token'])) {
    $_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}
?>

<form method="post" style="border: 1px solid #ccc; padding: 20px; max-width: 400px;">
    <p><strong>Test the login form with debug info:</strong></p>

    <input type="hidden" name="csrf_token"
        value="<?php echo htmlspecialchars($_SESSION['csrf_token'], ENT_QUOTES, 'UTF-8'); ?>">

    <div style="margin-bottom: 15px;">
        <label>Email:</label><br>
        <input type="email" name="email" value="admin@awesomemall.com" style="width: 100%; padding: 8px;">
    </div>

    <div style="margin-bottom: 15px;">
        <label>Password:</label><br>
        <input type="password" name="password" value="Test@123" style="width: 100%; padding: 8px;">
    </div>

    <button type="submit" name="login"
        style="padding: 10px 20px; background: #E8663D; color: white; border: none; cursor: pointer;">
        Submit Test Login
    </button>
</form>

<br>
<p><strong>Current CSRF Token in session:</strong></p>
<input type="text" value="<?php echo htmlspecialchars($_SESSION['csrf_token'], ENT_QUOTES, 'UTF-8'); ?>"
    style="width: 100%; padding: 8px;" readonly>

<br><br>
<a href="reset-login.php"
    style="padding: 10px 20px; background: #333; color: white; text-decoration: none; border-radius: 5px;">Reset
    Session</a>
<a href="login.php"
    style="padding: 10px 20px; background: #E8663D; color: white; text-decoration: none; border-radius: 5px; margin-left: 10px;">Go
    to Login Page</a>
