<?php
// Run from cron daily, for example:
// php /path/to/pyrk/cron/run_inactivity_rules.php
require __DIR__ . '/../api/bootstrap.php';

use App\Services\InactivityRulesService;

try {
    $svc = new InactivityRulesService();
    if (!$svc->tableReady()) {
        echo '[' . date('c') . "] Inactivity rules table is not installed.\n";
        exit(0);
    }

    $results = $svc->runAllActive(0);
    $rules = count($results);
    $members = 0;
    $points = 0;

    foreach ($results as $row) {
        $members += (int)($row['result']['applied_count'] ?? 0);
        $points += (int)($row['result']['points_deducted'] ?? 0);
    }

    echo '[' . date('c') . "] Inactivity rules applied. Rules: {$rules}, Members: {$members}, Points: {$points}\n";
} catch (Throwable $e) {
    echo '[' . date('c') . '] Inactivity rules failed: ' . $e->getMessage() . "\n";
    exit(1);
}
