<?php

declare(strict_types=1);

namespace PpPhp\Core\Platform\Identity;

final class PlatformIdentity
{
    public function __construct(
        private readonly string $uuid,
        private string $id,
        private readonly string $runtimeName = '',
        private readonly string $namespace = ''
    ) {
        $this->id = PlatformIdGenerator::normalize($id);
    }

    public static function make(
        string $id,
        string $prefix = 'asset',
        string $runtimeName = '',
        string $namespace = ''
    ): self {
        return new self(
            PlatformIdGenerator::uuid($prefix),
            $id,
            $runtimeName,
            $namespace
        );
    }

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

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

    public function runtimeName(): string
    {
        return $this->runtimeName ?: $this->id;
    }

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

    public function rename(string $id): void
    {
        $this->id = PlatformIdGenerator::normalize($id);
    }

    public function toArray(): array
    {
        return [
            'uuid' => $this->uuid,
            'id' => $this->id,
            'runtimeName' => $this->runtimeName(),
            'namespace' => $this->namespace,
        ];
    }
}
