<?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('financial_account_transactions', function (Blueprint $table): void {
            $table->index(['branch_id', 'type', 'transaction_date'], 'fat_branch_type_date_idx');
        });

        Schema::table('accounting_entries', function (Blueprint $table): void {
            $table->index(['branch_id', 'category', 'entry_date'], 'acct_branch_category_date_idx');
        });

        Schema::table('loans', function (Blueprint $table): void {
            $table->index(['branch_id', 'status', 'completed_at'], 'loans_branch_status_completed_idx');
            $table->index(['branch_id', 'status', 'written_off_at'], 'loans_branch_status_written_idx');
            $table->index(['branch_id', 'status', 'total_overdue'], 'loans_branch_status_overdue_idx');
        });

        Schema::table('customers', function (Blueprint $table): void {
            $table->index(['branch_id', 'created_at'], 'customers_branch_created_idx');
        });
    }

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

        Schema::table('loans', function (Blueprint $table): void {
            $table->dropIndex('loans_branch_status_completed_idx');
            $table->dropIndex('loans_branch_status_written_idx');
            $table->dropIndex('loans_branch_status_overdue_idx');
        });

        Schema::table('accounting_entries', function (Blueprint $table): void {
            $table->dropIndex('acct_branch_category_date_idx');
        });

        Schema::table('financial_account_transactions', function (Blueprint $table): void {
            $table->dropIndex('fat_branch_type_date_idx');
        });
    }
};
