<?php
/** User-safe status page. Does not expose secrets. */
error_reporting(0);
define('BASE_PATH', dirname(__DIR__));
require_once BASE_PATH . '/src/config.php';
header('Content-Type: application/json; charset=utf-8');
$out = ['ok'=>true, 'service'=>'GEM ACC Bot', 'time'=>date('c'), 'checks'=>[]];
try {
    $pdo = db();
    $pdo->query('SELECT 1');
    $out['checks']['database'] = 'ok';
    try {
        $pending = (int)$pdo->query("SELECT COUNT(*) FROM telegram_update_queue WHERE status IN ('PENDING','RETRY','RUNNING')")->fetchColumn();
        $failed = (int)$pdo->query("SELECT COUNT(*) FROM telegram_update_queue WHERE status='FAILED'")->fetchColumn();
        $out['checks']['telegram_queue'] = ['pending'=>$pending, 'failed'=>$failed];
    } catch (Throwable $ignored) {
        $out['checks']['telegram_queue'] = 'not_ready';
    }
} catch (Throwable $e) {
    $out['ok'] = false;
    $out['checks']['database'] = 'failed';
}
echo json_encode($out, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
