<?php

declare(strict_types=1);

namespace App\Framework\Waf\BotProtection\ValueObjects;

use App\Framework\Core\ValueObjects\Duration;
use App\Framework\Core\ValueObjects\Percentage;

/**
 * Result of behavioral analysis for bot detection
 */
final readonly class BehavioralAnalysisResult
{
    public function __construct(
        public bool $isSuspicious,
        public float $riskScore,
        public array $detections,
        public array $behaviorMetrics,
        public Duration $analysisDuration,
        public Percentage $confidence
    ) {
    }

    /**
     * Create empty result
     */
    public static function empty(): self
    {
        return new self(
            isSuspicious: false,
            riskScore: 0.0,
            detections: [],
            behaviorMetrics: [],
            analysisDuration: Duration::zero(),
            confidence: Percentage::from(0.0)
        );
    }

    /**
     * Get risk score
     */
    public function getRiskScore(): float
    {
        return $this->riskScore;
    }

    /**
     * Get detections
     */
    public function getDetections(): array
    {
        return $this->detections;
    }

    /**
     * Check if highly suspicious
     */
    public function isHighlySuspicious(): bool
    {
        return $this->riskScore > 80.0;
    }

    /**
     * Convert to array
     */
    public function toArray(): array
    {
        return [
            'is_suspicious' => $this->isSuspicious,
            'is_highly_suspicious' => $this->isHighlySuspicious(),
            'risk_score' => $this->riskScore,
            'detection_count' => count($this->detections),
            'behavior_metrics' => $this->behaviorMetrics,
            'analysis_duration_ms' => $this->analysisDuration->toMilliseconds(),
            'confidence' => $this->confidence->getValue(),
            'detections' => array_map(
                fn ($detection) => $detection instanceof BotDetection ? $detection->toArray() : $detection,
                $this->detections
            ),
        ];
    }
}
