<?php
// Test Login Debug Script
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

include "./core/dbInfo.php";
include_once "./core/dbFunctions.php";

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

// Test password encryption
$test_password = "12345";
$encrypted = abs(crc32(md5($test_password)));
echo "<p><strong>Password '12345' encrypted:</strong> " . $encrypted . "</p>";

// Check if user exists
$email = "testuser@gmail.com";
$mobile = "9999999999";

echo "<h3>Checking User in Database:</h3>";

$sql1 = "SELECT user_id, user_name, email, mobile, password, user_type, kyc_id, kyc_date, is_active FROM users WHERE email = '$email' OR mobile = '$mobile'";
echo "<p><strong>Query:</strong> $sql1</p>";

$result = getXbyY($sql1);
echo "<p><strong>Users Found:</strong> " . count($result) . "</p>";

if(count($result) > 0) {
    echo "<table border='1' cellpadding='5'>";
    echo "<tr><th>Field</th><th>Value</th><th>Status</th></tr>";

    $user = $result[0];

    echo "<tr><td>user_id</td><td>" . $user['user_id'] . "</td><td>OK</td></tr>";
    echo "<tr><td>user_name</td><td>" . $user['user_name'] . "</td><td>OK</td></tr>";
    echo "<tr><td>email</td><td>" . $user['email'] . "</td><td>OK</td></tr>";
    echo "<tr><td>mobile</td><td>" . $user['mobile'] . "</td><td>OK</td></tr>";
    echo "<tr><td>password (DB)</td><td>" . $user['password'] . "</td><td>" . ($user['password'] == $encrypted ? "<span style='color:green'>MATCH</span>" : "<span style='color:red'>MISMATCH - Expected: $encrypted</span>") . "</td></tr>";
    echo "<tr><td>user_type</td><td>" . $user['user_type'] . "</td><td>" . ($user['user_type'] != 'Admin' ? "<span style='color:green'>OK (Not Admin)</span>" : "<span style='color:red'>ISSUE - Admin type</span>") . "</td></tr>";
    echo "<tr><td>is_active</td><td>" . $user['is_active'] . "</td><td>" . ($user['is_active'] == 1 ? "<span style='color:green'>ACTIVE</span>" : "<span style='color:red'>INACTIVE!</span>") . "</td></tr>";
    echo "<tr><td>kyc_id</td><td>" . $user['kyc_id'] . "</td><td>" . ($user['kyc_id'] > 0 ? "<span style='color:green'>KYC Done</span>" : "<span style='color:orange'>KYC Pending - Check kyc_date</span>") . "</td></tr>";
    echo "<tr><td>kyc_date</td><td>" . $user['kyc_date'] . "</td><td>" . (strtotime($user['kyc_date']) > time() ? "<span style='color:green'>Not Expired</span>" : "<span style='color:red'>EXPIRED!</span>") . "</td></tr>";

    echo "</table>";

    // Test exact login query
    echo "<h3>Testing Exact Login Query:</h3>";
    $login_sql = "SELECT user_id, kyc_id, kyc_date FROM users WHERE user_type != 'Admin' AND (email = '$email' OR mobile = '$email') AND password = '$encrypted' AND is_active = 1";
    echo "<p><strong>Query:</strong> $login_sql</p>";

    $login_result = getXbyY($login_sql);
    echo "<p><strong>Result:</strong> " . count($login_result) . " rows</p>";

    if(count($login_result) > 0) {
        echo "<p style='color:green; font-weight:bold;'>LOGIN SHOULD WORK!</p>";
    } else {
        echo "<p style='color:red; font-weight:bold;'>LOGIN WILL FAIL - Query returns 0 rows</p>";

        // Debug each condition
        echo "<h4>Debugging Each Condition:</h4>";

        $q1 = "SELECT user_id FROM users WHERE email = '$email' OR mobile = '$email'";
        $r1 = getXbyY($q1);
        echo "<p>1. Email/Mobile match: " . (count($r1) > 0 ? "<span style='color:green'>YES</span>" : "<span style='color:red'>NO</span>") . "</p>";

        $q2 = "SELECT user_id FROM users WHERE password = '$encrypted'";
        $r2 = getXbyY($q2);
        echo "<p>2. Password match: " . (count($r2) > 0 ? "<span style='color:green'>YES</span>" : "<span style='color:red'>NO</span>") . "</p>";

        $q3 = "SELECT user_id FROM users WHERE is_active = 1 AND (email = '$email' OR mobile = '$mobile')";
        $r3 = getXbyY($q3);
        echo "<p>3. is_active = 1: " . (count($r3) > 0 ? "<span style='color:green'>YES</span>" : "<span style='color:red'>NO</span>") . "</p>";

        $q4 = "SELECT user_id FROM users WHERE user_type != 'Admin' AND (email = '$email' OR mobile = '$mobile')";
        $r4 = getXbyY($q4);
        echo "<p>4. user_type != Admin: " . (count($r4) > 0 ? "<span style='color:green'>YES</span>" : "<span style='color:red'>NO</span>") . "</p>";
    }
} else {
    echo "<p style='color:red; font-weight:bold;'>USER NOT FOUND IN DATABASE!</p>";
}

// Show all users
echo "<h3>All Users in Database:</h3>";
$all_users = "SELECT user_id, user_name, email, mobile, password, user_type, is_active FROM users LIMIT 10";
$all_result = getXbyY($all_users);
echo "<table border='1' cellpadding='5'>";
echo "<tr><th>user_id</th><th>user_name</th><th>email</th><th>mobile</th><th>password</th><th>user_type</th><th>is_active</th></tr>";
foreach($all_result as $u) {
    echo "<tr>";
    echo "<td>" . $u['user_id'] . "</td>";
    echo "<td>" . $u['user_name'] . "</td>";
    echo "<td>" . $u['email'] . "</td>";
    echo "<td>" . $u['mobile'] . "</td>";
    echo "<td>" . $u['password'] . "</td>";
    echo "<td>" . $u['user_type'] . "</td>";
    echo "<td>" . $u['is_active'] . "</td>";
    echo "</tr>";
}
echo "</table>";

?>
