<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    private array $timezones = [
        176   => 'Antarctica/Macquarie',
        1012  => 'America/Iqaluit',
        1129  => 'America/Iqaluit',
        1153  => 'America/Iqaluit',
        9241  => 'Asia/Anadyr',
        9245  => 'Asia/Anadyr',
        9364  => 'Europe/Moscow',
        9365  => 'Asia/Krasnoyarsk',
        11018 => 'Asia/Ulaanbaatar',
        11019 => 'Asia/Ulaanbaatar',
    ];

    public function up(): void
    {
        // 1. Populate timezones for the specific airports that were missing them
        foreach ($this->timezones as $id => $timezone) {
            DB::table('airports')
                ->where('id', $id)
                ->whereNull('timezone')
                ->update(['timezone' => $timezone]);
        }

        // 2. Make the timezone column non-nullable
        Schema::table('airports', function (Blueprint $table) {
            $table->string('timezone')->nullable(false)->change();
        });
    }

    public function down(): void
    {
        // 1. Revert the column back to nullable first
        Schema::table('airports', function (Blueprint $table) {
            $table->string('timezone')->nullable()->change();
        });

        // 2. Null out only the rows we populated
        DB::table('airports')
            ->whereIn('id', array_keys($this->timezones))
            ->update(['timezone' => null]);
    }
};
