<?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_forms', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained()->onDelete('cascade')->comment('테넌트 ID');
            $table->string('name', 100)->comment('양식명');
            $table->string('code', 50)->comment('양식코드');
            $table->string('category', 50)->nullable()->comment('분류');
            $table->json('template')->comment('양식 템플릿 (필드 정의)');
            $table->boolean('is_active')->default(true)->comment('활성 여부');
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete()->comment('생성자');
            $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete()->comment('수정자');
            $table->foreignId('deleted_by')->nullable()->constrained('users')->nullOnDelete()->comment('삭제자');
            $table->softDeletes();
            $table->timestamps();

            $table->unique(['tenant_id', 'code'], 'uk_tenant_code');
            $table->index('tenant_id', 'idx_tenant');
        });
    }

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