<?php

declare(strict_types=1);

namespace App\Application\Security\Events\Auth;

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

final class SessionTerminatedEvent implements OWASPSecurityEvent
{
    private MaskedEmail $maskedEmail;

    public function __construct(
        public readonly string $email,
        public readonly string $sessionId,
        public readonly string $reason = 'logout'
    ) {
        $this->maskedEmail = MaskedEmail::fromString($this->email);
    }

    public function getOWASPEventIdentifier(): OWASPEventIdentifier
    {
        return OWASPEventIdentifier::sessionTermination($this->maskedEmail->toString());
    }

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

    public function getDescription(): string
    {
        return "User {$this->maskedEmail->toString()} logged out";
    }

    public function getEventData(): array
    {
        return [
            'username' => $this->maskedEmail->toString(),
            'session_id' => hash('sha256', $this->sessionId),
            'termination_reason' => $this->reason,
        ];
    }

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