<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('item_pages', function (Blueprint $table) {
            // source_table 컬럼 추가
            $table->string('source_table', 100)
                ->nullable()
                ->after('item_type')
                ->comment('실제 저장 테이블명 (products, materials 등)');

            // 인덱스
            $table->index('source_table', 'idx_source_table');
        });

        // 기존 데이터 업데이트: item_type 기반으로 source_table 설정
        DB::table('item_pages')
            ->whereIn('item_type', ['FG', 'PT'])
            ->update(['source_table' => 'products']);

        DB::table('item_pages')
            ->whereIn('item_type', ['SM', 'RM', 'CS'])
            ->update(['source_table' => 'materials']);
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('item_pages', function (Blueprint $table) {
            $table->dropIndex('idx_source_table');
            $table->dropColumn('source_table');
        });
    }
};
