<?php

declare(strict_types=1);

namespace App\Framework\DDoS\Waf;

/**
 * DDoS WAF Layer Configuration
 */
final readonly class DDoSWafConfig
{
    public function __construct(
        public bool $enabled = true,
        public int $maxResourceIntensiveRequestsPerMinute = 5,
        public int $maxSlowConnectionsPerIp = 10,
        public float $httpFloodThreshold = 100.0, // requests per minute
        public float $botDetectionThreshold = 0.6, // 0.0-1.0 scale
        public int $maxQueryLength = 2048,
        public float $protocolAbuseThreshold = 0.5, // 0.0-1.0 scale
        public int $maxRepetitiveRequests = 20,
        public bool $enableLearning = true,
        public bool $enableStatistics = true,
        public int $cacheRetentionHours = 24
    ) {
    }

    /**
     * Create development configuration (more permissive)
     */
    public static function development(): self
    {
        return new self(
            enabled: true,
            maxResourceIntensiveRequestsPerMinute: 20, // Higher for development
            maxSlowConnectionsPerIp: 50,
            httpFloodThreshold: 500.0, // Much higher threshold
            botDetectionThreshold: 0.8, // Less sensitive
            maxQueryLength: 8192, // Allow longer queries
            protocolAbuseThreshold: 0.8, // Less sensitive
            maxRepetitiveRequests: 100, // Allow more repetition
            enableLearning: true,
            enableStatistics: true,
            cacheRetentionHours: 1 // Shorter retention for dev
        );
    }

    /**
     * Create production configuration (strict security)
     */
    public static function production(): self
    {
        return new self(
            enabled: true,
            maxResourceIntensiveRequestsPerMinute: 3, // Very strict
            maxSlowConnectionsPerIp: 5,
            httpFloodThreshold: 50.0, // Lower threshold
            botDetectionThreshold: 0.4, // More sensitive
            maxQueryLength: 1024, // Shorter queries
            protocolAbuseThreshold: 0.3, // More sensitive
            maxRepetitiveRequests: 10, // Less repetition allowed
            enableLearning: true,
            enableStatistics: true,
            cacheRetentionHours: 48 // Longer retention for analysis
        );
    }

    /**
     * Create testing configuration (disabled)
     */
    public static function testing(): self
    {
        return new self(
            enabled: false,
            enableLearning: false,
            enableStatistics: false,
            cacheRetentionHours: 1
        );
    }

    /**
     * Get configuration for specific attack type
     */
    public function getThresholdForAttackType(string $attackType): float
    {
        return match ($attackType) {
            'resource_intensive' => (float) $this->maxResourceIntensiveRequestsPerMinute,
            'slow_connections' => (float) $this->maxSlowConnectionsPerIp,
            'http_flood' => $this->httpFloodThreshold,
            'bot_detection' => $this->botDetectionThreshold,
            'protocol_abuse' => $this->protocolAbuseThreshold,
            'repetitive' => (float) $this->maxRepetitiveRequests,
            default => 0.5
        };
    }

    /**
     * Check if detection type is enabled
     */
    public function isDetectionEnabled(string $detectionType): bool
    {
        if (! $this->enabled) {
            return false;
        }

        // All detections are enabled if WAF layer is enabled
        // Could be extended to allow fine-grained control
        return true;
    }

    /**
     * Get cache retention time in seconds
     */
    public function getCacheRetentionSeconds(): int
    {
        return $this->cacheRetentionHours * 3600;
    }

    /**
     * Convert to array for serialization
     */
    public function toArray(): array
    {
        return [
            'enabled' => $this->enabled,
            'thresholds' => [
                'resource_intensive_per_minute' => $this->maxResourceIntensiveRequestsPerMinute,
                'slow_connections_per_ip' => $this->maxSlowConnectionsPerIp,
                'http_flood_per_minute' => $this->httpFloodThreshold,
                'bot_detection_score' => $this->botDetectionThreshold,
                'protocol_abuse_score' => $this->protocolAbuseThreshold,
                'repetitive_requests' => $this->maxRepetitiveRequests,
            ],
            'limits' => [
                'max_query_length' => $this->maxQueryLength,
            ],
            'features' => [
                'learning_enabled' => $this->enableLearning,
                'statistics_enabled' => $this->enableStatistics,
            ],
            'cache' => [
                'retention_hours' => $this->cacheRetentionHours,
            ],
        ];
    }
}
