#
# Note: <keep all update> Collection | ConfigKey
# - Replace all "_" in app service names with "-"
# - Enums of Collection and Database exists at "./enums"
#
import os

from typing import get_args
from typing import Tuple, Literal, Optional


Database = Literal[
  "auth",
  "domain",
  "context",
  "identity",
  "essential",
  "financial",
  "general",
  "special",
  "touch",
  "content",
  "logistic",
  "conjoint",
  "career",
  "thing",
]

Coll = Literal[
  # Auth
  "apts",
  "grants",
  # Domain
  "apps",
  "clients",
  # Context
  "configs",
  "settings",
  # Identity
  "users",
  "sessions",
  "profiles",
  # Essential
  "sagas",
  "saga-stages",
  # Financial
  "wallets",
  "accounts",
  "invoices",
  "currencies",
  "transactions",
  # General
  "events",
  "comments",
  "artifacts",
  "workflows",
  "activities",
  # Special
  "stats",
  "files",
  # Touch
  "emails",
  "notices",
  "smss",
  "pushes",
  "push-histories",
  # Content
  "notes",
  "posts",
  "tickets",
  # Logistic
  "cargoes",
  "drivers",
  "travels",
  "vehicles",
  "locations",
  # Conjoint
  "accounts",
  "channels",
  "contacts",
  "members",
  "messages",
  # Career
  "branches",
  "businesses",
  "customers",
  "employees",
  "products",
  "services",
  "stocks",
  "stores",
  # Thing
  "devices",
  "sensors",
  "metrics",
]

Collection = Literal[
  # Auth
  "auth/apts",
  "auth/grants",
  # Domain
  "domain/apps",
  "domain/clients",
  # Context
  "context/configs",
  "context/settings",
  # Identity
  "identity/users",
  "identity/sessions",
  "identity/profiles",
  # Essential
  "essential/sagas",
  "essential/saga-stages",
  # Financial
  "financial/wallets",
  "financial/accounts",
  "financial/invoices",
  "financial/currencies",
  "financial/transactions",
  # General
  "general/events",
  "general/comments",
  "general/artifacts",
  "general/workflows",
  "general/activities",
  # Special
  "special/stats",
  "special/files",
  # Touch
  "touch/emails",
  "touch/notices",
  "touch/smss",
  "touch/pushes",
  "touch/push-histories",
  # Content
  "content/notes",
  "content/posts",
  "content/tickets",
  # Logistic
  "logistic/cargoes",
  "logistic/drivers",
  "logistic/travels",
  "logistic/vehicles",
  "logistic/locations",
  # Conjoint
  "conjoint/accounts",
  "conjoint/channels",
  "conjoint/contacts",
  "conjoint/members",
  "conjoint/messages",
  # Career
  "career/branches",
  "career/businesses",
  "career/customers",
  "career/employees",
  "career/products",
  "career/services",
  "career/stocks",
  "career/stores",
  # Thing
  "thing/devices",
  "thing/sensors",
  "thing/metrics",
]


def COLLECTION(coll: Coll, db: Optional[Database] = None) -> Collection:
  if db is None and os.getenv("PROTOTYPING_APP") is None:
    raise Exception(f"unknown ?.{coll} collection")
  name = db or os.getenv("PROTOTYPING_APP")
  collection = f"{name}/{coll}".lower()
  if collection not in list(get_args(Collection)):
    raise Exception("unknown collection")
  return collection  # type: ignore


def COLL(collection: Collection) -> Tuple[Coll, Database]:
  if collection not in list(get_args(Collection)):
    raise Exception("unknown collection")
  db, coll = collection.split("/")
  return coll, db  # type: ignore
