<?php

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

/**
 * Indexes for the pickers on the Sales Order form.
 *
 * Both of these back an "active rows, ordered by name, first N" lookup that the
 * form runs on every render. The existing single-column name indexes cannot
 * serve them without a filesort once tenant and active are also in the
 * predicate, which is what made the customer and product pickers feel slow to
 * populate on a real catalog.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::table('customers', function (Blueprint $table) {
            $table->index(['tenant_id', 'is_active', 'business_name'], 'idx_customers_tenant_active_name');
        });
    }

    public function down(): void
    {
        Schema::table('customers', function (Blueprint $table) {
            $table->dropIndex('idx_customers_tenant_active_name');
        });
    }
};
