<?php

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

/**
 * Phase 5.5: 레거시 product_id/material_id 컬럼 삭제
 *
 * 전제조건:
 * - items 테이블로 데이터 마이그레이션 완료
 * - item_id 컬럼에 데이터 매핑 완료
 * - products/materials 테이블 삭제됨
 */
return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        // 1. orders.product_id 삭제
        if (Schema::hasTable('orders') && Schema::hasColumn('orders', 'product_id')) {
            Schema::table('orders', function (Blueprint $table) {
                $table->dropColumn('product_id');
            });
        }

        // 2. order_items.product_id 삭제
        if (Schema::hasTable('order_items') && Schema::hasColumn('order_items', 'product_id')) {
            Schema::table('order_items', function (Blueprint $table) {
                $table->dropColumn('product_id');
            });
        }

        // 3. quotes.product_id 삭제
        if (Schema::hasTable('quotes') && Schema::hasColumn('quotes', 'product_id')) {
            Schema::table('quotes', function (Blueprint $table) {
                $table->dropColumn('product_id');
            });
        }

        // 4. material_receipts.material_id 삭제
        if (Schema::hasTable('material_receipts') && Schema::hasColumn('material_receipts', 'material_id')) {
            Schema::table('material_receipts', function (Blueprint $table) {
                $table->dropColumn('material_id');
            });
        }

        // 5. lots.material_id 삭제
        if (Schema::hasTable('lots') && Schema::hasColumn('lots', 'material_id')) {
            Schema::table('lots', function (Blueprint $table) {
                $table->dropColumn('material_id');
            });
        }

        DB::statement("SELECT 'Dropped legacy product_id/material_id columns' AS result");
    }

    /**
     * Reverse the migrations.
     *
     * 주의: 컬럼 복원만 수행, 데이터는 item_id_mappings로 복원 필요
     */
    public function down(): void
    {
        // 1. orders.product_id 복원
        if (Schema::hasTable('orders') && ! Schema::hasColumn('orders', 'product_id')) {
            Schema::table('orders', function (Blueprint $table) {
                $table->unsignedBigInteger('product_id')->nullable()->after('category_code')
                    ->comment('(deprecated) 기존 products 참조');
            });
        }

        // 2. order_items.product_id 복원
        if (Schema::hasTable('order_items') && ! Schema::hasColumn('order_items', 'product_id')) {
            Schema::table('order_items', function (Blueprint $table) {
                $table->unsignedBigInteger('product_id')->nullable()->after('serial_no')
                    ->comment('(deprecated) 기존 products 참조');
            });
        }

        // 3. quotes.product_id 복원
        if (Schema::hasTable('quotes') && ! Schema::hasColumn('quotes', 'product_id')) {
            Schema::table('quotes', function (Blueprint $table) {
                $table->unsignedBigInteger('product_id')->nullable()->after('product_category')
                    ->comment('(deprecated) 기존 products 참조');
            });
        }

        // 4. material_receipts.material_id 복원
        if (Schema::hasTable('material_receipts') && ! Schema::hasColumn('material_receipts', 'material_id')) {
            Schema::table('material_receipts', function (Blueprint $table) {
                $table->unsignedBigInteger('material_id')->nullable()->first()
                    ->comment('(deprecated) 기존 materials 참조');
            });
        }

        // 5. lots.material_id 복원
        if (Schema::hasTable('lots') && ! Schema::hasColumn('lots', 'material_id')) {
            Schema::table('lots', function (Blueprint $table) {
                $table->unsignedBigInteger('material_id')->nullable()->after('id')
                    ->comment('(deprecated) 기존 materials 참조');
            });
        }

        // 데이터 복원 안내
        DB::statement("SELECT 'Columns restored. Use item_id_mappings to restore data if needed.' AS warning");
    }
};
