<?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
    {
        Schema::table('tasks', function (Blueprint $table) {
            // Add composite index for dashboard performance queries
            // This optimizes queries that filter by type, completed status, assigned user, and completion date
            $table->index(['type', 'completed', 'assigned_to', 'completed_at'], 'idx_tasks_performance');
            
            // Add index for completed_at field (used in date range queries)
            $table->index('completed_at', 'idx_tasks_completed_at');
            
            // Add composite index for type and completed status
            $table->index(['type', 'completed'], 'idx_tasks_type_completed');
            
            // Add index for client labour calculations (HelpersController optimization)
            $table->index(['completed', 'completed_at', 'client_id', 'client_type', 'labour'], 'idx_tasks_client_labour');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('tasks', function (Blueprint $table) {
            // Drop the performance indexes
            $table->dropIndex('idx_tasks_performance');
            $table->dropIndex('idx_tasks_completed_at');
            $table->dropIndex('idx_tasks_type_completed');
            $table->dropIndex('idx_tasks_client_labour');
        });
    }
};
