<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * item_pages.source_table을 'products'/'materials'에서 'items'로 통합
     * - item_type 컬럼은 유지 (FG, PT, SM, RM, CS로 구분)
     */
    public function up(): void
    {
        // products -> items 업데이트
        $productsUpdated = DB::table('item_pages')
            ->where('source_table', 'products')
            ->update(['source_table' => 'items']);

        // materials -> items 업데이트
        $materialsUpdated = DB::table('item_pages')
            ->where('source_table', 'materials')
            ->update(['source_table' => 'items']);

        // 로그 출력
        if ($productsUpdated > 0 || $materialsUpdated > 0) {
            echo "Updated item_pages.source_table: products({$productsUpdated}), materials({$materialsUpdated}) -> items\n";
        }
    }

    /**
     * Reverse the migrations.
     *
     * item_type 기준으로 source_table 복원
     */
    public function down(): void
    {
        // Product 타입 (FG, PT) -> products
        DB::table('item_pages')
            ->where('source_table', 'items')
            ->whereIn('item_type', ['FG', 'PT'])
            ->update(['source_table' => 'products']);

        // Material 타입 (SM, RM, CS) -> materials
        DB::table('item_pages')
            ->where('source_table', 'items')
            ->whereIn('item_type', ['SM', 'RM', 'CS'])
            ->update(['source_table' => 'materials']);
    }
};
