<?php

declare(strict_types=1);

namespace App\Application\Security\Events\Crypto;

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

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

    public function __construct(
        public readonly string $operation,
        public readonly string $algorithm,
        public readonly string $errorMessage,
        public readonly ?string $email = null
    ) {
        $this->maskedEmail = $this->email ? MaskedEmail::fromString($this->email) : null;
    }

    public function getOWASPEventIdentifier(): OWASPEventIdentifier
    {
        return OWASPEventIdentifier::cryptographicFailure($this->operation);
    }

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

    public function getDescription(): string
    {
        return "Cryptographic failure in {$this->operation}";
    }

    public function getEventData(): array
    {
        return [
            'operation' => $this->operation,
            'algorithm' => $this->algorithm,
            'error_message' => $this->errorMessage,
            'username' => $this->maskedEmail?->toString() ?? 'system',
        ];
    }

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