<?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
    {
        // Add indexes to products table
        Schema::table('products', function (Blueprint $table) {
            if (Schema::hasColumn('products', 'seller_id')) $table->index('seller_id', 'products_seller_id_index');
            if (Schema::hasColumn('products', 'category_id')) $table->index('category_id', 'products_category_id_index');
            if (Schema::hasColumn('products', 'status')) $table->index('status', 'products_status_index');
        });

        // Add indexes to rfqs table
        Schema::table('rfqs', function (Blueprint $table) {
            if (Schema::hasColumn('rfqs', 'buyer_id')) $table->index('buyer_id', 'rfqs_buyer_id_index');
            if (Schema::hasColumn('rfqs', 'status')) $table->index('status', 'rfqs_status_index');
            if (Schema::hasColumn('rfqs', 'created_at')) $table->index('created_at', 'rfqs_created_at_index');
        });

        // Add indexes to quotes table if it exists
        if (Schema::hasTable('quotes')) {
            Schema::table('quotes', function (Blueprint $table) {
                if (Schema::hasColumn('quotes', 'rfq_id')) $table->index('rfq_id', 'quotes_rfq_id_index');
                if (Schema::hasColumn('quotes', 'seller_id')) $table->index('seller_id', 'quotes_seller_id_index');
                if (Schema::hasColumn('quotes', 'status')) $table->index('status', 'quotes_status_index');
            });
        }

        // Add indexes to product_promotions table
        Schema::table('product_promotions', function (Blueprint $table) {
            if (Schema::hasColumn('product_promotions', 'product_id')) $table->index('product_id', 'product_promotions_product_id_index');
            if (Schema::hasColumn('product_promotions', 'seller_id')) $table->index('seller_id', 'product_promotions_seller_id_index');
             if (Schema::hasColumn('product_promotions', 'status')) $table->index('status', 'product_promotions_status_index');
             if (Schema::hasColumn('product_promotions', 'start_date') && Schema::hasColumn('product_promotions', 'end_date')) {
                 $table->index(['start_date', 'end_date'], 'product_promotions_dates_index');
             }
        });

        // Add indexes to transactions table
        Schema::table('transactions', function (Blueprint $table) {
            if (Schema::hasColumn('transactions', 'seller_id')) $table->index('seller_id', 'transactions_seller_id_index');
            if (Schema::hasColumn('transactions', 'transaction_type')) $table->index('transaction_type', 'transactions_type_index');
            if (Schema::hasColumn('transactions', 'status')) $table->index('status', 'transactions_status_index');
            if (Schema::hasColumn('transactions', 'created_at')) $table->index('created_at', 'transactions_created_at_index');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        // Remove indexes from products table
        Schema::table('products', function (Blueprint $table) {
            $table->dropIndex('products_seller_id_index');
            $table->dropIndex('products_category_id_index');
            $table->dropIndex('products_status_index');
        });

        // Remove indexes from rfqs table
        Schema::table('rfqs', function (Blueprint $table) {
            $table->dropIndex('rfqs_buyer_id_index');
            $table->dropIndex('rfqs_status_index');
            $table->dropIndex('rfqs_created_at_index');
        });

        // Remove indexes from quotes table if it exists
        if (Schema::hasTable('quotes')) {
            Schema::table('quotes', function (Blueprint $table) {
                $table->dropIndex('quotes_rfq_id_index');
                $table->dropIndex('quotes_seller_id_index');
                $table->dropIndex('quotes_status_index');
            });
        }

        // Remove indexes from product_promotions table
        Schema::table('product_promotions', function (Blueprint $table) {
            $table->dropIndex('product_promotions_product_id_index');
            $table->dropIndex('product_promotions_seller_id_index');
            $table->dropIndex('product_promotions_status_index');
            $table->dropIndex('product_promotions_dates_index');
        });

        // Remove indexes from transactions table
        Schema::table('transactions', function (Blueprint $table) {
            $table->dropIndex('transactions_seller_id_index');
            $table->dropIndex('transactions_type_index');
            $table->dropIndex('transactions_status_index');
            $table->dropIndex('transactions_created_at_index');
        });
    }
};
