<?php
// Database Connection Test
error_reporting(E_ALL);
ini_set('display_errors', 1);

echo "<h2>Database Connection Test</h2>";

// Test 1: Check if config file loads
echo "<h3>1. Testing Config File...</h3>";
if (file_exists('includes/config.php')) {
    echo "✅ config.php exists<br>";
    require_once('includes/config.php');
    echo "✅ config.php loaded<br>";
} else {
    echo "❌ config.php not found<br>";
    die();
}

// Test 2: Check database connection
echo "<h3>2. Testing Database Connection...</h3>";
if ($con) {
    echo "✅ Database connected successfully<br>";
    echo "Database: " . DB_NAME . "<br>";
} else {
    echo "❌ Database connection failed: " . mysqli_connect_error() . "<br>";
    die();
}

// Test 3: Check if users table exists
echo "<h3>3. Checking Users Table...</h3>";
$check_table = mysqli_query($con, "SHOW TABLES LIKE 'users'");
if (mysqli_num_rows($check_table) > 0) {
    echo "✅ users table exists<br>";
} else {
    echo "❌ users table does NOT exist<br>";
    echo "Please create the users table first<br>";
}

// Test 4: Check table structure
echo "<h3>4. Users Table Structure:</h3>";
$structure = mysqli_query($con, "DESCRIBE users");
if ($structure) {
    echo "<table border='1' cellpadding='5'>";
    echo "<tr><th>Field</th><th>Type</th><th>Null</th><th>Key</th></tr>";
    while ($row = mysqli_fetch_assoc($structure)) {
        echo "<tr>";
        echo "<td>{$row['Field']}</td>";
        echo "<td>{$row['Type']}</td>";
        echo "<td>{$row['Null']}</td>";
        echo "<td>{$row['Key']}</td>";
        echo "</tr>";
    }
    echo "</table>";
} else {
    echo "❌ Could not retrieve table structure<br>";
}

// Test 5: Count users
echo "<h3>5. Checking Users in Database:</h3>";
$count = mysqli_query($con, "SELECT COUNT(*) as total FROM users");
if ($count) {
    $row = mysqli_fetch_assoc($count);
    echo "Total users: <strong>{$row['total']}</strong><br>";
} else {
    echo "❌ Could not count users: " . mysqli_error($con) . "<br>";
}

// Test 6: List all users
echo "<h3>6. All Users:</h3>";
$users = mysqli_query($con, "SELECT id, name, email, contactno FROM users");
if ($users && mysqli_num_rows($users) > 0) {
    echo "<table border='1' cellpadding='5'>";
    echo "<tr><th>ID</th><th>Name</th><th>Email</th><th>Contact</th></tr>";
    while ($user = mysqli_fetch_assoc($users)) {
        echo "<tr>";
        echo "<td>{$user['id']}</td>";
        echo "<td>{$user['name']}</td>";
        echo "<td>{$user['email']}</td>";
        echo "<td>{$user['contactno']}</td>";
        echo "</tr>";
    }
    echo "</table>";
} else {
    echo "⚠️ No users found in database<br>";
    echo "You need to insert users first!<br>";
}

// Test 7: Check password hash for a specific user
echo "<h3>7. Password Check for admin@awesomemall.com:</h3>";
$stmt = $con->prepare("SELECT id, email, password FROM users WHERE email = ?");
$email = 'admin@awesomemall.com';
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows > 0) {
    $user = $result->fetch_assoc();
    echo "✅ User found!<br>";
    echo "Email: {$user['email']}<br>";
    echo "Password hash length: " . strlen($user['password']) . " characters<br>";
    echo "Password hash type: " . (strlen($user['password']) === 32 ? 'MD5 (old)' : 'Bcrypt (secure)') . "<br>";
    echo "First 20 chars of hash: " . substr($user['password'], 0, 20) . "...<br>";
} else {
    echo "❌ User admin@awesomemall.com not found<br>";
}
$stmt->close();

// Test 8: Test login with hardcoded credentials
echo "<h3>8. Testing Login Logic:</h3>";
$test_email = 'admin@awesomemall.com';
$test_password = 'Test@123';

$stmt = $con->prepare("SELECT id, name, email, password FROM users WHERE email = ?");
$stmt->bind_param("s", $test_email);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows === 1) {
    $user = $result->fetch_assoc();
    echo "Found user: {$user['name']}<br>";

    // Check password
    if (strlen($user['password']) === 32) {
        // MD5
        $md5_test = md5($test_password);
        if ($md5_test === $user['password']) {
            echo "✅ MD5 Password match!<br>";
        } else {
            echo "❌ MD5 Password does NOT match<br>";
            echo "Expected: $md5_test<br>";
            echo "Got: {$user['password']}<br>";
        }
    } else {
        // Bcrypt
        if (password_verify($test_password, $user['password'])) {
            echo "✅ Bcrypt Password match!<br>";
        } else {
            echo "❌ Bcrypt Password does NOT match<br>";
        }
    }
} else {
    echo "❌ User not found for login test<br>";
}
$stmt->close();

echo "<hr>";
echo "<h3>Summary:</h3>";
echo "If all tests pass, the database is working correctly.<br>";
echo "If login still fails, check:<br>";
echo "1. PHP error logs<br>";
echo "2. Browser console for JavaScript errors<br>";
echo "3. Session configuration<br>";
echo "4. CSRF token generation<br>";
?>
