<?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::create('provider_withdrawals', function (Blueprint $table) {
            $table->id();
            $table->foreignId('provider_id')->constrained('providers')->onDelete('cascade');
            $table->string('recipient_id');
            $table->string('transfer_id')->nullable();
            $table->string('idempotency_key')->unique();
            $table->decimal('gross_amount', 10, 2);
            $table->decimal('gateway_fee_amount', 10, 2)->default(0);
            $table->decimal('net_amount', 10, 2);
            $table->enum('status', ['pending_transfer', 'processing', 'transferred', 'failed', 'canceled'])->default('pending_transfer');
            $table->string('type')->nullable();
            $table->json('bank_account')->nullable();
            $table->text('bank_response')->nullable();
            $table->json('gateway_payload')->nullable();
            $table->timestamp('completed_at')->nullable();
            $table->timestamp('failed_at')->nullable();
            $table->json('metadata')->nullable();
            $table->timestamps();
            $table->softDeletes();

            $table->index('provider_id');
            $table->index('transfer_id');
            $table->index('status');
        });
    }

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