<?php

use App\Models\Achievement;
use App\Models\AchievementCategory;
use App\Models\AchievementDifficulty;
use App\Models\Aircraft;
use App\Models\IataEquipmentCode;
use App\Models\User;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */

    function setDesignatorDefault($designator, $model_name, $manufacturer_code=null): self
    {
        $manufacturerSearchTerm = $manufacturer_code ? ['manufacturer_code' => $manufacturer_code] : [];

        $count = Aircraft::where([
            'designator' => $designator,
            'model_full_name' => $model_name,
            ...$manufacturerSearchTerm,
        ])->update(['preferred' => true]);

        echo $designator . ' ' . $model_name . ' ' . $manufacturer_code . ' ' . $count . PHP_EOL;

        return $this;
    }

    function setDesignatorDefaults(): void
    {
        $this
            ->setDesignatorDefault('A19N', 'A-319neo')
            ->setDesignatorDefault('A306', 'A-300B4-600')
            ->setDesignatorDefault('A30B', 'A-300B4-200')
            ->setDesignatorDefault('A310', 'A-310')
            ->setDesignatorDefault('A318', 'A-318')
            ->setDesignatorDefault('A319', 'A-319')
            ->setDesignatorDefault('A320', 'A-320')
            ->setDesignatorDefault('A332', 'A-330-200')
            ->setDesignatorDefault('A338', 'A-330-800')
            ->setDesignatorDefault('A339', 'A-330-900')
            ->setDesignatorDefault('A342', 'A-340-200')
            ->setDesignatorDefault('A343', 'A-340-300')
            ->setDesignatorDefault('A345', 'A-340-500')
            ->setDesignatorDefault('A346', 'A-340-600')
            ->setDesignatorDefault('A359', 'A-350-900 XWB')
            ->setDesignatorDefault('A35K', 'A-350-1000 XWB')
            ->setDesignatorDefault('A388', 'A-380-800')
            ->setDesignatorDefault('AN24', 'An-24')
            ->setDesignatorDefault('AN26', 'An-26')
            ->setDesignatorDefault('AT43', 'ATR-42-300')
            ->setDesignatorDefault('AT44', 'ATR-42-400')
            ->setDesignatorDefault('AT72', 'ATR-72-201')
            ->setDesignatorDefault('AT73', 'ATR-72-211')
            ->setDesignatorDefault('AT75', 'ATR-72-500')
            ->setDesignatorDefault('AT76', 'ATR-72-600')
            ->setDesignatorDefault('B37M', '737 MAX 7')
            ->setDesignatorDefault('B38M', '737 MAX 8')
            ->setDesignatorDefault('B39M', '737 MAX 9')
            ->setDesignatorDefault('B3XM', '737 MAX 10')
            ->setDesignatorDefault('B461', 'BAe-146-100')
            ->setDesignatorDefault('B462', 'BAe-146-200')
            ->setDesignatorDefault('B703', '707-300')
            ->setDesignatorDefault('B712', '717-200')
            ->setDesignatorDefault('B721', '727-100')
            ->setDesignatorDefault('B732', '737-200')
            ->setDesignatorDefault('B737', '737-700')
            ->setDesignatorDefault('B738', '737-800')
            ->setDesignatorDefault('B739', '737-900')
            ->setDesignatorDefault('B742', '747-200')
            ->setDesignatorDefault('B748', '747-8')
            ->setDesignatorDefault('B752', '757-200')
            ->setDesignatorDefault('B772', '777-200')
            ->setDesignatorDefault('B778', '777-8')
            ->setDesignatorDefault('B77L', '777-200LR')
            ->setDesignatorDefault('B77W', '777-300ER')
            ->setDesignatorDefault('B788', '787-8 Dreamliner')
            ->setDesignatorDefault('B789', '787-9 Dreamliner')
            ->setDesignatorDefault('BCS1', 'A-220-100')
            ->setDesignatorDefault('BCS3', 'A-220-300')
            ->setDesignatorDefault('CRJ1', 'CL-600 Regional Jet CRJ-100')
            ->setDesignatorDefault('CRJ2', 'CL-600 Regional Jet CRJ-200')
            ->setDesignatorDefault('CRJ7', 'CL-600 Regional Jet CRJ-700')
            ->setDesignatorDefault('CRJ9', 'CL-600 Regional Jet CRJ-900')
            ->setDesignatorDefault('DC10', 'DC-10')
            ->setDesignatorDefault('DC3', 'DC-3')
            ->setDesignatorDefault('DH8B', 'DHC-8-200 Dash 8' , 'DE HAVILLAND CANADA')
            ->setDesignatorDefault('DH8A', 'DHC-8-100 Dash 8' , 'DE HAVILLAND CANADA')
            ->setDesignatorDefault('DH8C', 'DHC-8-300 Dash 8' , 'DE HAVILLAND CANADA')
            ->setDesignatorDefault('DH8D', 'DHC-8-400 Dash 8' , 'DE HAVILLAND CANADA')
            ->setDesignatorDefault('DHC6', 'DHC-6 Twin Otter','DE HAVILLAND CANADA')
            ->setDesignatorDefault('E120', 'EMB-120 Brasilia', 'EMBRAER')
            ->setDesignatorDefault('E135', 'ERJ-135')
            ->setDesignatorDefault('E145', 'ERJ-145ER')
            ->setDesignatorDefault('E170', '170')
            ->setDesignatorDefault('E190', '190')
            ->setDesignatorDefault('E195', '195')
            ->setDesignatorDefault('E275', 'E175-E2')
            ->setDesignatorDefault('E290', 'E190-E2')
            ->setDesignatorDefault('E295', 'E195-E2')
            ->setDesignatorDefault('F27', 'F-27 Friendship', 'FOKKER')
            ->setDesignatorDefault('JS32', 'BAe-3200 Jetstream Super 31', 'BRITISH AEROSPACE')
            ->setDesignatorDefault('JS41', 'BAe-4100 Jetstream 41', 'BRITISH AEROSPACE')
            ->setDesignatorDefault('MD81', 'MD-81', 'MCDONNELL DOUGLAS')
            ->setDesignatorDefault('MD82', 'MD-82', 'MCDONNELL DOUGLAS')
            ->setDesignatorDefault('MD83', 'MD-83', 'MCDONNELL DOUGLAS')
            ->setDesignatorDefault('MD87', 'MD-87', 'MCDONNELL DOUGLAS')
            ->setDesignatorDefault('MD88', 'MD-88', 'MCDONNELL DOUGLAS')
            ->setDesignatorDefault('MD90', 'MD-90', 'MCDONNELL DOUGLAS')
            ->setDesignatorDefault('AJ27', 'C-909')
        ;
    }

    public function up(): void
    {
        Schema::table('aircraft', function (Blueprint $table) {
            $table->boolean('preferred')->default(false)->after('designator');
        });

        $this->setDesignatorDefaults();

        IataEquipmentCode::create([
            'iata_code' => '388',
            'icao_code' => 'A388',
            'description' => 'Airbus A380'
        ]);

        Achievement::create([
            'name' => 'Gone a Gigametre',
            'internal_name' => 'general_flying.gigametre',
            'short_description' => 'Fly 1 million kilometres.',
            'long_description' => '',
            'achievement_difficulty_id' => AchievementDifficulty::whereInternalName('near_impossible')->first()->id,
            'achievement_category_id' => AchievementCategory::whereInternalName('general_flying')->first()->id,
            'icon' => 'standard_achievement.png',
            'has_page' => false,
            'sort_order' => 18,
            'progressive' => true,
            'threshold' => 1000000,
        ]);

        Achievement::whereInternalName('aircraft.smaller_manufacturer')->update([
            'difficulty_description' => 'General Aviation flights do not count. Only flights with an airline and flight number can earn this achievement',
        ]);

        foreach(User::all() as $user) {
            $user->calculateAchievements();
        }
    }

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