<?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
    {
        Schema::table('inventories', function (Blueprint $table) {
            // 不刪除舊索引（以免外鍵報錯），直接建立新的、更精確的唯一索引
            // 這樣「商品+批號+貨道」的組合就會被視為唯一，達成多貨道支援
            $table->unique(['warehouse_id', 'product_id', 'batch_number', 'location'], 'warehouse_product_batch_location_unique');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('inventories', function (Blueprint $table) {
            $table->dropUnique('warehouse_product_batch_location_unique');
        });
    }
};
