<?php

namespace App\Commands;

use App\Libraries\FootballReferenceSync;
use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;

class FootballSyncReference extends BaseCommand
{
    protected $group = 'Football';
    protected $name = 'football:sync-reference';
    protected $description = 'Sync countries and current leagues from API-Football';

    public function run(array $params)
    {
        try {
            set_time_limit(0);
            $sync = new FootballReferenceSync();
            CLI::write('Syncing countries and leagues (bulk API, paginated)...', 'yellow');
            $result = $sync->syncAll();
            CLI::write("Countries synced: {$result['countries']}", 'green');
            CLI::write("Leagues synced: {$result['leagues']}", 'green');

            foreach ($result['warnings'] as $warning) {
                CLI::write($warning, 'yellow');
            }

            CLI::write('Reference sync complete.', 'green');
        } catch (\Throwable $e) {
            CLI::error($e->getMessage());
            return EXIT_ERROR;
        }

        return EXIT_SUCCESS;
    }
}
