<?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
    {
        // 1. Modificar tabla plates
        Schema::table('plates', function (Blueprint $table) {
            // Eliminar índice compuesto antiguo
            $table->dropIndex('plate_name'); 
            
            // Alterar la columna a CHAR(7) con collation binaria para velocidad
            $table->char('plate_name', 7)->collation('utf8mb4_bin')->change();

            // Crear el nuevo índice Único
            $table->unique('plate_name', 'idx_plate_name_unique');
        });

        // 2. Modificar tabla plates_demo
        Schema::table('plates_demo', function (Blueprint $table) {
            $table->char('plate_name', 7)->collation('utf8mb4_bin')->change();
            $table->unique('plate_name', 'idx_demo_plate_name_unique');
        });

        // 3. Modificar tabla uuids
        Schema::table('uuids', function (Blueprint $table) {
            $table->index('uuid', 'idx_uuids_uuid');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('uuids', function (Blueprint $table) {
            $table->dropIndex('idx_uuids_uuid');
        });

        Schema::table('plates_demo', function (Blueprint $table) {
            $table->dropUnique('idx_demo_plate_name_unique');
            // Revertir a VARCHAR 12
            $table->string('plate_name', 12)->collation('utf8mb4_unicode_ci')->change();
        });

        Schema::table('plates', function (Blueprint $table) {
            $table->dropUnique('idx_plate_name_unique');
            $table->string('plate_name', 12)->collation('utf8mb4_unicode_ci')->change();
            $table->index(['plate_name', 'plate_enable', 'plate_level'], 'plate_name');
        });
    }
};
