<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Modules\Approval\Enums\ApprovalStepStatusEnum;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('approval_request_steps', function (Blueprint $table) {
            $table->id();
            $table->foreignId('approval_request_id')->constrained()->cascadeOnDelete();
            $table->foreignId('approval_workflow_step_id')->constrained()->cascadeOnDelete();
            $table->unsignedInteger('level');
            $table->string('permission_name');
            $table->enum('status', ApprovalStepStatusEnum::casesAsArray())->default(
                ApprovalStepStatusEnum::PENDING->value
            );
            $table->foreignId('action_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('action_at')->nullable();
            $table->text('remarks')->nullable();
            $table->timestamps();

            $table->unique([
                'approval_request_id',
                'level'
            ]);

            $table->index([
                'status',
                'level'
            ]);
        });
    }

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