<?php
/** Clean expired cache entries */
require_once dirname(__DIR__) . '/config.php';
try {
    $st = db()->prepare("DELETE FROM ai_cache WHERE expires_at < NOW()");
    $st->execute();
    echo date('Y-m-d H:i:s') . " Cache cleaned: " . $st->rowCount() . " entries removed\n";
    // Also clean old system metrics (older than 7 days)
    db()->exec("DELETE FROM system_metrics WHERE metric_minute < NOW() - INTERVAL 7 DAY");
    // Auto-clear old context memory (older than 24h)
    db()->exec("DELETE FROM context_memory WHERE created_at < NOW() - INTERVAL 24 HOUR");
    echo "Done\n";
} catch (Throwable $e) { echo "ERROR: ".$e->getMessage()."\n"; }
