<?php

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

/**
 * The one query every catalog screen runs first — Pos::productsPayload() and
 * SalesOrderProductGrid::productsPayload() both do
 * `Product::where('is_active', true)->...->orderBy('name')->get()` — had no
 * index covering either its filter or its sort. `products` carries no index
 * on `is_active` at all (see 2026_05_30_000022_create_products_table.php:
 * only `sku` and `barcode` are indexed), so a catalog of any real size forced
 * a full table scan plus a filesort on every page render of the POS terminal
 * and the sales-order product grid — companion to
 * 2026_08_22_000001_add_pos_performance_indexes.php, which covers every
 * downstream lot/price/serial query but not this base one.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::table('products', function (Blueprint $table) {
            $table->index(['is_active', 'name'], 'idx_products_active_name');
        });
    }

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