<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

/**
 * A driver now registers and owns multiple vehicles (only one of which is
 * "current" for a given shift, chosen when that shift starts — see
 * ShiftController::start()/ShiftService::blockingReason()) rather than the
 * previous one-vehicle-per-driver model this unique index enforced.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::table('vehicles', function (Blueprint $table) {
            // The unique index also backs driver_id's foreign key — MySQL
            // won't drop it until a replacement index exists, so add the
            // plain one first.
            $table->index('driver_id', 'vehicles_driver_id_index');
        });

        Schema::table('vehicles', function (Blueprint $table) {
            $table->dropUnique(['driver_id']);
        });
    }

    public function down(): void
    {
        Schema::table('vehicles', function (Blueprint $table) {
            $table->unique('driver_id');
        });

        Schema::table('vehicles', function (Blueprint $table) {
            $table->dropIndex('vehicles_driver_id_index');
        });
    }
};
