<?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_sections', function (Blueprint $table) {
            $table->id()->comment('섹션 인스턴스 ID');
            $table->unsignedBigInteger('tenant_id')->comment('테넌트 ID');
            $table->unsignedBigInteger('page_id')->comment('페이지 ID');
            $table->string('title')->comment('섹션 제목');
            $table->enum('type', ['fields', 'bom'])->default('fields')->comment('섹션 타입 (fields: 필드형, bom: BOM형)');
            $table->integer('order_no')->default(0)->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', 'page_id'], 'idx_item_sections_tenant_page');
            $table->index(['page_id', 'order_no'], 'idx_item_sections_order');

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

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