#!/usr/bin/env php
<?php
declare(strict_types=1);

/**
 * Purge stale remote-access sessions (cron: hourly).
 * Usage: php scripts/purge_remote_access.php [--hours=24]
 */
require_once __DIR__ . '/../src/bootstrap.php';
require_once __DIR__ . '/../src/RemoteAccessRepository.php';
require_once __DIR__ . '/../src/WireGuardPeerProvisioner.php';

$hours = 24;
foreach ($argv as $arg) {
    if (str_starts_with($arg, '--hours=')) {
        $hours = max(1, (int) substr($arg, 8));
    }
}

RemoteAccessRepository::ensureSchema();
$n = RemoteAccessRepository::purgeStale($hours);
fwrite(STDOUT, "purged_sessions={$n}\n");
