<?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::create('envios', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('empresa_id')->index();
            $table->string('numero_envio', 30)->index();
            $table->unsignedBigInteger('cliente_id')->nullable();
            $table->unsignedBigInteger('pedido_manychat_id')->nullable();

            // Remitente
            $table->string('remitente_nombre', 150);
            $table->string('remitente_telefono', 30)->nullable();
            $table->string('remitente_direccion', 255)->nullable();
            $table->string('remitente_ciudad', 100)->nullable();

            // Destinatario
            $table->string('destinatario_nombre', 150);
            $table->string('destinatario_telefono', 30)->nullable();
            $table->string('destinatario_direccion', 255);
            $table->string('destinatario_ciudad', 100);
            $table->string('destinatario_departamento', 100)->nullable();
            $table->string('destinatario_pais', 60)->default('Colombia');

            // Contenido
            $table->string('descripcion_contenido', 255);
            $table->decimal('peso_kg', 8, 3)->default(0);
            $table->json('dimensiones')->nullable(); // {largo, ancho, alto} en cm
            $table->decimal('valor_declarado', 12, 2)->nullable();

            // Logística
            $table->string('transportadora', 100)->nullable();
            $table->string('numero_guia', 100)->nullable()->index();

            // Estado
            $table->enum('estado', [
                'pendiente',
                'en_preparacion',
                'listo_para_despacho',
                'despachado',
                'en_transito',
                'entregado',
                'devuelto',
            ])->default('pendiente')->index();

            $table->text('observaciones')->nullable();
            $table->unsignedBigInteger('created_by')->nullable();
            $table->timestamps();

            $table->unique(['empresa_id', 'numero_envio']);
            $table->foreign('empresa_id')->references('id')->on('empresas')->cascadeOnDelete();
            $table->foreign('cliente_id')->references('id')->on('clientes')->nullOnDelete();
            $table->foreign('pedido_manychat_id')->references('id')->on('manychat_pedidos')->nullOnDelete();
        });

        Schema::create('envio_historial', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('envio_id')->index();
            $table->string('estado_anterior', 30)->nullable();
            $table->string('estado_nuevo', 30);
            $table->string('nota', 255)->nullable();
            $table->unsignedBigInteger('usuario_id')->nullable();
            $table->timestamp('created_at')->useCurrent();

            $table->foreign('envio_id')->references('id')->on('envios')->cascadeOnDelete();
        });
    }

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