<?php

declare(strict_types=1);

namespace App\Application\Security\Events\Network;

use App\Application\Security\{OWASPSecurityEvent};
use App\Application\Security\ValueObjects\{MaskedEmail, OWASPEventIdentifier, OWASPLogLevel};

final class SuspiciousNetworkActivityEvent implements OWASPSecurityEvent
{
    private ?MaskedEmail $maskedEmail;

    public function __construct(
        public readonly string $sourceIp,
        public readonly string $activityType,
        public readonly int $requestCount,
        public readonly string $timeWindow,
        public readonly ?string $email = null
    ) {
        $this->maskedEmail = $this->email ? MaskedEmail::fromString($this->email) : null;
    }

    public function getOWASPEventIdentifier(): OWASPEventIdentifier
    {
        return OWASPEventIdentifier::suspiciousNetworkActivity($this->activityType);
    }

    public function getOWASPLogLevel(): OWASPLogLevel
    {
        return OWASPLogLevel::WARN;
    }

    public function getDescription(): string
    {
        return "Suspicious network activity detected: {$this->activityType}";
    }

    public function getEventData(): array
    {
        return [
            'source_ip' => $this->sourceIp,
            'activity_type' => $this->activityType,
            'request_count' => $this->requestCount,
            'time_window' => $this->timeWindow,
            'username' => $this->maskedEmail?->toString() ?? 'anonymous',
        ];
    }

    public function getMaskedEmail(): ?MaskedEmail
    {
        return $this->maskedEmail;
    }
}
