"""Constants for Mingx OpenTelemetry integration."""

from enum import Enum
from typing import Literal, Union, get_args

from typing_extensions import TypeAlias

MINGX_TRACER_NAME = "mingx-sdk"


class ObservationType(str, Enum):
    """Observation 类型枚举，对应 OTLP 属性 mingx.observation.type 的取值。

    与 Langfuse 的观测类型对齐，便于在同一 OTLP 管线或迁移时语义一致。
    """

    EVENT = "event"
    """瞬时点事件（discrete events），start_time = end_time。"""
    SPAN = "span"
    """通用 span，表示一段时间内的单元工作。"""
    GENERATION = "generation"
    """LLM 生成，含 prompt、token 用量、成本等。"""
    AGENT = "agent"
    """Agent 决策/应用流，如 ReAct 等使用 LLM 与 tool 的流程。"""
    TOOL = "tool"
    """工具调用，如调用天气 API、检索等。"""
    CHAIN = "chain"
    """链式步骤，如把 retriever 结果传给 LLM。"""
    RETRIEVER = "retriever"
    """检索步骤，如向量库、数据库查询。"""
    EVALUATOR = "evaluator"
    """评估函数，评估相关性/正确性/有用性等。"""
    EMBEDDING = "embedding"
    """Embedding 调用，可含 model、token 用量、成本。"""
    GUARDRAIL = "guardrail"
    """护栏组件，防护恶意内容或 jailbreak。"""


ObservationTypeLiteral: TypeAlias = Literal[
    "event", "span", "generation", "agent", "tool", "chain",
    "retriever", "evaluator", "embedding", "guardrail",
]

ObservationTypeLiteralNoEvent: TypeAlias = Literal[
    "span", "generation", "agent", "tool", "chain",
    "retriever", "evaluator", "embedding", "guardrail",
]


def get_observation_types_list() -> list[str]:
    """Return list of valid observation type strings."""
    return [e.value for e in ObservationType]


def normalize_observation_type(as_type: Union[ObservationType, str]) -> str:
    """Normalize to string for OTEL attribute; accept enum or str."""
    return as_type.value if isinstance(as_type, ObservationType) else as_type
