<?php
/**
 * Cleanup Expired Sessions - تنظيف الجلسات المنتهية
 * للتشغيل من Cron: 0 * * * * php /path/to/scripts/cleanup_sessions.php
 */

$baseDir = dirname(__DIR__);
require_once $baseDir . '/src/Core/Database.php';
require_once $baseDir . '/src/Core/Logger.php';
require_once $baseDir . '/src/Core/SessionManager.php';

$config = require $baseDir . '/config/config.php';
date_default_timezone_set($config['timezone'] ?? 'Asia/Baghdad');

$logger = new \App\Core\Logger($baseDir . '/logs/sessions.log');
$db = \App\Core\Database::getInstance($config['db'])->connection();
$sessionManager = new \App\Core\SessionManager($db, $logger, $config);

try {
    $deleted = $sessionManager->cleanup();
    $logger->info("Sessions cleaned", ['count' => $deleted]);
    echo "Cleaned {$deleted} expired sessions\n";
} catch (\Throwable $e) {
    $logger->error("Session cleanup failed", ['error' => $e->getMessage()]);
    exit(1);
}
