<?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('approval_steps', function (Blueprint $table) {
            $table->id();
            $table->foreignId('approval_id')->constrained('approvals')->onDelete('cascade')->comment('결재 문서 ID');
            $table->integer('step_order')->comment('결재 순서');
            $table->string('step_type', 20)->comment('단계 유형: approval/agreement/reference');
            $table->foreignId('approver_id')->constrained('users')->onDelete('restrict')->comment('결재자 ID');
            $table->string('status', 20)->default('pending')->comment('상태: pending/approved/rejected/skipped');
            $table->text('comment')->nullable()->comment('결재 의견');
            $table->timestamp('acted_at')->nullable()->comment('결재일시');
            $table->boolean('is_read')->default(false)->comment('열람 여부 (참조용)');
            $table->timestamp('read_at')->nullable()->comment('열람일시 (참조용)');
            $table->timestamps();

            $table->index('approval_id', 'idx_approval');
            $table->index(['approver_id', 'status'], 'idx_approver_status');
        });
    }

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