<?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
    {
        Schema::table('deliveries', function (Blueprint $table) {
            $table->foreignUuid('origem_referencia_id')->nullable()->after('cliente_id')->constrained('locations')->nullOnDelete();
            $table->foreignUuid('destino_referencia_id')->nullable()->after('origem_referencia_id')->constrained('locations')->nullOnDelete();
            $table->decimal('origem_lat', 10, 7)->nullable()->after('origem');
            $table->decimal('origem_lng', 10, 7)->nullable()->after('origem_lat');
            $table->decimal('destino_lat', 10, 7)->nullable()->after('destino');
            $table->decimal('destino_lng', 10, 7)->nullable()->after('destino_lat');
            $table->decimal('distancia_km', 8, 2)->nullable()->after('destino_lng');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('deliveries', function (Blueprint $table) {
            $table->dropConstrainedForeignId('origem_referencia_id');
            $table->dropConstrainedForeignId('destino_referencia_id');
            $table->dropColumn(['origem_lat', 'origem_lng', 'destino_lat', 'destino_lng', 'distancia_km']);
        });
    }
};
