<?php

declare(strict_types=1);

namespace App\Shared\Domain;

use InvalidArgumentException;
use Ramsey\Uuid\Uuid;

use function sprintf;

final readonly class CorrelationId
{
    private function __construct(
        private string $value,
    ) {
    }

    public static function generate(): self
    {
        return new self(Uuid::uuid7()->toString());
    }

    public static function fromString(string $value): self
    {
        if (!Uuid::isValid($value)) {
            throw new InvalidArgumentException(
                sprintf('Invalid correlation ID format: "%s". Expected UUID.', $value),
            );
        }

        return new self($value);
    }

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

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