<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('joint_account_approvals', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('joint_account_transaction_id');
            $table->unsignedBigInteger('approver_id');
            $table->enum('action', ['approved', 'rejected']); 
            $table->text('comment')->nullable();
            $table->timestamp('action_at');
            $table->timestamps();

            $table->foreign('joint_account_transaction_id', 'ja_approvals_transaction_foreign')
                  ->references('id')->on('joint_account_transactions')->onDelete('cascade');
            $table->foreign('approver_id')->references('id')->on('users')->onDelete('cascade');
            
            $table->unique(['joint_account_transaction_id', 'approver_id'], 'ja_approvals_unique');
            $table->index(['approver_id', 'action_at']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('joint_account_approvals');
    }
}; 