<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Event\Distributed;

final class DistributedNodeId
{
    public function __construct(private readonly string $value) {}

    public static function generate(string $prefix = 'node'): self
    {
        return new self($prefix . '_' . substr(hash('sha256', gethostname() . getmypid() . microtime(true)), 0, 16));
    }

    public static function fromString(string $value): self
    {
        return new self($value !== '' ? $value : self::generate()->value());
    }

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