<?php

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

/**
 * PRC-1143 — konsolide şema: `transactions` tablosunun TAM tanımı (tek dosya).
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('transactions', function (Blueprint $t) {
            $t->id();
            $t->char('uuid', 36);
            $t->unsignedBigInteger('client_id');
            $t->unsignedBigInteger('currency_id');
            $t->enum('type', ['payment','refund','withdrawal','deposit']);
            $t->enum('status', ['pending','completed','failed','cancelled'])->default('pending');
            $t->decimal('amount', 15, 2);
            $t->decimal('amount_in', 15, 2)->default(0.00);
            $t->decimal('amount_out', 15, 2)->default(0.00);
            $t->decimal('bank_fee', 15, 2)->default(0.00);
            $t->string('transaction_id', 255)->nullable();
            $t->text('notes')->nullable();
            $t->text('description')->nullable();
            $t->string('payment_method', 255)->nullable();
            $t->string('payment_gateway', 255)->nullable();
            $t->string('payment_transaction_id', 255)->nullable();
            $t->json('payment_details')->nullable();
            $t->unsignedBigInteger('invoice_id')->nullable();
            $t->unsignedBigInteger('order_id')->nullable();
            $t->string('reference_type', 255)->nullable();
            $t->unsignedBigInteger('reference_id')->nullable();
            $t->unsignedBigInteger('admin_id')->nullable();
            $t->string('ip_address', 45)->nullable();
            $t->json('metadata')->nullable();
            $t->timestamp('completed_at')->nullable();
            $t->timestamp('failed_at')->nullable();
            $t->timestamp('created_at')->nullable();
            $t->timestamp('updated_at')->nullable();
            $t->unsignedTinyInteger('installment_count')->nullable();
            $t->decimal('installment_surcharge', 15, 2)->nullable();
            $t->string('installment_payment_id', 32)->nullable();
            $t->index('admin_id', 'transactions_admin_id_foreign');
            $t->index(['client_id', 'created_at'], 'transactions_client_id_created_at_index');
            $t->index(['client_id', 'currency_id'], 'transactions_client_id_currency_id_index');
            $t->index('created_at', 'transactions_created_at_index');
            $t->index('currency_id', 'transactions_currency_id_foreign');
            $t->index('invoice_id', 'transactions_invoice_id_index');
            $t->index('order_id', 'transactions_order_id_index');
            $t->index('payment_transaction_id', 'transactions_payment_transaction_id_index');
            $t->index(['reference_type', 'reference_id'], 'transactions_reference_type_reference_id_index');
            $t->index(['status', 'created_at'], 'transactions_status_created_at_index');
            $t->index(['type', 'created_at'], 'transactions_type_created_at_index');
            $t->unique('uuid', 'transactions_uuid_unique');
            $t->foreign('admin_id', 'transactions_admin_id_foreign')->references('id')->on('admins')->onDelete('set null');
            $t->foreign('client_id', 'transactions_client_id_foreign')->references('id')->on('clients')->onDelete('cascade');
            $t->foreign('currency_id', 'transactions_currency_id_foreign')->references('id')->on('currencies')->onDelete('cascade');
            $t->foreign('invoice_id', 'transactions_invoice_id_foreign')->references('id')->on('invoices')->onDelete('set null');
            $t->foreign('order_id', 'transactions_order_id_foreign')->references('id')->on('orders')->onDelete('set null');
        });
    }

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