<?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::hasColumn('agents', 'country')) {
            Schema::table('agents', function (Blueprint $table): void {
                $table->string('country')->default('Nigeria')->after('residential_address')->index();
            });
        }

        DB::table('agents')
            ->whereNull('country')
            ->orWhere('country', '')
            ->update(['country' => 'Nigeria']);
    }

    public function down(): void
    {
        if (Schema::hasColumn('agents', 'country')) {
            Schema::table('agents', function (Blueprint $table): void {
                $table->dropColumn('country');
            });
        }
    }
};
