<?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_shipment_logs')) {
            Schema::create('order_courier_shipment_logs', function (Blueprint $table) {
                $table->id();
                $table->foreignId('order_courier_shipment_id')->nullable()->constrained('order_courier_shipments')->cascadeOnDelete();
                $table->foreignId('order_id')->constrained('orders')->cascadeOnDelete();
                $table->string('action', 30)->index();
                $table->string('provider', 50)->nullable()->index();
                $table->boolean('success')->default(false)->index();
                $table->json('request_payload')->nullable();
                $table->json('response_payload')->nullable();
                $table->unsignedSmallInteger('http_status')->nullable();
                $table->text('error_message')->nullable();
                $table->foreignId('triggered_by_admin_id')->nullable()->constrained('admins')->nullOnDelete();
                $table->timestamps();
            });
        }
    }

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