<?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
    {
        if (Schema::hasTable('payout_address_allowlists')) {
            Schema::table('payout_address_allowlists', function (Blueprint $table) {
                if (! Schema::hasColumn('payout_address_allowlists', 'public_id')) $table->string('public_id', 40)->nullable()->unique()->after('id');
                if (! Schema::hasColumn('payout_address_allowlists', 'network')) $table->string('network', 40)->nullable()->after('asset_id');
                if (! Schema::hasColumn('payout_address_allowlists', 'is_default')) $table->boolean('is_default')->default(false)->after('status');
                if (! Schema::hasColumn('payout_address_allowlists', 'metadata')) $table->json('metadata')->nullable()->after('verified_at');
                if (! Schema::hasColumn('payout_address_allowlists', 'archived_at')) $table->timestamp('archived_at')->nullable()->after('metadata');
            });
        }

        if (Schema::hasTable('merchants')) {
            Schema::table('merchants', function (Blueprint $table) {
                if (! Schema::hasColumn('merchants', 'kyc_status')) $table->string('kyc_status', 30)->default('not_submitted')->index()->after('status');
                if (! Schema::hasColumn('merchants', 'kyc_submitted_at')) $table->timestamp('kyc_submitted_at')->nullable()->after('kyc_status');
                if (! Schema::hasColumn('merchants', 'kyc_reviewed_at')) $table->timestamp('kyc_reviewed_at')->nullable()->after('kyc_submitted_at');
            });
        }

        if (! Schema::hasTable('kyc_documents')) {
            Schema::create('kyc_documents', function (Blueprint $table) {
                $table->id();
                $table->string('public_id', 40)->unique();
                $table->foreignId('merchant_id')->constrained()->cascadeOnDelete();
                $table->string('type', 40);
                $table->string('status', 30)->default('submitted')->index();
                $table->string('file_path');
                $table->string('original_filename')->nullable();
                $table->string('mime_type', 120)->nullable();
                $table->unsignedBigInteger('size_bytes')->default(0);
                $table->foreignId('reviewed_by')->nullable()->constrained('merchants')->nullOnDelete();
                $table->timestamp('reviewed_at')->nullable();
                $table->text('rejection_reason')->nullable();
                $table->json('metadata')->nullable();
                $table->timestamps();
                $table->index(['merchant_id', 'status']);
            });
        }

        if (! Schema::hasTable('settlement_batches')) {
            Schema::create('settlement_batches', function (Blueprint $table) {
                $table->id();
                $table->string('public_id', 40)->unique();
                $table->foreignId('merchant_id')->constrained()->cascadeOnDelete();
                $table->foreignId('asset_id')->constrained('assets')->restrictOnDelete();
                $table->foreignId('payout_wallet_id')->nullable()->constrained('payout_address_allowlists')->nullOnDelete();
                $table->foreignId('payout_id')->nullable()->constrained('payouts')->nullOnDelete();
                $table->string('environment', 16);
                $table->string('status', 30)->default('pending')->index();
                $table->decimal('amount_atomic', 65, 0);
                $table->decimal('fee_atomic', 65, 0)->default(0);
                $table->text('failure_reason')->nullable();
                $table->timestamp('approved_at')->nullable();
                $table->timestamp('completed_at')->nullable();
                $table->json('metadata')->nullable();
                $table->timestamps();
                $table->index(['merchant_id', 'environment', 'status']);
            });
        }
    }

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