<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        // 最直接的做法：暫時關閉外鍵檢查，然後強制刪除報錯的索引
        DB::statement('SET FOREIGN_KEY_CHECKS=0;');
        
        try {
            // 直接下達 SQL 指令刪除索引
            DB::statement('ALTER TABLE inventories DROP INDEX warehouse_product_batch_unique;');
        } catch (\Exception $e) {
            // 索引不存在則跳過
        }

        DB::statement('SET FOREIGN_KEY_CHECKS=1;');
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        DB::statement('SET FOREIGN_KEY_CHECKS=0;');
        
        DB::statement('ALTER TABLE inventories ADD UNIQUE INDEX warehouse_product_batch_unique (warehouse_id, product_id, batch_number);');

        DB::statement('SET FOREIGN_KEY_CHECKS=1;');
    }
};
