<?php

use App\Models\Airline;
use App\Models\Country;
use App\Models\SeatType;
use App\Models\UserFlight;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function createAirline(
        string $name,
        bool $active,
        string $countryCode,
        ?string $iataCode = null,
        ?string $icaoCode = null,
        ?string $logo = null,
        string $internalName = null,
    ): self {
        $country = Country::where('code', $countryCode)->firstOrFail();

        $logo = $logo ?? $iataCode . '.png';

        Airline::create([
            'IATA_code'     => $iataCode,
            'ICAO_code'     => $icaoCode,
            'name'          => $name,
            'internal_name' => $internalName,
            'active'        => $active,
            'logo'          => $logo,
            'country_id'    => $country->id,
        ]);

        return $this;
    }


    function createAirlines(): void{
        $this
            ->createAirline(name: 'Horizontal Falls Seaplane Adventures', active: true, countryCode: 'AU', logo: 'horizontal-falls-seaplane-adventures.png', internalName: 'horizontal-falls-seaplane-adventures')
            ->createAirline(name: 'Pineapple Air', active: true, countryCode: 'BS', icaoCode: 'PNP', logo: 'pineapple-air.png', internalName: 'pineapple-air')
            ->createAirline(name: 'ATA Airlines', active: true, countryCode: 'IR', iataCode: 'I3', icaoCode: 'TBZ', logo: 'ata-airlines.png', internalName: 'ata-airlines')
            ->createAirline(name: 'Chabahar Air', active: true, countryCode: 'IR', iataCode: null, icaoCode: 'IRU', logo: 'chabahar-airlines.png', internalName: 'chabahar-airlines')
            ->createAirline(name: 'Kish Air', active: true, countryCode: 'IR', iataCode: 'Y9', icaoCode: 'KIS', logo: 'kish-air.png', internalName: 'kish-air')
            ->createAirline(name: 'Meraj Airlines', active: true, countryCode: 'IR', iataCode: 'JI', icaoCode: 'MRJ', logo: 'meraj-airlines.png', internalName: 'meraj-airlines')
            ->createAirline(name: 'Qazaq Air', active: false, countryCode: 'KZ', iataCode: 'IQ', icaoCode: 'QAZ', logo: 'qazaq-air.png', internalName: 'qazaq-air')
            ->createAirline(name: 'Raimon Airways', active: true, countryCode: 'IR', iataCode: null, icaoCode: 'RAI', logo: 'raimon-airways.png', internalName: 'raimon-airways')
            ->createAirline(name: 'Saha Airlines', active: true, countryCode: 'IR', iataCode: null, icaoCode: 'IRZ', logo: 'saha-airlines.png', internalName: 'saha-airlines')
            ->createAirline(name: 'Yazd Airways', active: true, countryCode: 'IR', iataCode: null, icaoCode: 'DZD', logo: 'yazd-airways.png', internalName: 'yazd-airways')
            ->createAirline(name: 'Zagros Airlines', active: true, countryCode: 'IR', iataCode: 'ZO', icaoCode: 'IZG', logo: 'zagros-airlines.png', internalName: 'zagros-airlines')
            ->createAirline(name: 'Taban Airlines', active: true, countryCode: 'IR', iataCode: null, icaoCode: 'TBN', logo: 'taban-airlines.png', internalName: 'taban-airlines')
            ->createAirline(name: 'Maun Helicopter Horizons', active: true, countryCode: 'BW', iataCode: null, icaoCode: null, logo: 'blank.png', internalName: 'maun-helicopter-horizons')
            ->createAirline(name: 'Pacific Mission Aviation', active: true, countryCode: 'PW', iataCode: null, icaoCode: null, logo: 'pacific-mission-aviation.png', internalName: 'pacific-mission-aviation')
            ->createAirline(name: 'Jetstar Pacific', active: false, countryCode: 'VN', iataCode: 'BL', icaoCode: 'PIC', logo: 'jq.png', internalName: 'jetstar-pacific')
            ->createAirline(name: 'Jetsmart', active: true, countryCode: 'CL', iataCode: 'JA', icaoCode: 'JAT', logo: 'ja.png', internalName: 'jetsmart')
            ->createAirline(name: 'Jetsmart Peru', active: true, countryCode: 'PE', iataCode: 'JZ', icaoCode: 'JAP', logo: 'jz.png', internalName: 'jetsmart-peru')
            ->createAirline(name: 'Jetsmart Argentina', active: true, countryCode: 'AR', iataCode: 'WJ', icaoCode: 'JES', logo: 'wj.png', internalName: 'jetsmart-argentina')
            ->createAirline(name: 'Jetsmart Colombia', active: true, countryCode: 'CO', iataCode: 'J6', icaoCode: 'JEC', logo: 'j6.png', internalName: 'jetsmart-colombia')
        ;
    }


    public function up(): void
    {
        DB::table('airlines')->whereNull('logo')->update(['logo' => 'blank.png']);

        Schema::table('airlines', function (Blueprint $table) {
            $table->string('logo')->default('blank.png')->nullable(false)->change();
        });

        Airline::where('name', 'AVIANCA')->update(['name' => 'Avianca']);

        $this->createAirlines();

        UserFlight::where('seat_type_id', null)->update(['seat_type_id' => 0]);
        UserFlight::where('flight_reason_id', null)->update(['flight_reason_id' => 0]);
        UserFlight::where('flight_class_id', null)->update(['flight_class_id' => 0]);

        UserFlight::where('flight_reason_id', 0)->update(['flight_reason_id' => 1]);
        SeatType::where('id', 0)->update(['name' => 'Unassigned']);
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        //
    }
};
