<?php
require_once __DIR__ . '/../auth.php';
if (function_exists('require_login')) require_login('login.php');
require_once __DIR__ . '/../db_bootstrap.php';
require_once __DIR__ . '/../ua_unified_stats_lib.php';
require_once __DIR__ . '/../stats_snapshot_lib.php';
if (session_status() === PHP_SESSION_ACTIVE) @session_write_close();
header('Content-Type: application/json; charset=utf-8');
if (function_exists('ensure_db')) ensure_db(true);
$conn=$GLOBALS['conn']??null;
if(!($conn instanceof mysqli)){ echo json_encode(['ok'=>false,'message'=>'db_not_ready'], JSON_UNESCAPED_UNICODE); exit; }
echo json_encode(ua_app_stats_snapshot($conn, 10, ((string)($_GET['force'] ?? '') === '1')), JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);