<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        if (!Schema::hasColumn('warehouses', 'default_transit_warehouse_id')) {
            Schema::table('warehouses', function (Blueprint $table) {
                $table->foreignId('default_transit_warehouse_id')
                    ->nullable()
                    ->after('driver_name')
                    ->comment('預設使用的在途倉（物流車）')
                    ->constrained('warehouses')
                    ->nullOnDelete();
            });
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('warehouses', function (Blueprint $table) {
            $table->dropForeign(['default_transit_warehouse_id']);
            $table->dropColumn('default_transit_warehouse_id');
        });
    }
};
