<?php

use App\Contracts\Migration;
use App\Models\Fare;
use App\Models\PirepFare;
use App\Services\FareService;

/**
 * Check if the PIREP fare has an empty capacity and update it from the subfleet
 */
return new class() extends Migration
{
    public function up(): void
    {
        $cached = [];
        $fareSvc = app(FareService::class);
        $all_fares = PirepFare::with('pirep', 'pirep.aircraft', 'pirep.aircraft.subfleet')->get();

        /** @var PirepFare $fare */
        foreach ($all_fares as $fare) {
            if (empty($fare->pirep) || !empty($fare->capacity)) { // has capacity, skip
                continue;
            }

            // look up the subfleet
            $subfleet = $fare->pirep->aircraft?->subfleet;
            if (empty($subfleet)) {
                continue;
            }

            if (empty($cached[$subfleet->id])) {
                $cached[$subfleet->id] = $fareSvc->getForSubfleet($subfleet);
            }

            /** @var Fare $sf */
            $sf = $cached[$subfleet->id]->where('code', $fare->code)->first();
            $fare->capacity = empty($sf) ? $fare->count : $sf->capacity;
            $fare->save();
        }
    }
};
