# [DEF:backend.src.services.clean_release.demo_data_service:Module]
# @TIER: STANDARD
# @SEMANTICS: clean-release, demo-mode, namespace, isolation, repository
# @PURPOSE: Provide deterministic namespace helpers and isolated in-memory repository creation for demo and real modes.
# @LAYER: Domain
# @RELATION: DEPENDS_ON -> backend.src.services.clean_release.repository
# @INVARIANT: Demo and real namespaces must never collide for generated physical identifiers.

from __future__ import annotations

from .repository import CleanReleaseRepository


# [DEF:resolve_namespace:Function]
# @PURPOSE: 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"
# [/DEF:resolve_namespace:Function]


# [DEF:build_namespaced_id:Function]
# @PURPOSE: 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}"
# [/DEF:build_namespaced_id:Function]


# [DEF:create_isolated_repository:Function]
# @PURPOSE: 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()
    setattr(repository, "namespace", namespace)
    return repository
# [/DEF:create_isolated_repository:Function]

# [/DEF:backend.src.services.clean_release.demo_data_service:Module]