<?php

namespace App\Commands;

use App\Services\RecurrenceService;
use App\Support\CommandLock;
use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;

class GenerateTaskOccurrences extends BaseCommand
{
    protected $group       = 'Tasks';
    protected $name        = 'tasks:generate-occurrences';
    protected $description = 'Keep active recurring tasks generated approximately 90 days ahead.';

    public function run(array $params)
    {
        return CommandLock::run('tasks-generate-occurrences', static function (): int {
            $created = (new RecurrenceService())->extendHorizon();
            CLI::write(
                sprintf('Generated %d occurrence(s).', $created),
                $created > 0 ? 'green' : 'white',
            );

            return EXIT_SUCCESS;
        });
    }
}
