<?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('item_master_fields', function (Blueprint $table) {
            $table->id()->comment('마스터 필드 ID');
            $table->unsignedBigInteger('tenant_id')->comment('테넌트 ID');
            $table->string('field_name')->comment('필드명');
            $table->enum('field_type', ['textbox', 'number', 'dropdown', 'checkbox', 'date', 'textarea'])->comment('필드 타입');
            $table->string('category', 100)->nullable()->comment('카테고리');
            $table->text('description')->nullable()->comment('설명');
            $table->boolean('is_common')->default(false)->comment('공통 필드 여부');
            $table->text('default_value')->nullable()->comment('기본값');
            $table->json('options')->nullable()->comment('드롭다운 옵션 [{"label": "옵션1", "value": "val1"}]');
            $table->json('validation_rules')->nullable()->comment('검증 규칙 {"min": 0, "max": 100, "pattern": "regex"}');
            $table->json('properties')->nullable()->comment('필드 속성 {"unit": "mm", "precision": 2, "format": "YYYY-MM-DD"}');
            $table->unsignedBigInteger('created_by')->nullable()->comment('생성자 ID');
            $table->unsignedBigInteger('updated_by')->nullable()->comment('수정자 ID');
            $table->unsignedBigInteger('deleted_by')->nullable()->comment('삭제자 ID');
            $table->timestamps();
            $table->softDeletes()->comment('소프트 삭제');

            // 인덱스
            $table->index('tenant_id', 'idx_item_master_fields_tenant_id');
            $table->index('category', 'idx_item_master_fields_category');

            // 외래키
            $table->foreign('tenant_id', 'fk_item_master_fields_tenant')
                ->references('id')->on('tenants')
                ->onDelete('cascade');
        });
    }

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