<?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
    {
        // MySQL may use one of the old composite unique indexes to support the
        // tenant foreign key. Give that foreign key its own index before the
        // unique indexes are removed.
        Schema::whenTableDoesntHaveIndex(
            'carritos',
            'carritos_tenant_codigo_index',
            fn (Blueprint $table) => $table->index(
                'tenant_codigo',
                'carritos_tenant_codigo_index',
            ),
        );

        Schema::whenTableHasIndex(
            'carritos',
            'carritos_tenant_codigo_user_id_unique',
            fn (Blueprint $table) => $table->dropUnique(
                'carritos_tenant_codigo_user_id_unique',
            ),
        );
        Schema::whenTableHasIndex(
            'carritos',
            'carritos_tenant_codigo_guest_token_unique',
            fn (Blueprint $table) => $table->dropUnique(
                'carritos_tenant_codigo_guest_token_unique',
            ),
        );

        Schema::whenTableDoesntHaveColumn('carritos', 'active_user_id', function (Blueprint $table): void {
            $table->unsignedBigInteger('active_user_id')
                ->virtualAs("CASE WHEN status = 'active' AND deleted_at IS NULL THEN user_id ELSE NULL END");
        });
        Schema::whenTableDoesntHaveColumn('carritos', 'active_guest_token', function (Blueprint $table): void {
            $table->string('active_guest_token')
                ->virtualAs("CASE WHEN status = 'active' AND deleted_at IS NULL THEN guest_token ELSE NULL END");
        });

        Schema::whenTableDoesntHaveIndex(
            'carritos',
            'carritos_tenant_active_user_unique',
            fn (Blueprint $table) => $table->unique(
                ['tenant_codigo', 'active_user_id'],
                'carritos_tenant_active_user_unique',
            ),
        );
        Schema::whenTableDoesntHaveIndex(
            'carritos',
            'carritos_tenant_active_guest_unique',
            fn (Blueprint $table) => $table->unique(
                ['tenant_codigo', 'active_guest_token'],
                'carritos_tenant_active_guest_unique',
            ),
        );
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::whenTableHasIndex(
            'carritos',
            'carritos_tenant_active_user_unique',
            fn (Blueprint $table) => $table->dropUnique(
                'carritos_tenant_active_user_unique',
            ),
        );
        Schema::whenTableHasIndex(
            'carritos',
            'carritos_tenant_active_guest_unique',
            fn (Blueprint $table) => $table->dropUnique(
                'carritos_tenant_active_guest_unique',
            ),
        );

        Schema::whenTableHasColumn(
            'carritos',
            'active_user_id',
            fn (Blueprint $table) => $table->dropColumn('active_user_id'),
        );
        Schema::whenTableHasColumn(
            'carritos',
            'active_guest_token',
            fn (Blueprint $table) => $table->dropColumn('active_guest_token'),
        );

        Schema::whenTableDoesntHaveIndex(
            'carritos',
            'carritos_tenant_codigo_user_id_unique',
            fn (Blueprint $table) => $table->unique(['tenant_codigo', 'user_id']),
        );
        Schema::whenTableDoesntHaveIndex(
            'carritos',
            'carritos_tenant_codigo_guest_token_unique',
            fn (Blueprint $table) => $table->unique(['tenant_codigo', 'guest_token']),
        );
        Schema::whenTableHasIndex(
            'carritos',
            'carritos_tenant_codigo_index',
            fn (Blueprint $table) => $table->dropIndex('carritos_tenant_codigo_index'),
        );
    }
};
