"""
Helper functions for appending A2A agents to model lists.

Used by proxy model endpoints to make agents appear in UI alongside models.
"""

from litellm._logging import verbose_proxy_logger
from litellm.proxy._types import UserAPIKeyAuth
from litellm.types.proxy.management_endpoints.model_management_endpoints import (
    ModelGroupInfoProxy,
)


async def append_agents_to_model_group(
    model_groups: list[ModelGroupInfoProxy],
    user_api_key_dict: UserAPIKeyAuth,
) -> list[ModelGroupInfoProxy]:
    """
    Append A2A agents to model groups list for UI display.

    Converts agents to model format with "a2a/<agent-name>" naming
    so they appear in playground and work with LiteLLM routing.
    """
    try:
        from litellm.proxy.agent_endpoints.agent_registry import global_agent_registry
        from litellm.proxy.agent_endpoints.auth.agent_permission_handler import (
            AgentRequestHandler,
            RestrictedAgentAccess,
        )

        match await AgentRequestHandler.resolve_agent_access(user_api_key_auth=user_api_key_dict):
            case RestrictedAgentAccess(allowed_agent_ids):
                for agent_id in allowed_agent_ids:
                    agent = global_agent_registry.get_agent_by_id(agent_id)
                    if agent is not None:
                        model_groups.append(
                            ModelGroupInfoProxy(
                                model_group=f"a2a/{agent.agent_name}",
                                mode="chat",
                                providers=["a2a"],
                            )
                        )
            case _:
                pass
    except Exception as e:
        verbose_proxy_logger.debug("Error appending agents to model_group/info: %s", e)

    return model_groups


async def append_agents_to_model_info(
    models: list[dict],
    user_api_key_dict: UserAPIKeyAuth,
) -> list[dict]:
    """
    Append A2A agents to model info list for UI display.

    Converts agents to model format with "a2a/<agent-name>" naming
    so they appear in models page and work with LiteLLM routing.
    """
    try:
        from litellm.proxy.agent_endpoints.agent_registry import global_agent_registry
        from litellm.proxy.agent_endpoints.auth.agent_permission_handler import (
            AgentRequestHandler,
            RestrictedAgentAccess,
        )

        match await AgentRequestHandler.resolve_agent_access(user_api_key_auth=user_api_key_dict):
            case RestrictedAgentAccess(allowed_agent_ids):
                for agent_id in allowed_agent_ids:
                    agent = global_agent_registry.get_agent_by_id(agent_id)
                    if agent is not None:
                        models.append(
                            {
                                "model_name": f"a2a/{agent.agent_name}",
                                "litellm_params": {
                                    "model": f"a2a/{agent.agent_name}",
                                    "custom_llm_provider": "a2a",
                                },
                                "model_info": {
                                    "id": agent.agent_id,
                                    "mode": "chat",
                                    "db_model": True,
                                    "created_by": agent.created_by,
                                    "created_at": agent.created_at,
                                    "updated_at": agent.updated_at,
                                },
                            }
                        )
            case _:
                pass
    except Exception as e:
        verbose_proxy_logger.debug("Error appending agents to v2/model/info: %s", e)

    return models
