<?php

declare(strict_types=1);

namespace App\Framework\Mcp\Core\ValueObjects;

/**
 * Cache Strategy Enumeration
 *
 * Defines different caching strategies for MCP tools based on
 * data volatility, performance requirements, and usage patterns.
 */
enum CacheStrategy: string
{
    case SHORT = 'short';           // 5 minutes - for rapidly changing data
    case MEDIUM = 'medium';         // 30 minutes - for moderately stable data
    case LONG = 'long';             // 2 hours - for stable data
    case PERSISTENT = 'persistent'; // 24 hours - for very stable data
    case ADAPTIVE = 'adaptive';     // Dynamic based on usage patterns
    case DISABLED = 'disabled';     // No caching

    public function getDescription(): string
    {
        return match ($this) {
            self::SHORT => 'Short-term caching for rapidly changing data (5 minutes)',
            self::MEDIUM => 'Medium-term caching for moderately stable data (30 minutes)',
            self::LONG => 'Long-term caching for stable data (2 hours)',
            self::PERSISTENT => 'Persistent caching for very stable data (24 hours)',
            self::ADAPTIVE => 'Adaptive caching based on usage patterns and data characteristics',
            self::DISABLED => 'No caching - always fetch fresh data'
        };
    }

    public function getTTLSeconds(): int
    {
        return match ($this) {
            self::SHORT => 300,
            self::MEDIUM => 1800,
            self::LONG => 7200,
            self::PERSISTENT => 86400,
            self::ADAPTIVE => 3600, // Default for adaptive
            self::DISABLED => 0
        };
    }

    public function isEnabled(): bool
    {
        return $this !== self::DISABLED;
    }

    public function getRecommendedFor(): array
    {
        return match ($this) {
            self::SHORT => [
                'health_checks',
                'system_status',
                'performance_metrics',
                'real_time_data',
            ],
            self::MEDIUM => [
                'user_data',
                'configuration',
                'moderate_analytics',
                'session_data',
            ],
            self::LONG => [
                'route_definitions',
                'container_bindings',
                'static_analysis',
                'framework_structure',
            ],
            self::PERSISTENT => [
                'metadata',
                'schemas',
                'constants',
                'immutable_data',
            ],
            self::ADAPTIVE => [
                'mixed_volatility',
                'usage_dependent',
                'learning_systems',
                'default_choice',
            ],
            self::DISABLED => [
                'security_sensitive',
                'one_time_operations',
                'debugging',
                'development_mode',
            ]
        };
    }

    public static function recommendFor(string $toolName, string $methodName): self
    {
        // Health and status checks
        if (str_contains(strtolower($toolName), 'health') ||
            str_contains(strtolower($methodName), 'status')) {
            return self::SHORT;
        }

        // Performance and metrics
        if (str_contains(strtolower($toolName), 'performance') ||
            str_contains(strtolower($methodName), 'metrics')) {
            return self::SHORT;
        }

        // Security tools
        if (str_contains(strtolower($toolName), 'security') ||
            str_contains(strtolower($toolName), 'vulnerability')) {
            return self::MEDIUM;
        }

        // Route and container analysis
        if (str_contains(strtolower($toolName), 'route') ||
            str_contains(strtolower($toolName), 'container')) {
            return self::LONG;
        }

        // Framework structure and static analysis
        if (str_contains(strtolower($methodName), 'analyze') ||
            str_contains(strtolower($methodName), 'discover') ||
            str_contains(strtolower($toolName), 'framework')) {
            return self::LONG;
        }

        // File system operations
        if (str_contains(strtolower($toolName), 'file') ||
            str_contains(strtolower($methodName), 'list') ||
            str_contains(strtolower($methodName), 'find')) {
            return self::MEDIUM;
        }

        // Default to adaptive for unknown patterns
        return self::ADAPTIVE;
    }

    public function shouldCompress(): bool
    {
        return match ($this) {
            self::LONG, self::PERSISTENT => true,
            self::ADAPTIVE => true,
            default => false
        };
    }

    public function getCompressionLevel(): int
    {
        return match ($this) {
            self::SHORT => 1,     // Fast compression
            self::MEDIUM => 3,    // Balanced
            self::LONG => 6,      // Better compression
            self::PERSISTENT => 9, // Maximum compression
            self::ADAPTIVE => 6,  // Balanced default
            self::DISABLED => 0
        };
    }
}
