<?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('orders', function (Blueprint $table) {
            // Most common listing filters
            $table->index(['store_id', 'status', 'created_at'], 'orders_store_status_date_idx');
            $table->index(['store_id', 'type', 'created_at'], 'orders_store_type_date_idx');
            $table->index(['customer_id', 'created_at'], 'orders_customer_date_idx');
            $table->index('status', 'orders_status_idx');
            $table->index('payment_status', 'orders_payment_status_idx');
            $table->index('created_at', 'orders_created_at_idx');
            // Stats query
            $table->index(['store_id', 'created_at', 'status'], 'orders_stats_idx');
        });

        Schema::table('order_items', function (Blueprint $table) {
            $table->index('order_id', 'order_items_order_idx');
            $table->index('product_id', 'order_items_product_idx');
        });

        Schema::table('stock', function (Blueprint $table) {
            $table->index('store_id', 'stock_store_idx');
            $table->index('quantity', 'stock_quantity_idx');
        });
    }

    public function down(): void
    {
        Schema::table('orders', function (Blueprint $table) {
            $table->dropIndex('orders_store_status_date_idx');
            $table->dropIndex('orders_store_type_date_idx');
            $table->dropIndex('orders_customer_date_idx');
            $table->dropIndex('orders_status_idx');
            $table->dropIndex('orders_payment_status_idx');
            $table->dropIndex('orders_created_at_idx');
            $table->dropIndex('orders_stats_idx');
        });

        Schema::table('order_items', function (Blueprint $table) {
            $table->dropIndex('order_items_order_idx');
            $table->dropIndex('order_items_product_idx');
        });

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