<?php

use App\Models\Achievement;
use App\Models\AchievementCategory;
use App\Models\AchievementDifficulty;
use App\Models\Airline;
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.
     */
    public function up(): void
    {

        Achievement::create([
            'name' => 'Circumnavigator',
            'internal_name' => 'general_flying.circumference_of_the_earth',
            'short_description' => 'Fly the same distance as the circumference of the Earth at the equator!',
            'icon' => 'standard_achievement.png',
            'progressive' => true,
            'long_description' => '',
            'achievement_category_id' => AchievementCategory::where('internal_name', 'general_flying')->first()->id,
            'achievement_difficulty_id' => AchievementDifficulty::where('internal_name', 'moderate')->first()->id,
            'threshold' => 40075,
            'has_page' => false,
        ]);

        Achievement::create([
            'name' => 'Fly Me to The Moon',
            'internal_name' => 'general_flying.to_the_moon',
            'short_description' => 'Fly the same distance as the Earth to the Moon!',
            'icon' => 'standard_achievement.png',
            'long_description' => '',
            'progressive' => true,
            'threshold' => 384400,
            'achievement_category_id' => AchievementCategory::where('internal_name', 'general_flying')->first()->id,
            'achievement_difficulty_id' => AchievementDifficulty::where('internal_name', 'hard')->first()->id,
            'has_page' => false,
        ]);

        Achievement::whereInternalName('aircraft.all_boeing_7x7')->update(['has_page' => true]);
        Achievement::whereInternalName('aircraft.all_airbus_a3xx')->update(['has_page' => true]);
        Airline::whereInternalName('south-africa-airways')->update(['name' => 'South African Airways']);

        Schema::table('achievements', function (Blueprint $table) {
            $table->unsignedInteger('sort_order')->nullable()->after('id');
        });

        // Seed sort_order from current id order, scoped per category
        $achievements = DB::table('achievements')
            ->orderBy('achievement_category_id')
            ->orderBy('id')
            ->get();

        $position = 1;
        $currentCategory = null;

        foreach ($achievements as $achievement) {
            if ($achievement->achievement_category_id !== $currentCategory) {
                $position = 1;
                $currentCategory = $achievement->achievement_category_id;
            }
            DB::table('achievements')
                ->where('id', $achievement->id)
                ->update(['sort_order' => $position++]);
        }

        // Move "Four on the Floor" (id 30) after "Triple Threat" (id 34)
        // within the aircraft category — swap their sort_order values
        $triEngine  = DB::table('achievements')->where('internal_name', 'aircraft.tri_engine')->first();
        $quadEngine = DB::table('achievements')->where('internal_name', 'aircraft.quad_engine')->first();

        DB::table('achievements')->where('internal_name', 'aircraft.quad_engine')
            ->update(['sort_order' => $triEngine->sort_order + 1]);

        // Shift everything between them up by 1 to make room
        DB::table('achievements')
            ->where('achievement_category_id', $quadEngine->achievement_category_id)
            ->where('sort_order', '>=', $triEngine->sort_order + 1)
            ->where('internal_name', '!=', 'aircraft.quad_engine')
            ->increment('sort_order');

        $users = User::all();

        foreach ($users as $user) {
            $user->calculateAchievements();
        }
    }

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