<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     * 
     * Add performance optimization indexes for legislation search
     */
    public function up(): void
    {
        Schema::table('legal_documents', function (Blueprint $table) {
            // Full-text index for fast searching on act names
            DB::statement('ALTER TABLE legal_documents ADD FULLTEXT INDEX legal_documents_act_name_fulltext (act_name)');
            
            // Composite index for common query patterns (status + subject + jurisdiction)
            $table->index(['status', 'law_subject_id', 'jurisdiction_id'], 'legal_documents_status_subject_jurisdiction_idx');
            
            // Index for table_name searching (also used in search)
            $table->index('table_name', 'legal_documents_table_name_idx');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('legal_documents', function (Blueprint $table) {
            // Drop full-text index
            DB::statement('ALTER TABLE legal_documents DROP INDEX legal_documents_act_name_fulltext');
            
            // Drop composite index
            $table->dropIndex('legal_documents_status_subject_jurisdiction_idx');
            
            // Drop table_name index
            $table->dropIndex('legal_documents_table_name_idx');
        });
    }
};
