<?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('tab_columns', function (Blueprint $table) {
            $table->id()->comment('탭 컬럼 설정 ID');
            $table->unsignedBigInteger('tenant_id')->comment('테넌트 ID');
            $table->unsignedBigInteger('tab_id')->comment('탭 ID');
            $table->json('columns')->comment('컬럼 설정 [{"key": "name", "label": "품목명", "visible": true, "order": 0}]');
            $table->unsignedBigInteger('created_by')->nullable()->comment('생성자 ID');
            $table->unsignedBigInteger('updated_by')->nullable()->comment('수정자 ID');
            $table->timestamps();

            // 인덱스
            $table->unique(['tenant_id', 'tab_id'], 'uk_tab_columns_tenant_tab');

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

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