from __future__ import annotations

from typing import Annotated

from pydantic import AliasChoices, Field

from ai.backend.common.config import BaseConfigSchema
from ai.backend.common.meta import BackendAIConfigMeta, ConfigExample

__all__ = ("OTELConfig",)


class OTELConfig(BaseConfigSchema):
    """Configuration for OpenTelemetry tracing and logging.

    OpenTelemetry is used for distributed tracing and observability
    across Backend.AI components.
    """

    enabled: Annotated[
        bool,
        Field(default=False),
        BackendAIConfigMeta(
            description=(
                "Whether to enable OpenTelemetry for tracing or logging. "
                "When enabled, traces or logs will be collected and sent to the configured OTLP endpoint."
            ),
            added_version="25.7.0",
            example=ConfigExample(local="false", prod="true"),
        ),
    ]
    log_level: Annotated[
        str,
        Field(
            default="INFO",
            validation_alias=AliasChoices("log-level", "log_level"),
            serialization_alias="log-level",
        ),
        BackendAIConfigMeta(
            description=(
                "Log level for OpenTelemetry. "
                "Controls the verbosity of logs generated by OpenTelemetry. "
                "Common levels include 'DEBUG', 'INFO', 'WARN', 'ERROR'."
            ),
            added_version="25.7.0",
            example=ConfigExample(local="DEBUG", prod="INFO"),
        ),
    ]
    endpoint: Annotated[
        str,
        Field(default="http://localhost:4317"),
        BackendAIConfigMeta(
            description=(
                "OTLP endpoint for sending traces. "
                "Should include the host and port of the OTLP receiver."
            ),
            added_version="25.7.0",
            example=ConfigExample(local="http://localhost:4317", prod="http://otel-collector:4317"),
        ),
    ]
