<?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
    {
        // ── sales ────────────────────────────────────────────────────────────
        // Reports and dashboard filter heavily on (org + date) and (org + status).
        // outstanding_balance is queried directly in the AR/reconciliation modules.
        Schema::table('sales', function (Blueprint $table) {
            $table->index(['organization_id', 'sale_date'],  'sales_org_date_idx');
            $table->index(['organization_id', 'status'],     'sales_org_status_idx');
            $table->index('outstanding_balance',             'sales_outstanding_idx');
        });

        // ── purchases ────────────────────────────────────────────────────────
        // Purchase reports and AP aging filter on (org + date) and (org + status).
        Schema::table('purchases', function (Blueprint $table) {
            $table->index(['organization_id', 'purchase_date'], 'purchases_org_date_idx');
            $table->index(['organization_id', 'status'],        'purchases_org_status_idx');
        });

        // ── products ─────────────────────────────────────────────────────────
        // Low-stock and active-product list queries use (org + is_active) and
        // (org + stock_quantity). Category-filtered product list uses (org + category_id).
        Schema::table('products', function (Blueprint $table) {
            $table->index(['organization_id', 'is_active'],     'products_org_active_idx');
            $table->index(['organization_id', 'stock_quantity'], 'products_org_stock_idx');
            $table->index(['organization_id', 'category_id'],   'products_org_category_idx');
        });

        // ── expenses ─────────────────────────────────────────────────────────
        // expense_date has no index at all; expense reports filter by (org + date).
        Schema::table('expenses', function (Blueprint $table) {
            $table->index('expense_date',                      'expenses_date_idx');
            $table->index(['organization_id', 'expense_date'], 'expenses_org_date_idx');
        });
    }

    public function down(): void
    {
        Schema::table('sales', function (Blueprint $table) {
            $table->dropIndex('sales_org_date_idx');
            $table->dropIndex('sales_org_status_idx');
            $table->dropIndex('sales_outstanding_idx');
        });

        Schema::table('purchases', function (Blueprint $table) {
            $table->dropIndex('purchases_org_date_idx');
            $table->dropIndex('purchases_org_status_idx');
        });

        Schema::table('products', function (Blueprint $table) {
            $table->dropIndex('products_org_active_idx');
            $table->dropIndex('products_org_stock_idx');
            $table->dropIndex('products_org_category_idx');
        });

        Schema::table('expenses', function (Blueprint $table) {
            $table->dropIndex('expenses_date_idx');
            $table->dropIndex('expenses_org_date_idx');
        });
    }
};
