"""Short immutable public refs (tenant_ref, project_ref) for hostnames."""
from __future__ import annotations

import secrets
import string

_ALPHANUM = string.ascii_lowercase + string.digits


def generate_public_ref(length: int = 8, prefix: str = "") -> str:
    """Cryptographically random ref; prefix is optional e.g. ``t`` / ``p`` (keep short)."""
    body = "".join(secrets.choice(_ALPHANUM) for _ in range(length))
    return f"{prefix}{body}" if prefix else body
