<?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('document_template_section_fields', function (Blueprint $table) {
            $table->id();
            $table->foreignId('template_id')->constrained('document_templates')->cascadeOnDelete();
            $table->string('field_key', 50)->comment('내부 키 (category, item, standard 등)');
            $table->string('label', 50)->comment('표시명 (구분, 검사항목 등)');
            $table->string('field_type', 30)->default('text')
                ->comment('text|number|select|select_api|json_tolerance|json_criteria|composite_frequency');
            $table->json('options')->nullable()->comment('선택지, API경로 등 부가 설정');
            $table->string('width', 20)->default('100px')->comment('에디터 UI 컬럼 너비');
            $table->boolean('is_required')->default(false);
            $table->unsignedSmallInteger('sort_order')->default(0);
            $table->timestamps();

            $table->index(['template_id', 'sort_order'], 'idx_template_sort');
        });
    }

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