<?php

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

return new class extends Migration
{
    /**
     * Add indexes for frequent filters and joins to keep queries fast at scale.
     */
    public function up(): void
    {
        Schema::table('visits', function (Blueprint $table) {
            $table->index('visit_date');
            $table->index('import_batch_id');
        });

        Schema::table('visits_raw_staging', function (Blueprint $table) {
            $table->index(['import_batch_id', 'resolved_mr_id']);
            $table->index(['import_batch_id', 'match_confidence']);
        });

        Schema::table('mapping_decisions', function (Blueprint $table) {
            $table->index('staging_row_id');
        });
    }

    public function down(): void
    {
        Schema::table('visits', function (Blueprint $table) {
            $table->dropIndex(['visit_date']);
            $table->dropIndex(['import_batch_id']);
        });
        Schema::table('visits_raw_staging', function (Blueprint $table) {
            $table->dropIndex(['import_batch_id', 'resolved_mr_id']);
            $table->dropIndex(['import_batch_id', 'match_confidence']);
        });
        Schema::table('mapping_decisions', function (Blueprint $table) {
            $table->dropIndex(['staging_row_id']);
        });
    }
};
