<?php
declare(strict_types=1);

namespace App\Application\Security\Events\Input;

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

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

    public function __construct(
        public readonly string $fieldName,
        public readonly string $attackPattern,
        public readonly string $sanitizedValue,
        public readonly ?string $email = null
    ) {
        $this->maskedEmail = $this->email ? MaskedEmail::fromString($this->email) : null;
    }

    public function getOWASPEventIdentifier(): OWASPEventIdentifier
    {
        return OWASPEventIdentifier::maliciousInput($this->attackPattern);
    }

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

    public function getDescription(): string
    {
        return "Malicious input detected: {$this->attackPattern}";
    }

    public function getEventData(): array
    {
        return [
            'field_name' => $this->fieldName,
            'attack_pattern' => $this->attackPattern,
            'sanitized_value' => $this->sanitizedValue,
            'username' => $this->maskedEmail?->toString() ?? 'anonymous'
        ];
    }

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