<?php

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

/**
 * Refunds (§31).
 *
 * A refund is its own record from request through payout; it never edits the
 * payment it refers back to (§70). Amounts are minor units with a currency.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('refunds', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('refund_no', 40);

            $table->foreignId('company_id')->constrained()->cascadeOnUpdate()->restrictOnDelete();
            $table->foreignId('branch_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('applicant_id')->constrained()->cascadeOnUpdate()->restrictOnDelete();
            $table->foreignId('invoice_id')->nullable()->constrained()->nullOnDelete();

            // The receipt being refunded, where the money is traceable to one.
            $table->foreignId('payment_id')->nullable()->constrained()->nullOnDelete();

            $table->string('status', 20)->default('requested');
            $table->char('currency', 3);

            // What was asked for, what the agency accepts, and what it keeps.
            $table->unsignedBigInteger('requested_amount');
            $table->unsignedBigInteger('approved_amount')->default(0);
            $table->unsignedBigInteger('deduction_amount')->default(0);
            $table->unsignedBigInteger('paid_amount')->default(0);
            $table->text('deduction_reason')->nullable();

            $table->string('reason_code', 40)->nullable();
            $table->text('reason')->nullable();

            $table->foreignId('requested_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('requested_at');

            $table->foreignId('reviewed_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('reviewed_at')->nullable();
            $table->text('review_note')->nullable();

            $table->foreignId('approved_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('approved_at')->nullable();

            $table->text('rejection_reason')->nullable();

            // Payout details, filled in when accounts actually disburse.
            $table->string('payout_method', 30)->nullable();
            $table->string('beneficiary_name')->nullable();
            $table->string('beneficiary_account', 120)->nullable();
            $table->string('transaction_reference', 100)->nullable();
            $table->date('paid_on')->nullable();
            $table->foreignId('paid_by')->nullable()->constrained('users')->nullOnDelete();

            $table->text('notes')->nullable();
            $table->timestamps();

            $table->unique(['company_id', 'refund_no']);
            $table->unique(['company_id', 'transaction_reference']);
            $table->index(['applicant_id', 'status']);
            $table->index(['company_id', 'status', 'requested_at']);
        });
    }

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