<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('approval_delegations', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained()->onDelete('cascade');
            $table->foreignId('delegator_id')->constrained('users')->comment('위임자');
            $table->foreignId('delegate_id')->constrained('users')->comment('대리인');
            $table->date('start_date')->comment('시작일');
            $table->date('end_date')->comment('종료일');
            $table->json('form_ids')->nullable()->comment('위임 대상 양식 (NULL=전체)');
            $table->boolean('notify_delegator')->default(true)->comment('대결 시 보고');
            $table->boolean('is_active')->default(true);
            $table->string('reason', 200)->nullable()->comment('위임 사유');
            $table->foreignId('created_by')->nullable()->constrained('users');
            $table->softDeletes();
            $table->timestamps();

            $table->index(['tenant_id', 'delegator_id', 'is_active']);
        });
    }

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