<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

/**
 * Item Master 하이브리드 구조 전환 - Phase 1
 *
 * 목적: CASCADE FK 기반 계층 구조를 독립 엔티티 + 링크 테이블 구조로 전환
 *
 * 변경 내용:
 * 1. item_sections에서 page_id FK 제거 (컬럼은 유지, FK 제약만 제거)
 * 2. item_fields에서 section_id FK 제거
 * 3. item_bom_items에서 section_id FK 제거
 * 4. 모든 item 관련 테이블에 group_id 추가 (카테고리 격리용, 기본값 1 = 품목관리)
 */
return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        // 1. item_sections: page_id FK 제거 (컬럼 유지, FK만 제거)
        Schema::table('item_sections', function (Blueprint $table) {
            $table->dropForeign('fk_item_sections_page');
        });

        // 2. item_fields: section_id FK 제거 (컬럼 유지, FK만 제거)
        Schema::table('item_fields', function (Blueprint $table) {
            $table->dropForeign('fk_item_fields_section');
        });

        // 3. item_bom_items: section_id FK 제거 (컬럼 유지, FK만 제거)
        Schema::table('item_bom_items', function (Blueprint $table) {
            $table->dropForeign('fk_item_bom_items_section');
        });

        // 4. 모든 테이블에 group_id 추가 (기본값 1 = 품목관리)
        Schema::table('item_pages', function (Blueprint $table) {
            $table->unsignedInteger('group_id')->default(1)->after('tenant_id')->comment('그룹 ID (1: 품목관리)');
            $table->index(['tenant_id', 'group_id'], 'idx_item_pages_tenant_group');
        });

        Schema::table('item_sections', function (Blueprint $table) {
            $table->unsignedInteger('group_id')->default(1)->after('tenant_id')->comment('그룹 ID (1: 품목관리)');
            $table->index(['tenant_id', 'group_id'], 'idx_item_sections_tenant_group');
        });

        Schema::table('item_fields', function (Blueprint $table) {
            $table->unsignedInteger('group_id')->default(1)->after('tenant_id')->comment('그룹 ID (1: 품목관리)');
            $table->index(['tenant_id', 'group_id'], 'idx_item_fields_tenant_group');
        });

        Schema::table('item_bom_items', function (Blueprint $table) {
            $table->unsignedInteger('group_id')->default(1)->after('tenant_id')->comment('그룹 ID (1: 품목관리)');
            $table->index(['tenant_id', 'group_id'], 'idx_item_bom_items_tenant_group');
        });

        Schema::table('section_templates', function (Blueprint $table) {
            $table->unsignedInteger('group_id')->default(1)->after('tenant_id')->comment('그룹 ID (1: 품목관리)');
            $table->index(['tenant_id', 'group_id'], 'idx_section_templates_tenant_group');
        });

        Schema::table('item_master_fields', function (Blueprint $table) {
            $table->unsignedInteger('group_id')->default(1)->after('tenant_id')->comment('그룹 ID (1: 품목관리)');
            $table->index(['tenant_id', 'group_id'], 'idx_item_master_fields_tenant_group');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        // group_id 인덱스 및 컬럼 제거
        Schema::table('item_master_fields', function (Blueprint $table) {
            $table->dropIndex('idx_item_master_fields_tenant_group');
            $table->dropColumn('group_id');
        });

        Schema::table('section_templates', function (Blueprint $table) {
            $table->dropIndex('idx_section_templates_tenant_group');
            $table->dropColumn('group_id');
        });

        Schema::table('item_bom_items', function (Blueprint $table) {
            $table->dropIndex('idx_item_bom_items_tenant_group');
            $table->dropColumn('group_id');
        });

        Schema::table('item_fields', function (Blueprint $table) {
            $table->dropIndex('idx_item_fields_tenant_group');
            $table->dropColumn('group_id');
        });

        Schema::table('item_sections', function (Blueprint $table) {
            $table->dropIndex('idx_item_sections_tenant_group');
            $table->dropColumn('group_id');
        });

        Schema::table('item_pages', function (Blueprint $table) {
            $table->dropIndex('idx_item_pages_tenant_group');
            $table->dropColumn('group_id');
        });

        // FK 복원
        Schema::table('item_bom_items', function (Blueprint $table) {
            $table->foreign('section_id', 'fk_item_bom_items_section')
                ->references('id')->on('item_sections')
                ->onDelete('cascade');
        });

        Schema::table('item_fields', function (Blueprint $table) {
            $table->foreign('section_id', 'fk_item_fields_section')
                ->references('id')->on('item_sections')
                ->onDelete('cascade');
        });

        Schema::table('item_sections', function (Blueprint $table) {
            $table->foreign('page_id', 'fk_item_sections_page')
                ->references('id')->on('item_pages')
                ->onDelete('cascade');
        });
    }
};
