<?php

declare(strict_types=1);

namespace App\Application\Security\ValueObjects;

use DateTimeImmutable;

final class SecurityContext
{
    private function __construct(
        private ?string $sourceIp,
        private ?string $userAgent,
        private ?string $sessionId,
        private ?string $requestId,
        private ?string $userId,
        private DateTimeImmutable $timestamp
    ) {
    }

    public static function fromGlobals(): self
    {
        return new self(
            $_SERVER['REMOTE_ADDR'] ?? null,
            $_SERVER['HTTP_USER_AGENT'] ?? null,
            session_id() ?: null,
            $_SERVER['HTTP_X_REQUEST_ID'] ?? null,
            $_SESSION['user_id'] ?? null,
            new DateTimeImmutable()
        );
    }

    public static function create(
        ?string $sourceIp = null,
        ?string $userAgent = null,
        ?string $sessionId = null,
        ?string $requestId = null,
        ?string $userId = null
    ): self {
        return new self(
            $sourceIp,
            $userAgent,
            $sessionId,
            $requestId,
            $userId,
            new DateTimeImmutable()
        );
    }

    public function getSourceIp(): ?string
    {
        return $this->sourceIp;
    }

    public function getUserAgent(): ?string
    {
        return $this->userAgent;
    }

    public function getSessionId(): ?string
    {
        return $this->sessionId;
    }

    public function getRequestId(): ?string
    {
        return $this->requestId;
    }

    public function getUserId(): ?string
    {
        return $this->userId;
    }

    public function getTimestamp(): DateTimeImmutable
    {
        return $this->timestamp;
    }
}
