<?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('payouts', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id');
            $table->string('payout_number', 50)->unique();
            $table->enum('recipient_type', ['driver', 'operator', 'affiliate']);
            $table->unsignedBigInteger('recipient_id');
            $table->decimal('amount', 12, 2);
            $table->string('currency', 3)->default('USD');
            $table->date('period_start')->nullable();
            $table->date('period_end')->nullable();
            $table->enum('status', ['pending', 'processing', 'completed', 'failed', 'canceled'])
                ->default('pending');
            $table->string('payment_method', 50)->nullable();
            $table->unsignedBigInteger('bank_account_id')->nullable();
            $table->timestamp('processing_date')->nullable();
            $table->timestamp('completed_date')->nullable();
            $table->timestamps();
            
            $table->foreign('tenant_id')->references('id')->on('tenants')->onDelete('cascade');
            $table->index('tenant_id');
            $table->index('recipient_type');
            $table->index('status');
        });
    }

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