<?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
    {
        if (Schema::hasTable('order_courier_shipments')) {
            Schema::table('order_courier_shipments', function (Blueprint $table) {
                $table->string('courier_status_code', 100)->nullable()->after('courier_status');
                $table->string('courier_status_text')->nullable()->after('courier_status_code');
                $table->string('courier_rider_id')->nullable()->after('courier_status_text');
                $table->string('courier_rider_name')->nullable()->after('courier_rider_id');
                $table->string('courier_rider_phone', 50)->nullable()->after('courier_rider_name');
                $table->string('courier_event_reference')->nullable()->after('courier_rider_phone');
                $table->timestamp('out_for_delivery_at')->nullable()->after('courier_event_reference')->index();
                $table->timestamp('out_for_delivery_sms_sent_at')->nullable()->after('out_for_delivery_at');
                $table->string('out_for_delivery_sms_status', 30)->nullable()->after('out_for_delivery_sms_sent_at');
                $table->unsignedTinyInteger('out_for_delivery_sms_attempts')->default(0)->after('out_for_delivery_sms_status');
                $table->timestamp('out_for_delivery_sms_last_attempt_at')->nullable()->after('out_for_delivery_sms_attempts');
                $table->text('out_for_delivery_sms_last_error')->nullable()->after('out_for_delivery_sms_last_attempt_at');
                $table->timestamp('out_for_delivery_sms_next_retry_at')->nullable()->after('out_for_delivery_sms_last_error')->index();
            });
        }

        if (! Schema::hasTable('courier_status_events')) {
            Schema::create('courier_status_events', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('order_id')->index();
                $table->unsignedBigInteger('order_courier_shipment_id')->index();
                $table->string('provider', 50)->index();
                $table->string('source', 50);
                $table->char('idempotency_key', 64)->unique();
                $table->string('courier_status_code', 100)->nullable();
                $table->string('courier_status_text')->nullable();
                $table->string('event_reference')->nullable();
                $table->string('rider_id')->nullable();
                $table->string('rider_name')->nullable();
                $table->string('rider_phone', 50)->nullable();
                $table->boolean('rider_confirmed')->default(false);
                $table->string('mapping_result', 100);
                $table->boolean('accepted')->default(false);
                $table->timestamp('event_occurred_at')->nullable();
                $table->timestamp('processed_at')->nullable();
                $table->timestamps();
            });
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('courier_status_events');

        if (! Schema::hasTable('order_courier_shipments')) {
            return;
        }

        $columns = collect([
            'courier_status_code',
            'courier_status_text',
            'courier_rider_id',
            'courier_rider_name',
            'courier_rider_phone',
            'courier_event_reference',
            'out_for_delivery_at',
            'out_for_delivery_sms_sent_at',
            'out_for_delivery_sms_status',
            'out_for_delivery_sms_attempts',
            'out_for_delivery_sms_last_attempt_at',
            'out_for_delivery_sms_last_error',
            'out_for_delivery_sms_next_retry_at',
        ])->filter(fn (string $column) => Schema::hasColumn('order_courier_shipments', $column))->all();

        if ($columns !== []) {
            Schema::table('order_courier_shipments', fn (Blueprint $table) => $table->dropColumn($columns));
        }
    }
};
