<?php

namespace App\Commands;

use App\Models\FootballSettingModel;
use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;

class FootballSetSeason extends BaseCommand
{
    protected $group = 'Football';
    protected $name = 'football:set-season';
    protected $description = 'Set the current API-Football season year';
    protected $usage = 'football:set-season <year> [season_ends_at Y-m-d]';

    public function run(array $params)
    {
        $year = (int) ($params[0] ?? 0);
        if ($year < 2000 || $year > 2100) {
            CLI::error('Usage: php spark football:set-season <year> [season_ends_at]');
            return EXIT_ERROR;
        }

        $endsAt = $params[1] ?? null;
        $payload = ['current_season' => $year];
        if ($endsAt !== null) {
            $payload['season_ends_at'] = $endsAt;
        }

        $model = new FootballSettingModel();
        $model->updateSettings($payload);

        CLI::write("Current season set to {$year}.", 'green');

        return EXIT_SUCCESS;
    }
}
