<?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('order_courier_shipments', function (Blueprint $table) {
            if (!Schema::hasColumn('order_courier_shipments', 'cod_received_at')) {
                $table->timestamp('cod_received_at')->nullable()->after('courier_synced_at');
            }
            if (!Schema::hasColumn('order_courier_shipments', 'cod_received_by_admin_id')) {
                $table->foreignId('cod_received_by_admin_id')->nullable()->after('cod_received_at')
                    ->constrained('admins')->nullOnDelete();
            }
            if (!Schema::hasColumn('order_courier_shipments', 'cod_note')) {
                $table->string('cod_note')->nullable()->after('cod_received_by_admin_id');
            }
        });
    }

    public function down(): void
    {
        Schema::table('order_courier_shipments', function (Blueprint $table) {
            $table->dropColumn(['cod_received_at', 'cod_note']);
            $table->dropConstrainedForeignId('cod_received_by_admin_id');
        });
    }
};
