<?php

declare(strict_types=1);

namespace App\Framework\Exception\SecurityEvent;

use App\Framework\Exception\SecurityLogLevel;

/**
 * Login-Erfolg nach Fehlversuchen Event
 */
final readonly class AuthenticationLoginSuccessAfterFailEvent implements SecurityEventInterface
{
    public function __construct(
        public string $userId,
        public int $retries
    ) {}

    public function getEventIdentifier(): string
    {
        return "authn_login_successafterfail:{$this->userId},{$this->retries}";
    }

    public function getDescription(): string
    {
        return "User {$this->userId} login successfully after {$this->retries} failures";
    }

    public function getLogLevel(): SecurityLogLevel
    {
        return SecurityLogLevel::WARN;
    }

    public function getCategory(): string
    {
        return 'authentication';
    }

    public function requiresAlert(): bool
    {
        // Alert wenn viele Fehlversuche vorausgingen
        return $this->retries >= 5;
    }

    public function toArray(): array
    {
        return [
            'userId' => $this->userId,
            'retries' => $this->retries,
            'event_identifier' => $this->getEventIdentifier(),
            'category' => $this->getCategory(),
            'log_level' => $this->getLogLevel()->value,
            'requires_alert' => $this->requiresAlert()
        ];
    }
}
