<?php

declare(strict_types=1);

namespace App\Framework\Waf\BotProtection;

/**
 * Bot detection types for classification
 */
enum BotDetectionType: string
{
    case FINGERPRINT_ANOMALY = 'fingerprint_anomaly';
    case BEHAVIORAL_ANOMALY = 'behavioral_anomaly';
    case DEVICE_ANOMALY = 'device_anomaly';
    case CAPTCHA_FAILURE = 'captcha_failure';
    case RATE_LIMITING = 'rate_limiting';
    case USER_AGENT_ANALYSIS = 'user_agent_analysis';
    case IP_REPUTATION = 'ip_reputation';
    case MOUSE_MOVEMENT = 'mouse_movement';
    case KEYSTROKE_PATTERN = 'keystroke_pattern';
    case TIMING_ANALYSIS = 'timing_analysis';
    case HEADLESS_DETECTION = 'headless_detection';
    case WEBDRIVER_DETECTION = 'webdriver_detection';
    case AUTOMATION_DETECTION = 'automation_detection';

    /**
     * Get detection description
     */
    public function getDescription(): string
    {
        return match($this) {
            self::FINGERPRINT_ANOMALY => 'Browser fingerprint shows anomalous characteristics',
            self::BEHAVIORAL_ANOMALY => 'User behavior patterns indicate automation',
            self::DEVICE_ANOMALY => 'Device characteristics are suspicious',
            self::CAPTCHA_FAILURE => 'CAPTCHA validation failed',
            self::RATE_LIMITING => 'Request rate exceeds normal patterns',
            self::USER_AGENT_ANALYSIS => 'User agent matches known bot patterns',
            self::IP_REPUTATION => 'IP address has poor reputation',
            self::MOUSE_MOVEMENT => 'Mouse movement patterns are unnatural',
            self::KEYSTROKE_PATTERN => 'Keystroke timing indicates automation',
            self::TIMING_ANALYSIS => 'Request timing patterns are suspicious',
            self::HEADLESS_DETECTION => 'Headless browser detected',
            self::WEBDRIVER_DETECTION => 'WebDriver automation detected',
            self::AUTOMATION_DETECTION => 'General automation indicators found'
        };
    }

    /**
     * Get severity level for this detection type
     */
    public function getDefaultSeverity(): DetectionSeverity
    {
        return match($this) {
            self::CAPTCHA_FAILURE,
            self::HEADLESS_DETECTION,
            self::WEBDRIVER_DETECTION => DetectionSeverity::HIGH,

            self::FINGERPRINT_ANOMALY,
            self::BEHAVIORAL_ANOMALY,
            self::DEVICE_ANOMALY,
            self::AUTOMATION_DETECTION => DetectionSeverity::MEDIUM,

            self::RATE_LIMITING,
            self::USER_AGENT_ANALYSIS,
            self::IP_REPUTATION,
            self::MOUSE_MOVEMENT,
            self::KEYSTROKE_PATTERN,
            self::TIMING_ANALYSIS => DetectionSeverity::LOW
        };
    }

    /**
     * Check if this detection type should block requests
     */
    public function shouldBlock(): bool
    {
        return match($this) {
            self::CAPTCHA_FAILURE,
            self::HEADLESS_DETECTION,
            self::WEBDRIVER_DETECTION => true,
            default => false
        };
    }

    /**
     * Get all detection types
     */
    public static function getAll(): array
    {
        return [
            self::FINGERPRINT_ANOMALY,
            self::BEHAVIORAL_ANOMALY,
            self::DEVICE_ANOMALY,
            self::CAPTCHA_FAILURE,
            self::RATE_LIMITING,
            self::USER_AGENT_ANALYSIS,
            self::IP_REPUTATION,
            self::MOUSE_MOVEMENT,
            self::KEYSTROKE_PATTERN,
            self::TIMING_ANALYSIS,
            self::HEADLESS_DETECTION,
            self::WEBDRIVER_DETECTION,
            self::AUTOMATION_DETECTION,
        ];
    }

    /**
     * Get detection types by severity
     */
    public static function getBySeverity(DetectionSeverity $severity): array
    {
        return array_filter(
            self::getAll(),
            fn ($type) => $type->getDefaultSeverity() === $severity
        );
    }
}
