<?php
declare(strict_types=1);

namespace App\Application\Security\ValueObjects;

final class OWASPEventIdentifier
{
    private function __construct(
        private string $category,
        private string $action,
        private ?string $subject = null
    ) {}

    public static function authenticationSuccess(string $username): self
    {
        return new self('AUTHN', 'login_success', $username);
    }

    public static function authenticationFailure(string $username): self
    {
        return new self('AUTHN', 'login_failure', $username);
    }

    public static function sessionTermination(string $username): self
    {
        return new self('AUTHN', 'logout', $username);
    }

    public static function passwordChange(string $username): self
    {
        return new self('AUTHN', 'password_change', $username);
    }

    public static function accountLocked(string $username): self
    {
        return new self('AUTHN', 'account_locked', $username);
    }

    public static function authorizationFailure(string $username, string $resource): self
    {
        return new self('AUTHZ', 'access_denied', "{$username}:{$resource}");
    }

    public static function privilegeEscalation(string $username, string $fromRole, string $toRole): self
    {
        return new self('AUTHZ', 'privilege_escalation', "{$username}:{$fromRole}>{$toRole}");
    }

    public static function inputValidationFailure(string $field): self
    {
        return new self('INPUT', 'validation_failure', $field);
    }

    public static function maliciousInput(string $attackType): self
    {
        return new self('INPUT', 'malicious_input', $attackType);
    }

    public static function fileUploadFailure(string $filename): self
    {
        return new self('INPUT', 'file_upload_failure', $filename);
    }

    public static function sessionHijacking(string $username): self
    {
        return new self('SESS', 'session_hijacking', $username);
    }

    public static function sessionTimeout(string $username): self
    {
        return new self('SESS', 'session_timeout', $username);
    }

    public static function malwareDetected(string $malwareType): self
    {
        return new self('MALICIOUS', 'malware_detected', $malwareType);
    }

    public static function auditFailure(string $eventType): self
    {
        return new self('AUDIT', 'audit_failure', $eventType);
    }

    public static function sessionFixation(string $username): self
    {
        return new self('SESS', 'session_fixation', $username);
    }

    public static function cryptographicFailure(string $operation): self
    {
        return new self('CRYPTO', 'crypto_failure', $operation);
    }

    public static function suspiciousNetworkActivity(string $activityType): self
    {
        return new self('NETWORK', 'suspicious_activity', $activityType);
    }

    public static function systemAnomaly(string $anomalyType): self
    {
        return new self('SYSTEM', 'anomaly_detected', $anomalyType);
    }

    public static function csrfViolation(string $requestPath): self
    {
        return new self('WEB', 'csrf_violation', $requestPath);
    }

    public function toString(): string
    {
        $identifier = "{$this->category}_{$this->action}";

        if ($this->subject !== null) {
            $identifier .= ":{$this->subject}";
        }

        return $identifier;
    }
}
