<?php
declare(strict_types=1);

require_once __DIR__ . '/bootstrap.php';
require_once __DIR__ . '/db.php';
require_once __DIR__ . '/special_odds_engine.php';

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

$token = (string) ($_GET['token'] ?? '');
$expected = defined('SPECIAL_ODDS_CRON_TOKEN') ? (string) SPECIAL_ODDS_CRON_TOKEN : '';
if ($expected === '' || !hash_equals($expected, $token)) {
    http_response_code(404);
    echo json_encode(['ok' => false, 'error' => 'not_found'], JSON_UNESCAPED_UNICODE);
    exit;
}

try {
    $pdo = mocker_db();
} catch (Throwable $e) {
    http_response_code(503);
    echo json_encode(['ok' => false, 'error' => 'db'], JSON_UNESCAPED_UNICODE);
    exit;
}

$r = special_odds_sync_all_open($pdo);
echo json_encode(
    [
        'ok' => true,
        'checked' => $r['checked'],
        'settled' => $r['settled'],
        'errors' => $r['errors'],
    ],
    JSON_UNESCAPED_UNICODE
);
