# #region DemoDataService [C:5] [TYPE Module] [SEMANTICS clean-release, demo, seed, fixture]
# @defgroup Services Module group.
# @BRIEF Provide deterministic namespace helpers and isolated in-memory repository creation for demo and real modes.
# @LAYER Domain
# @RELATION DEPENDS_ON -> [RepositoryRelations]
# @INVARIANT  Demo and real namespaces must never collide for generated physical identifiers.
# @SIDE_EFFECT  Writes demo entities to repository
# @DATA_CONTRACT  SeedConfig -> DemoEntities

from __future__ import annotations

from .repository import CleanReleaseRepository


# #region resolve_namespace [TYPE Function]
# @ingroup Services
# @BRIEF Resolve canonical clean-release namespace for requested mode.
# @PRE  mode is a non-empty string identifying runtime mode.
# @POST  Returns deterministic namespace key for demo/real separation.
def resolve_namespace(mode: str) -> str:
    normalized = (mode or "").strip().lower()
    if normalized == "demo":
        return "clean-release:demo"
    return "clean-release:real"


# #endregion resolve_namespace


# #region build_namespaced_id [TYPE Function]
# @ingroup Services
# @BRIEF Build storage-safe physical identifier under mode namespace.
# @PRE  namespace and logical_id are non-empty strings.
# @POST  Returns deterministic "{namespace}::{logical_id}" identifier.
def build_namespaced_id(namespace: str, logical_id: str) -> str:
    if not namespace or not namespace.strip():
        raise ValueError("namespace must be non-empty")
    if not logical_id or not logical_id.strip():
        raise ValueError("logical_id must be non-empty")
    return f"{namespace}::{logical_id}"


# #endregion build_namespaced_id


# #region create_isolated_repository [TYPE Function]
# @ingroup Services
# @BRIEF Create isolated in-memory repository instance for selected mode namespace.
# @PRE  mode is a valid runtime mode marker.
# @POST  Returns repository instance tagged with namespace metadata.
def create_isolated_repository(mode: str) -> CleanReleaseRepository:
    namespace = resolve_namespace(mode)
    repository = CleanReleaseRepository()
    repository.namespace = namespace
    return repository


# #endregion create_isolated_repository

# #endregion DemoDataService
