<?php
declare(strict_types=1);

namespace CronJobs;

use App\Core\App;
use App\Core\Database;
use App\Core\Logger;
use App\Modules\SportsActivity\Services\SubscriptionGeneratorService;

class SaMonthlySubscriptionJob
{
    private Database $db;
    public function __construct(Database $db) { $this->db = $db; }

    public function shouldRun(): bool
    {
        return (int) date('j') <= 3;
    }

    public function run(): array
    {
        App::getInstance()->setDb($this->db);

        $yearMonth = date('Y-m');

        $result = SubscriptionGeneratorService::generateForMonth($yearMonth);

        Logger::info(sprintf(
            "SaMonthlySubscriptionJob: generated=%d, skipped=%d, medical_blocked=%d for %s",
            $result['generated'],
            $result['skipped'],
            $result['medical_blocked'],
            $yearMonth
        ));

        return [
            'generated'       => $result['generated'],
            'skipped'         => $result['skipped'],
            'medical_blocked' => $result['medical_blocked'],
            'month'           => $yearMonth,
        ];
    }
}
