<?php

declare(strict_types=1);

namespace App\Application\Security\ValueObjects;

final class RequestContext
{
    private function __construct(
        private ?string $hostIp,
        private ?string $hostname,
        private ?string $protocol,
        private ?string $port,
        private ?string $requestUri,
        private ?string $requestMethod,
        private ?string $region,
        private ?string $geo
    ) {
    }

    public static function fromGlobals(): self
    {
        return new self(
            $_SERVER['SERVER_ADDR'] ?? null,
            $_SERVER['HTTP_HOST'] ?? $_SERVER['SERVER_NAME'] ?? null,
            isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http',
            $_SERVER['SERVER_PORT'] ?? null,
            $_SERVER['REQUEST_URI'] ?? null,
            $_SERVER['REQUEST_METHOD'] ?? null,
            $_ENV['AWS_REGION'] ?? $_ENV['REGION'] ?? null,
            $_ENV['GEO_REGION'] ?? null
        );
    }

    public function getHostIp(): ?string
    {
        return $this->hostIp;
    }

    public function getHostname(): ?string
    {
        return $this->hostname;
    }

    public function getProtocol(): ?string
    {
        return $this->protocol;
    }

    public function getPort(): ?string
    {
        return $this->port;
    }

    public function getRequestUri(): ?string
    {
        return $this->requestUri;
    }

    public function getRequestMethod(): ?string
    {
        return $this->requestMethod;
    }

    public function getRegion(): ?string
    {
        return $this->region;
    }

    public function getGeo(): ?string
    {
        return $this->geo;
    }
}
