<?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('section_templates', function (Blueprint $table) {
            $table->id()->comment('섹션 템플릿 ID');
            $table->unsignedBigInteger('tenant_id')->comment('테넌트 ID');
            $table->string('title')->comment('템플릿명');
            $table->enum('type', ['fields', 'bom'])->default('fields')->comment('섹션 타입 (fields: 필드형, bom: BOM형)');
            $table->text('description')->nullable()->comment('설명');
            $table->boolean('is_default')->default(false)->comment('기본 템플릿 여부');
            $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_section_templates_tenant_id');

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

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