<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('agents', function (Blueprint $table): void {
            if (! Schema::hasColumn('agents', 'kyc_status')) {
                $table->string('kyc_status', 30)->default('not_submitted')->after('status')->index();
            }

            if (! Schema::hasColumn('agents', 'kyc_submitted_at')) {
                $table->timestamp('kyc_submitted_at')->nullable()->after('kyc_status');
            }

            if (! Schema::hasColumn('agents', 'kyc_verified_at')) {
                $table->timestamp('kyc_verified_at')->nullable()->after('kyc_submitted_at');
            }

            if (! Schema::hasColumn('agents', 'kyc_rejected_reason')) {
                $table->text('kyc_rejected_reason')->nullable()->after('kyc_verified_at');
            }

            if (! Schema::hasColumn('agents', 'date_of_birth')) {
                $table->date('date_of_birth')->nullable()->after('kyc_rejected_reason');
            }

            if (! Schema::hasColumn('agents', 'residential_address')) {
                $table->text('residential_address')->nullable()->after('date_of_birth');
            }

            if (! Schema::hasColumn('agents', 'country')) {
                $table->string('country')->default('Nigeria')->after('residential_address')->index();
            }

            if (! Schema::hasColumn('agents', 'state')) {
                $table->string('state')->nullable()->after('country');
            }

            if (! Schema::hasColumn('agents', 'city')) {
                $table->string('city')->nullable()->after('state');
            }

            if (! Schema::hasColumn('agents', 'id_type')) {
                $table->string('id_type')->nullable()->after('city');
            }

            if (! Schema::hasColumn('agents', 'id_number')) {
                $table->string('id_number')->nullable()->after('id_type');
            }

            if (! Schema::hasColumn('agents', 'bvn')) {
                $table->string('bvn', 20)->nullable()->after('id_number');
            }

            if (! Schema::hasColumn('agents', 'nin')) {
                $table->string('nin', 20)->nullable()->after('bvn');
            }

            if (! Schema::hasColumn('agents', 'business_registration_number')) {
                $table->string('business_registration_number')->nullable()->after('nin');
            }

            if (! Schema::hasColumn('agents', 'years_experience')) {
                $table->unsignedSmallInteger('years_experience')->nullable()->after('business_registration_number');
            }

            if (! Schema::hasColumn('agents', 'coverage_area')) {
                $table->string('coverage_area')->nullable()->after('years_experience');
            }

            if (! Schema::hasColumn('agents', 'referee_name')) {
                $table->string('referee_name')->nullable()->after('coverage_area');
            }

            if (! Schema::hasColumn('agents', 'referee_phone')) {
                $table->string('referee_phone', 50)->nullable()->after('referee_name');
            }

            if (! Schema::hasColumn('agents', 'passport_photo_path')) {
                $table->string('passport_photo_path')->nullable()->after('referee_phone');
            }

            if (! Schema::hasColumn('agents', 'id_document_path')) {
                $table->string('id_document_path')->nullable()->after('passport_photo_path');
            }
        });
    }

    public function down(): void
    {
        $columns = [
            'id_document_path',
            'passport_photo_path',
            'referee_phone',
            'referee_name',
            'coverage_area',
            'years_experience',
            'business_registration_number',
            'nin',
            'bvn',
            'id_number',
            'id_type',
            'city',
            'state',
            'country',
            'residential_address',
            'date_of_birth',
            'kyc_rejected_reason',
            'kyc_verified_at',
            'kyc_submitted_at',
            'kyc_status',
        ];

        Schema::table('agents', function (Blueprint $table) use ($columns): void {
            foreach ($columns as $column) {
                if (Schema::hasColumn('agents', $column)) {
                    $table->dropColumn($column);
                }
            }
        });
    }
};
