<?php
/**
 * سودانا v23 — Public Platform Stats
 */
require_once __DIR__ . '/../../config.php';
require_once __DIR__ . '/../../engine/Kernel.php';
Kernel::handle(['auth' => false, 'public' => true]);
require_once __DIR__ . '/../../engine/Logger.php';
header('Access-Control-Allow-Origin: *');
header('Cache-Control: public, max-age=60');

$stats = ['status'=>'operational','version'=>'v23','timestamp'=>date('c'),
          'requests_today'=>0,'new_users_week'=>0,'active_flows'=>0,'queue_depth'=>0];

$queries = [
    'requests_today' => "SELECT COUNT(*) FROM ai_requests WHERE DATE(created_at)=CURDATE()",
    'new_users_week' => "SELECT COUNT(*) FROM users WHERE created_at>=DATE_SUB(NOW(),INTERVAL 7 DAY)",
    'active_flows'   => "SELECT COUNT(*) FROM flow_sessions WHERE status='active'",
    'queue_depth'    => "SELECT COUNT(*) FROM tasks WHERE status='pending'",
];
foreach ($queries as $key => $sql) {
    try { $stats[$key] = (int)db()->query($sql)->fetchColumn(); } catch (Throwable $e) {}
}
json_response($stats);
