<?php

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

return new class extends Migration
{
    /**
     * 在調撥單中新增在途倉庫相關欄位
     */
    public function up(): void
    {
        Schema::table('inventory_transfer_orders', function (Blueprint $table) {
            // 在途倉庫（可選）
            $table->foreignId('transit_warehouse_id')
                ->nullable()
                ->after('to_warehouse_id')
                ->constrained('warehouses')
                ->nullOnDelete();

            // 出貨資訊
            $table->timestamp('dispatched_at')->nullable()->after('posted_at');
            $table->foreignId('dispatched_by')->nullable()->after('dispatched_at')->constrained('users')->nullOnDelete();

            // 收貨確認資訊
            $table->timestamp('received_at')->nullable()->after('dispatched_by');
            $table->foreignId('received_by')->nullable()->after('received_at')->constrained('users')->nullOnDelete();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('inventory_transfer_orders', function (Blueprint $table) {
            $table->dropForeign(['transit_warehouse_id']);
            $table->dropForeign(['dispatched_by']);
            $table->dropForeign(['received_by']);
            $table->dropColumn([
                'transit_warehouse_id',
                'dispatched_at',
                'dispatched_by',
                'received_at',
                'received_by',
            ]);
        });
    }
};
