"""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.)

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

# Import generic tools from MACSDK
from macsdk.tools import api_get, 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 the tools for this agent, ensuring API is registered.

    Returns:
        List of generic tools from MACSDK.
    """
    _ensure_api_registered()
    return [api_get, fetch_file]


# Tools exposed for CLI inspection
TOOLS = [api_get, fetch_file]


# =============================================================================
# CUSTOMIZATION
# =============================================================================
#
# To use your own API:
#
# 1. Modify _ensure_api_registered() to register your API:
#
#    register_api_service(
#        name="myapi",
#        base_url="https://api.example.com",
#        token=os.environ.get("API_TOKEN"),  # Optional auth
#        timeout=30,
#    )
#
# 2. Update the system prompt (prompts.py) to describe your API endpoints
#
# 3. The LLM will use api_get with your endpoints automatically!
#
# For creating custom tools instead of using generic ones,
# see examples/api-agent/ for a complete example.
