<?php

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

return new class extends Migration
{
    public function up()
    {
        Schema::create('transfers', function (Blueprint $table) {
            $table->id();
            $table->foreignId('sender_id')->constrained('users')->onDelete('cascade');
            $table->foreignId('recipient_id')->constrained('clients')->onDelete('cascade');
            $table->decimal('amount', 15, 2);
            $table->string('currency_from', 3);
            $table->string('currency_to', 3);
            $table->decimal('exchange_rate', 10, 6);
            $table->decimal('fee', 10, 2);
            $table->decimal('total_amount', 15, 2);
            $table->decimal('recipient_amount', 15, 2);
            $table->enum('status', ['pending', 'completed', 'failed', 'cancelled', 'scheduled'])->default('pending');
            $table->timestamp('transfer_date');
            $table->timestamp('expected_delivery_date')->nullable();
            $table->timestamp('actual_delivery_date')->nullable();
            $table->text('notes')->nullable();
            $table->string('reference_number')->unique();
            $table->string('payment_method');
            $table->json('bank_details')->nullable();
            $table->enum('transfer_type', ['instant', 'scheduled', 'recurring'])->default('instant');
            $table->enum('recurring_frequency', ['daily', 'weekly', 'monthly'])->nullable();
            $table->timestamp('recurring_end_date')->nullable();
            $table->boolean('is_recurring')->default(false);
            $table->foreignId('parent_transfer_id')->nullable()->constrained('transfers')->onDelete('cascade');
            $table->timestamps();

            $table->index(['sender_id', 'status']);
            $table->index(['recipient_id', 'status']);
            $table->index(['transfer_date']);
            $table->index(['reference_number']);
        });
    }

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