<?php

require_once __DIR__ . '/../core/cron_guard.php';
icylab_require_cron_access();

ini_set('display_errors', '0');
ini_set('display_startup_errors', '0');
error_reporting(E_ALL);

require_once __DIR__ . '/../services/analytics/AnalyticsEngine.php';
if (PHP_SAPI !== 'cli') header('Content-Type: text/plain; charset=utf-8');

try {
    $engine = new AnalyticsEngine();
    $engine->rebuild();
    echo "OK ANALYTICS" . PHP_EOL;
    echo "Analytics Engine reconstruido correctamente." . PHP_EOL;
} catch (Throwable $e) {
    error_log('rebuild_analytics error: ' . $e->getMessage());
    if (PHP_SAPI !== 'cli') http_response_code(500);
    echo "ERROR ANALYTICS" . PHP_EOL;
    echo "La reconstruccion no pudo completarse. Revisa el log del servidor." . PHP_EOL;
}
