<?php

declare(strict_types=1);

namespace App\Domain\SmartLink\ValueObjects;

final readonly class DestinationUrl
{
    private function __construct(
        private string $value
    ) {
        $this->validate();
    }

    public static function fromString(string $url): self
    {
        return new self($url);
    }

    private function validate(): void
    {
        if (! filter_var($this->value, FILTER_VALIDATE_URL)) {
            throw new \InvalidArgumentException('Invalid destination URL format');
        }

        $scheme = parse_url($this->value, PHP_URL_SCHEME);
        if (! in_array($scheme, ['http', 'https'], true)) {
            throw new \InvalidArgumentException('Destination URL must use HTTP or HTTPS protocol');
        }
    }

    public function toString(): string
    {
        return $this->value;
    }

    public function getHost(): string
    {
        return parse_url($this->value, PHP_URL_HOST) ?? '';
    }

    public function isSecure(): bool
    {
        return parse_url($this->value, PHP_URL_SCHEME) === 'https';
    }

    public function equals(self $other): bool
    {
        return $this->value === $other->value;
    }

    public function __toString(): string
    {
        return $this->value;
    }
}
