<?php
declare(strict_types=1);

namespace App\Application\Security\Events\Session;

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

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

    public function __construct(
        public readonly ?string $email,
        public readonly string $oldSessionId,
        public readonly string $newSessionId,
        public readonly string $attackVector
    ) {
        $this->maskedEmail = $this->email ? MaskedEmail::fromString($this->email) : null;
    }

    public function getOWASPEventIdentifier(): OWASPEventIdentifier
    {
        return OWASPEventIdentifier::sessionFixation($this->maskedEmail?->toString() ?? 'anonymous');
    }

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

    public function getDescription(): string
    {
        return "Session fixation attack detected for user {$this->maskedEmail?->toString() ?? 'anonymous'}";
    }

    public function getEventData(): array
    {
        return [
            'username' => $this->maskedEmail?->toString() ?? 'anonymous',
            'old_session_id' => hash('sha256', $this->oldSessionId),
            'new_session_id' => hash('sha256', $this->newSessionId),
            'attack_vector' => $this->attackVector
        ];
    }

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