<?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('customers', function (Blueprint $table): void {
            $table->string('virtual_account_provider')->nullable()->after('account_number');
            $table->string('virtual_account_bank_name')->nullable()->after('virtual_account_provider');
            $table->string('virtual_account_bank_code')->nullable()->after('virtual_account_bank_name');
            $table->string('virtual_account_number')->nullable()->after('virtual_account_bank_code');
            $table->string('virtual_account_name')->nullable()->after('virtual_account_number');
            $table->string('virtual_account_reference')->nullable()->after('virtual_account_name');
            $table->string('virtual_account_customer_code')->nullable()->after('virtual_account_reference');
            $table->string('virtual_account_status')->default('not_created')->after('virtual_account_customer_code');
            $table->timestamp('virtual_account_assigned_at')->nullable()->after('virtual_account_status');
            $table->json('virtual_account_metadata')->nullable()->after('virtual_account_assigned_at');

            $table->index('virtual_account_provider', 'customers_virtual_account_provider_idx');
            $table->unique('virtual_account_number', 'customers_virtual_account_number_unique');
            $table->index('virtual_account_reference', 'customers_virtual_account_reference_idx');
            $table->index(['tenant_id', 'virtual_account_status'], 'customers_tenant_virtual_status_idx');
        });
    }

    public function down(): void
    {
        Schema::table('customers', function (Blueprint $table): void {
            $table->dropIndex('customers_tenant_virtual_status_idx');
            $table->dropIndex('customers_virtual_account_reference_idx');
            $table->dropUnique('customers_virtual_account_number_unique');
            $table->dropIndex('customers_virtual_account_provider_idx');

            $table->dropColumn([
                'virtual_account_provider',
                'virtual_account_bank_name',
                'virtual_account_bank_code',
                'virtual_account_number',
                'virtual_account_name',
                'virtual_account_reference',
                'virtual_account_customer_code',
                'virtual_account_status',
                'virtual_account_assigned_at',
                'virtual_account_metadata',
            ]);
        });
    }
};
