<?php

namespace App\Console\Commands;

use App\Models\Airline;
use App\Models\Alliance;
use Illuminate\Console\Attributes\Description;
use Illuminate\Console\Attributes\Signature;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Collection;

#[Signature('app:update-alliances')]
#[Description('Looks for any changes to Alliance membership and updates automatically')]
class UpdateAlliances extends Command
{
    public function handle(): void
    {
        $day = now('UTC')->startOfMinute()->toDateString();

        $schedules = [
            '2026-12-17' => [
                ['airline' => 'asiana', 'alliance' => null],
            ],
            '2027-12-12' => [
                ['airline' => 'philippine-airlines', 'alliance' => 'oneworld'],
            ],
        ];

        if (!isset($schedules[$day])) {
            return;
        }

        foreach ($schedules[$day] as $change) {
            $allianceId = $change['alliance']
                ? Alliance::whereInternalName($change['alliance'])->value('id')
                : null;

            Airline::whereInternalName($change['airline'])->update(['alliance' => $allianceId]);
        }

        Alliance::recalculateChallengeThresholds();
    }

}
