<?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::hasTable('shipments')) {
            return;
        }
        
        Schema::create('shipments', function (Blueprint $table) {
            $table->id();
            $table->foreignId('order_id')->constrained()->onDelete('cascade');
            $table->foreignId('carrier_id')->constrained()->onDelete('cascade');
            $table->string('tracking_id')->nullable();
            $table->enum('status', ['pending', 'created', 'failed', 'delivered'])->default('pending');
            $table->string('label_url')->nullable();
            $table->json('raw_response')->nullable(); // Raw API response
            $table->json('metadata')->nullable(); // Carrier-specific data
            $table->timestamps();

            $table->index('order_id');
            $table->index('carrier_id');
            $table->index('tracking_id');
            $table->index('status');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('shipments');
    }
};
