<?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 {
    public function up(): void
    {
        if (! Schema::hasTable('countries')) {
            $this->createCountriesTable();

            return;
        }

        if (Schema::hasColumn('countries', 'id') && Schema::hasColumn('countries', 'iso2')) {
            return;
        }

        $legacyCountries = DB::table('countries')->get();
        Schema::drop('countries');
        $this->createCountriesTable();

        foreach ($legacyCountries as $country) {
            $iso2 = strtoupper(trim((string) ($country->iso ?? '')));

            if ($iso2 === '' || ! preg_match('/^[A-Z]{2}$/', $iso2)) {
                continue;
            }

            DB::table('countries')->insert([
                'iso' => $iso2,
                'iso2' => $iso2,
                'name' => $country->name ?? null,
                'native' => $country->native ?? null,
                'phone' => $country->phone ?? null,
                'continent' => $country->continent ?? null,
                'capital' => $country->capital ?? null,
                'currency' => $country->currency ?? null,
                'languages' => $country->languages ?? null,
                'name_common' => $country->name ?? $iso2,
                'name_official' => $country->native ?? null,
                'active' => true,
                'sort_order' => 1000,
                'is_featured' => false,
                'created_at' => now(),
                'updated_at' => now(),
            ]);
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('countries');
    }

    private function createCountriesTable(): void
    {
        Schema::create('countries', function (Blueprint $table): void {
            $table->id();
            $table->char('iso', 2)->nullable()->unique();
            $table->char('iso2', 2)->unique();
            $table->char('iso3', 3)->nullable()->index();
            $table->string('numeric_code', 3)->nullable();
            $table->string('name', 255)->nullable();
            $table->string('native', 255)->nullable();
            $table->string('phone', 50)->nullable();
            $table->char('continent', 2)->nullable();
            $table->string('capital', 255)->nullable();
            $table->string('currency', 32)->nullable();
            $table->string('languages', 255)->nullable();
            $table->string('name_common', 255);
            $table->string('name_official', 255)->nullable();
            $table->string('region', 120)->nullable();
            $table->string('subregion', 120)->nullable();
            $table->text('flag_svg_url')->nullable();
            $table->text('flag_png_url')->nullable();
            $table->string('flag_emoji', 16)->nullable();
            $table->boolean('active')->default(true);
            $table->unsignedInteger('sort_order')->default(1000);
            $table->boolean('is_featured')->default(false);
            $table->timestamps();

            $table->index('active');
            $table->index('name_common');
            $table->index(['is_featured', 'sort_order']);
        });
    }
};
