from collections.abc import Iterator
from typing import ClassVar

from .types import OPTIONAL, RECOMMENDED, REQUIRED, AttributeSpec


class OtelGenAISpanSpecification:
    SPEC_VERSION: ClassVar[str] = "1.38.0"

    SUPPORTED_ATTRIBUTES: ClassVar[dict[str, AttributeSpec]] = {
        "aws.bedrock.guardrail.id": AttributeSpec(
            name="aws.bedrock.guardrail.id",
            requirement=REQUIRED,
            type="string",
        ),
        "aws.bedrock.knowledge_base.id": AttributeSpec(
            name="aws.bedrock.knowledge_base.id",
            requirement=RECOMMENDED,
            type="string",
        ),
        "error.type": AttributeSpec(
            name="error.type",
            requirement=OPTIONAL,
            type="string",
        ),
        "gen_ai.agent.description": AttributeSpec(
            name="gen_ai.agent.description",
            requirement=OPTIONAL,
            type="string",
        ),
        "gen_ai.agent.id": AttributeSpec(
            name="gen_ai.agent.id",
            requirement=OPTIONAL,
            type="string",
        ),
        "gen_ai.agent.name": AttributeSpec(
            name="gen_ai.agent.name",
            requirement=OPTIONAL,
            type="string",
        ),
        "gen_ai.completion": AttributeSpec(
            deprecated=True,
            deprecated_message="Deprecated in v1.36.0+. Use Event API with gen_ai.output.messages instead",
            name="gen_ai.completion",
            requirement=OPTIONAL,
            type="string",
        ),
        "gen_ai.content.completion": AttributeSpec(
            deprecated=True,
            deprecated_message="Deprecated in v1.36.0+. Use Event API with gen_ai.output.messages instead",
            name="gen_ai.content.completion",
            requirement=OPTIONAL,
            type="string",
        ),
        "gen_ai.content.prompt": AttributeSpec(
            deprecated=True,
            deprecated_message="Deprecated in v1.36.0+. Use Event API with gen_ai.input.messages instead",
            name="gen_ai.content.prompt",
            requirement=OPTIONAL,
            type="string",
        ),
        "gen_ai.conversation.id": AttributeSpec(
            name="gen_ai.conversation.id",
            requirement=OPTIONAL,
            type="string",
        ),
        "gen_ai.data_source.id": AttributeSpec(
            name="gen_ai.data_source.id",
            requirement=OPTIONAL,
            type="string",
        ),
        "gen_ai.embeddings.dimension.count": AttributeSpec(
            name="gen_ai.embeddings.dimension.count",
            requirement=RECOMMENDED,
            type="int",
        ),
        # Event-specific attributes
        #
        # # "gen_ai.evaluation.explanation": AttributeSpec(
        #     name="gen_ai.evaluation.explanation",
        #     requirement=RECOMMENDED,
        #     type="string",
        # ),
        # "gen_ai.evaluation.name": AttributeSpec(
        #     name="gen_ai.evaluation.name",
        #     requirement=REQUIRED,
        #     type="string",
        # ),
        # "gen_ai.evaluation.score.label": AttributeSpec(
        #     name="gen_ai.evaluation.score.label",
        #     requirement=RECOMMENDED,
        #     type="string",
        # ),
        # "gen_ai.evaluation.score.value": AttributeSpec(
        #     name="gen_ai.evaluation.score.value",
        #     requirement=RECOMMENDED,
        #     type="double",
        # ),
        "gen_ai.input.messages": AttributeSpec(
            name="gen_ai.input.messages",
            requirement=OPTIONAL,
            type="any",
        ),
        "gen_ai.operation.name": AttributeSpec(
            name="gen_ai.operation.name",
            requirement=REQUIRED,
            type="string",
            valid_values=[
                "chat",
                "create_agent",
                "embeddings",
                "execute_tool",
                "generate_content",
                "invoke_agent",
                "text_completion",
            ],
        ),
        "gen_ai.output.messages": AttributeSpec(
            name="gen_ai.output.messages",
            requirement=OPTIONAL,
            type="any",
        ),
        "gen_ai.output.type": AttributeSpec(
            name="gen_ai.output.type",
            requirement=OPTIONAL,
            type="string",
            valid_values=[
                "image",
                "json",
                "speech",
                "text",
            ],
        ),
        "gen_ai.prompt": AttributeSpec(
            deprecated=True,
            deprecated_message="Deprecated in v1.36.0+. Use Event API with gen_ai.input.messages instead",
            name="gen_ai.prompt",
            requirement=OPTIONAL,
            type="string",
        ),
        "gen_ai.provider.name": AttributeSpec(
            name="gen_ai.provider.name",
            requirement=REQUIRED,
            type="string",
            valid_values=[
                "anthropic",
                "aws.bedrock",
                "azure.ai.inference",
                "azure.ai.openai",
                "cohere",
                "deepseek",
                "gcp.gemini",
                "gcp.gen_ai",
                "gcp.vertex_ai",
                "groq",
                "ibm.watsonx.ai",
                "mistral_ai",
                "openai",
                "perplexity",
                "x_ai",
            ],
        ),
        "gen_ai.request.choice.count": AttributeSpec(
            name="gen_ai.request.choice.count",
            requirement=OPTIONAL,
            type="int",
        ),
        "gen_ai.request.encoding_formats": AttributeSpec(
            name="gen_ai.request.encoding_formats",
            requirement=RECOMMENDED,
            type="string[]",
        ),
        "gen_ai.request.frequency_penalty": AttributeSpec(
            name="gen_ai.request.frequency_penalty",
            requirement=RECOMMENDED,
            type="double",
        ),
        "gen_ai.request.max_tokens": AttributeSpec(
            name="gen_ai.request.max_tokens",
            requirement=RECOMMENDED,
            type="int",
        ),
        "gen_ai.request.model": AttributeSpec(
            name="gen_ai.request.model",
            requirement=OPTIONAL,
            type="string",
        ),
        "gen_ai.request.presence_penalty": AttributeSpec(
            name="gen_ai.request.presence_penalty",
            requirement=RECOMMENDED,
            type="double",
        ),
        "gen_ai.request.seed": AttributeSpec(
            name="gen_ai.request.seed",
            requirement=OPTIONAL,
            type="int",
        ),
        "gen_ai.request.stop_sequences": AttributeSpec(
            name="gen_ai.request.stop_sequences",
            requirement=RECOMMENDED,
            type="string[]",
        ),
        "gen_ai.request.temperature": AttributeSpec(
            name="gen_ai.request.temperature",
            requirement=RECOMMENDED,
            type="double",
        ),
        "gen_ai.request.top_k": AttributeSpec(
            name="gen_ai.request.top_k",
            requirement=RECOMMENDED,
            type="double",
        ),
        "gen_ai.request.top_p": AttributeSpec(
            name="gen_ai.request.top_p",
            requirement=RECOMMENDED,
            type="double",
        ),
        "gen_ai.response.finish_reasons": AttributeSpec(
            name="gen_ai.response.finish_reasons",
            requirement=RECOMMENDED,
            type="string[]",
            valid_values=[
                "content_filter",
                "error",
                "length",
                "other",
                "stop",
                "tool_calls",
            ],
        ),
        "gen_ai.response.id": AttributeSpec(
            name="gen_ai.response.id",
            requirement=RECOMMENDED,
            type="string",
        ),
        "gen_ai.response.model": AttributeSpec(
            name="gen_ai.response.model",
            requirement=RECOMMENDED,
            type="string",
        ),
        "gen_ai.system": AttributeSpec(
            deprecated=True,
            deprecated_message="Deprecated in v1.38.0+. Use gen_ai.provider.name instead",
            name="gen_ai.system",
            requirement=OPTIONAL,
            type="string",
            valid_values=[
                "anthropic",
                "azure_ai",
                "cohere",
                "openai",
                "vertex_ai",
            ],
        ),
        "gen_ai.system_instructions": AttributeSpec(
            name="gen_ai.system_instructions",
            requirement=OPTIONAL,
            type="any",
        ),
        # Event-specific attribute
        #
        # "gen_ai.token.type": AttributeSpec(
        #     name="gen_ai.token.type",
        #     requirement=OPTIONAL,
        #     type="string",
        #     valid_values=[
        #         "input",
        #         "output",
        #     ],
        # ),
        "gen_ai.tool.call.arguments": AttributeSpec(
            name="gen_ai.tool.call.arguments",
            requirement=OPTIONAL,
            type="any",
        ),
        "gen_ai.tool.call.id": AttributeSpec(
            name="gen_ai.tool.call.id",
            requirement=RECOMMENDED,
            type="string",
        ),
        "gen_ai.tool.call.result": AttributeSpec(
            name="gen_ai.tool.call.result",
            requirement=OPTIONAL,
            type="any",
        ),
        "gen_ai.tool.definitions": AttributeSpec(
            name="gen_ai.tool.definitions",
            requirement=OPTIONAL,
            type="any",
        ),
        "gen_ai.tool.description": AttributeSpec(
            name="gen_ai.tool.description",
            requirement=RECOMMENDED,
            type="string",
        ),
        "gen_ai.tool.name": AttributeSpec(
            name="gen_ai.tool.name",
            requirement=RECOMMENDED,
            type="string",
        ),
        "gen_ai.tool.type": AttributeSpec(
            name="gen_ai.tool.type",
            requirement=RECOMMENDED,
            type="string",
            valid_values=[
                "datastore",
                "extension",
                "function",
            ],
        ),
        "gen_ai.usage.input_tokens": AttributeSpec(
            name="gen_ai.usage.input_tokens",
            requirement=RECOMMENDED,
            type="int",
        ),
        "gen_ai.usage.output_tokens": AttributeSpec(
            name="gen_ai.usage.output_tokens",
            requirement=RECOMMENDED,
            type="int",
        ),
        "openai.request.service_tier": AttributeSpec(
            name="openai.request.service_tier",
            requirement=OPTIONAL,
            type="string",
            valid_values=[
                "auto",
                "default",
            ],
        ),
        "openai.response.service_tier": AttributeSpec(
            name="openai.response.service_tier",
            requirement=RECOMMENDED,
            type="string",
            valid_values=[
                "default",
                "scale",
            ],
        ),
        "openai.response.system_fingerprint": AttributeSpec(
            name="openai.response.system_fingerprint",
            requirement=RECOMMENDED,
            type="string",
        ),
        "server.address": AttributeSpec(
            name="server.address",
            requirement=RECOMMENDED,
            type="string",
        ),
        "server.port": AttributeSpec(
            name="server.port",
            requirement=OPTIONAL,
            type="int",
        ),
    }

    def __iter__(self) -> Iterator[AttributeSpec]:
        return iter(self.SUPPORTED_ATTRIBUTES.values())
