<?php
declare(strict_types=1);
/**
 * test_assign.php — quick debug: hit this to see why room assign fails
 * https://ludoking.gamingserver.win/test_assign.php
 * DELETE after debugging!
 */
require_once __DIR__ . '/db.php';

header('Content-Type: application/json; charset=utf-8');

$result = [];

// 1. DB connection
try {
    $pdo = lk_pdo();
    $result['db'] = 'OK';
} catch (Throwable $e) {
    echo json_encode(['db' => 'FAIL: ' . $e->getMessage()]);
    exit;
}

// 2. users table exists?
try {
    $cnt = (int)$pdo->query("SELECT COUNT(*) FROM users")->fetchColumn();
    $result['users_table'] = 'OK, ' . $cnt . ' users';
} catch (Throwable $e) {
    $result['users_table'] = 'FAIL: ' . $e->getMessage();
}

// 3. TopGamingBD user exists with correct api_key?
$apiKey    = 'XpY2GAXeKJwxSqF87BbPzD68Woy5trj8iKS1PPM';
$apiSecret = '182ae519c6143236c8e071579986195fe94e4d267c4390c767c78d58496a6c34';
try {
    $st = $pdo->prepare("SELECT id, api_key, api_secret FROM users WHERE api_key=? LIMIT 1");
    $st->execute([$apiKey]);
    $user = $st->fetch(PDO::FETCH_ASSOC);
    if (!$user) {
        $result['api_key_check'] = 'FAIL: user with this api_key not found in DB';
    } else {
        $secretMatch = hash_equals((string)$user['api_secret'], $apiSecret);
        $result['api_key_check'] = 'OK, user id=' . $user['id'];
        $result['api_secret_match'] = $secretMatch ? 'OK' : 'FAIL: secret mismatch. DB has: ' . substr((string)$user['api_secret'], -8) . ' (last8)';
    }
} catch (Throwable $e) {
    $result['api_key_check'] = 'FAIL: ' . $e->getMessage();
}

// 4. rooms table and waiting rooms?
try {
    $total   = (int)$pdo->query("SELECT COUNT(*) FROM rooms")->fetchColumn();
    $waiting = (int)$pdo->query("SELECT COUNT(*) FROM rooms WHERE status='waiting' AND is_assigned=0")->fetchColumn();
    $result['rooms'] = "total=$total, available_waiting=$waiting";
    if ($waiting === 0) {
        $result['rooms_warn'] = 'NO available rooms! status or is_assigned might be wrong';
        // show sample rows
        $sample = $pdo->query("SELECT id, room_code, status, is_assigned FROM rooms LIMIT 5")->fetchAll(PDO::FETCH_ASSOC);
        $result['rooms_sample'] = $sample;
    }
} catch (Throwable $e) {
    $result['rooms'] = 'FAIL: ' . $e->getMessage();
}

// 5. Try actual assign
try {
    $pdo->beginTransaction();
    $room = $pdo->query("
        SELECT id, room_code FROM rooms
        WHERE is_assigned=0 AND status='waiting'
        ORDER BY id ASC LIMIT 1 FOR UPDATE
    ")->fetch(PDO::FETCH_ASSOC);
    $pdo->rollBack(); // DON'T actually assign, just test

    if ($room) {
        $result['assign_test'] = 'OK — would assign room_code=' . $room['room_code'] . ' id=' . $room['id'];
    } else {
        $result['assign_test'] = 'FAIL — no room found with is_assigned=0 AND status=waiting';
    }
} catch (Throwable $e) {
    if ($pdo->inTransaction()) $pdo->rollBack();
    $result['assign_test'] = 'FAIL: ' . $e->getMessage();
}

echo json_encode($result, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
