<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('products', function (Blueprint $table) {
            $table->index(['is_active', 'created_at'], 'products_active_created_idx');
            $table->index(['is_active', 'is_featured'], 'products_active_featured_idx');
            $table->index(['is_active', 'is_new'], 'products_active_new_idx');
            $table->index(['category_id', 'is_active'], 'products_category_active_idx');
            $table->index(['brand_id', 'is_active'], 'products_brand_active_idx');
            $table->index('price', 'products_price_idx');
            $table->index('views_count', 'products_views_idx');
        });

        Schema::table('product_variations', function (Blueprint $table) {
            $table->index('product_id', 'variations_product_idx');
        });

        Schema::table('stock', function (Blueprint $table) {
            $table->index(['product_id', 'store_id'], 'stock_product_store_idx');
            $table->index('variant_id', 'stock_variant_idx');
        });
    }

    public function down(): void
    {
        Schema::table('products', function (Blueprint $table) {
            $table->dropIndex('products_active_created_idx');
            $table->dropIndex('products_active_featured_idx');
            $table->dropIndex('products_active_new_idx');
            $table->dropIndex('products_category_active_idx');
            $table->dropIndex('products_brand_active_idx');
            $table->dropIndex('products_price_idx');
            $table->dropIndex('products_views_idx');
        });

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

        Schema::table('stock', function (Blueprint $table) {
            $table->dropIndex('stock_product_store_idx');
            $table->dropIndex('stock_variant_idx');
        });
    }
};
