<?php

declare(strict_types=1);

namespace App\Framework\Discovery\Enhanced\ValueObjects;

/**
 * Discovery Plan Strategy enumeration
 *
 * Defines different approaches to discovery execution.
 */
enum PlanStrategy: string
{
    case PERFORMANCE_OPTIMIZED = 'performance_optimized';
    case COMPREHENSIVE = 'comprehensive';
    case MINIMAL = 'minimal';
    case MEMORY_EFFICIENT = 'memory_efficient';
    case CACHE_FIRST = 'cache_first';
    case REAL_TIME = 'real_time';

    /**
     * Get strategy description
     */
    public function getDescription(): string
    {
        return match ($this) {
            self::PERFORMANCE_OPTIMIZED => 'Optimized for maximum performance with intelligent caching',
            self::COMPREHENSIVE => 'Thorough discovery with complete analysis and dependency mapping',
            self::MINIMAL => 'Minimal discovery with basic component detection',
            self::MEMORY_EFFICIENT => 'Memory-optimized discovery with streaming and chunking',
            self::CACHE_FIRST => 'Cache-first approach with minimal fresh discovery',
            self::REAL_TIME => 'Real-time discovery with immediate processing'
        };
    }

    /**
     * Get default optimizations for this strategy
     */
    public function getDefaultOptimizations(): array
    {
        return match ($this) {
            self::PERFORMANCE_OPTIMIZED => [
                'cache_aggressive' => true,
                'parallel_processing' => true,
                'memory_efficient' => true,
                'pattern_recognition' => true,
            ],
            self::COMPREHENSIVE => [
                'deep_analysis' => true,
                'dependency_mapping' => true,
                'pattern_recognition' => true,
                'performance_profiling' => true,
            ],
            self::MINIMAL => [
                'cache_only' => true,
                'skip_analysis' => true,
                'basic_patterns_only' => true,
            ],
            self::MEMORY_EFFICIENT => [
                'streaming_mode' => true,
                'chunk_processing' => true,
                'memory_monitoring' => true,
                'aggressive_cleanup' => true,
            ],
            self::CACHE_FIRST => [
                'cache_preference' => 100,
                'fresh_discovery_timeout' => 100,
                'fallback_minimal' => true,
            ],
            self::REAL_TIME => [
                'immediate_processing' => true,
                'skip_caching' => true,
                'minimal_analysis' => true,
            ]
        };
    }

    /**
     * Get expected performance characteristics
     */
    public function getPerformanceProfile(): array
    {
        return match ($this) {
            self::PERFORMANCE_OPTIMIZED => [
                'execution_time' => 'fast',
                'memory_usage' => 'moderate',
                'accuracy' => 'high',
                'cache_efficiency' => 'high',
            ],
            self::COMPREHENSIVE => [
                'execution_time' => 'slow',
                'memory_usage' => 'high',
                'accuracy' => 'maximum',
                'cache_efficiency' => 'moderate',
            ],
            self::MINIMAL => [
                'execution_time' => 'very_fast',
                'memory_usage' => 'low',
                'accuracy' => 'basic',
                'cache_efficiency' => 'high',
            ],
            self::MEMORY_EFFICIENT => [
                'execution_time' => 'moderate',
                'memory_usage' => 'very_low',
                'accuracy' => 'high',
                'cache_efficiency' => 'moderate',
            ],
            self::CACHE_FIRST => [
                'execution_time' => 'very_fast',
                'memory_usage' => 'low',
                'accuracy' => 'depends_on_cache',
                'cache_efficiency' => 'maximum',
            ],
            self::REAL_TIME => [
                'execution_time' => 'immediate',
                'memory_usage' => 'moderate',
                'accuracy' => 'good',
                'cache_efficiency' => 'none',
            ]
        };
    }

    /**
     * Check if strategy supports a specific feature
     */
    public function supports(string $feature): bool
    {
        $features = match ($this) {
            self::PERFORMANCE_OPTIMIZED => [
                'caching', 'parallel_processing', 'pattern_recognition', 'dependency_analysis',
            ],
            self::COMPREHENSIVE => [
                'caching', 'deep_analysis', 'dependency_mapping', 'pattern_recognition', 'performance_profiling',
            ],
            self::MINIMAL => [
                'caching', 'basic_discovery',
            ],
            self::MEMORY_EFFICIENT => [
                'streaming', 'chunking', 'memory_monitoring', 'cleanup',
            ],
            self::CACHE_FIRST => [
                'caching', 'fallback_discovery',
            ],
            self::REAL_TIME => [
                'immediate_processing', 'basic_discovery',
            ]
        };

        return in_array($feature, $features, true);
    }

    /**
     * Get recommended strategy based on context
     */
    public static function recommendFor(array $context): self
    {
        $pathCount = $context['path_count'] ?? 0;
        $memoryLimit = $context['memory_limit'] ?? 128 * 1024 * 1024; // 128MB
        $timeLimit = $context['time_limit'] ?? 30; // 30 seconds
        $accuracyRequired = $context['accuracy'] ?? 'high';

        // Large codebase with memory constraints
        if ($pathCount > 1000 || $memoryLimit < 64 * 1024 * 1024) {
            return self::MEMORY_EFFICIENT;
        }

        // Real-time requirements
        if ($timeLimit < 5) {
            return self::REAL_TIME;
        }

        // High accuracy requirements
        if ($accuracyRequired === 'maximum') {
            return self::COMPREHENSIVE;
        }

        // Basic requirements
        if ($accuracyRequired === 'basic' || $pathCount < 50) {
            return self::MINIMAL;
        }

        // Default to performance optimized
        return self::PERFORMANCE_OPTIMIZED;
    }
}
