<?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_collection_status')) {
                $table->string('cod_collection_status', 100)->nullable()->after('collectable_amount');
            }

            if (! Schema::hasColumn('order_courier_shipments', 'cod_collected_amount')) {
                $table->decimal('cod_collected_amount', 24, 8)->nullable()->after('cod_collection_status');
            }

            if (! Schema::hasColumn('order_courier_shipments', 'cod_collected_at')) {
                $table->timestamp('cod_collected_at')->nullable()->after('cod_collected_amount');
            }

            if (! Schema::hasColumn('order_courier_shipments', 'settlement_status')) {
                $table->string('settlement_status', 100)->nullable()->after('cod_note');
            }

            if (! Schema::hasColumn('order_courier_shipments', 'settled_amount')) {
                $table->decimal('settled_amount', 24, 8)->nullable()->after('settlement_status');
            }

            if (! Schema::hasColumn('order_courier_shipments', 'settlement_paid_at')) {
                $table->timestamp('settlement_paid_at')->nullable()->after('settled_amount');
            }

            if (! Schema::hasColumn('order_courier_shipments', 'settlement_reference')) {
                $table->string('settlement_reference', 191)->nullable()->after('settlement_paid_at')->index();
            }

            if (! Schema::hasColumn('order_courier_shipments', 'settlement_payload')) {
                $table->json('settlement_payload')->nullable()->after('settlement_reference');
            }

            if (! Schema::hasColumn('order_courier_shipments', 'courier_accounting_synced_at')) {
                $table->timestamp('courier_accounting_synced_at')->nullable()->after('settlement_payload');
            }

            if (! Schema::hasColumn('order_courier_shipments', 'courier_accounting_note')) {
                $table->string('courier_accounting_note')->nullable()->after('courier_accounting_synced_at');
            }
        });
    }

    public function down(): void
    {
        Schema::table('order_courier_shipments', function (Blueprint $table) {
            foreach ([
                'cod_collection_status',
                'cod_collected_amount',
                'cod_collected_at',
                'settlement_status',
                'settled_amount',
                'settlement_paid_at',
                'settlement_payload',
                'courier_accounting_synced_at',
                'courier_accounting_note',
            ] as $column) {
                if (Schema::hasColumn('order_courier_shipments', $column)) {
                    $table->dropColumn($column);
                }
            }

            if (Schema::hasColumn('order_courier_shipments', 'settlement_reference')) {
                $table->dropIndex(['settlement_reference']);
                $table->dropColumn('settlement_reference');
            }
        });
    }
};
