"""Tools for the agent.

This agent uses MACSDK's generic tools directly:
- api_get: For REST API calls
- fetch_file: For downloading files (logs, configs, etc.)
- calculate: For mathematical calculations (LLMs are unreliable at math)
{% if with_knowledge %}
- Knowledge tools: read_skill, read_fact
{% endif %}

The API schema is described in the system prompt, letting the LLM
decide which endpoints to call based on user queries.
"""

from __future__ import annotations

from macsdk.tools import api_get, calculate, fetch_file

# =============================================================================
# SERVICE REGISTRATION
# =============================================================================

_api_registered = False


def _ensure_api_registered() -> None:
    """Register the API service on first use."""
    global _api_registered
    if not _api_registered:
        from macsdk.core.api_registry import register_api_service

        # Register DevOps Mock API as a service
        # Replace with your own API endpoint for production use
        register_api_service(
            name="devops",
            base_url="https://my-json-server.typicode.com/juanje/devops-mock-api",
            timeout=10,
            max_retries=2,
        )
        _api_registered = True


def get_tools() -> list:
    """Get all tools for this agent, ensuring API is registered.

    Includes:
    - Generic SDK tools (api_get, fetch_file, calculate)
{% if with_knowledge %}
    - Knowledge tools (read_skill, read_fact)
{% endif %}

    Note: calculate is included by default. LLMs are unreliable at math,
    so always keep this tool available. Do not remove it.

    Returns:
        List of all tools for this agent.
    """
    _ensure_api_registered()
{% if with_knowledge %}

    # Get knowledge tools configured for this package
    from macsdk.tools.knowledge import get_knowledge_bundle

    knowledge_tools, _ = get_knowledge_bundle(__package__)
{% endif %}

    return [
        api_get,
        fetch_file,
        calculate,  # Required - LLMs cannot do math reliably
{% if with_knowledge %}
        *knowledge_tools,  # Skills and facts tools
{% endif %}
    ]


# Tools exposed for CLI inspection
TOOLS = get_tools()


# =============================================================================
# CUSTOMIZATION
# =============================================================================
#
# To use your own API with authentication:
#
# 1. Add your credentials to config.py (the class already exists there):
#
#        api_token: SecretStr | None = None
#        api_base_url: str = "https://api.example.com"
#
# 2. Use them here in _ensure_api_registered():
#
#    from .config import config
#
#    register_api_service(
#        name="myapi",
#        base_url=config.api_base_url,
#        token=config.api_token.get_secret_value() if config.api_token else None,
#        timeout=30,
#    )
#
# 3. Update the system prompt (prompts.py) to describe your API endpoints
#
# 4. The LLM will use api_get with your endpoints automatically!
#
# Benefits of using config instead of os.environ.get():
# - Pydantic automatically loads values from .env file
# - Type validation and default values
# - Single source of truth for all configuration
# - No need for load_dotenv() in your code
#
# For creating custom tools instead of using generic ones,
# see examples/api-agent/ for a complete example.
