"""Timmy's delegation tools — submit tasks and list agents.

Reads agent roster from agents.yaml via the loader module.
No hardcoded agent lists.
"""

import logging
from typing import Any

logger = logging.getLogger(__name__)


def delegate_task(
    agent_name: str, task_description: str, priority: str = "normal"
) -> dict[str, Any]:
    """Record a delegation intent to another agent.

    Args:
        agent_name: Name or ID of the agent to delegate to
        task_description: What you want the agent to do
        priority: Task priority - "low", "normal", "high"

    Returns:
        Dict with agent, status, and message
    """
    from timmy.agents.loader import list_agents

    agent_name = agent_name.lower().strip()

    # Build valid agents map from YAML config
    available = {a["id"]: a["role"] for a in list_agents()}

    if agent_name not in available:
        return {
            "success": False,
            "error": f"Unknown agent: {agent_name}. Valid agents: {', '.join(sorted(available))}",
            "task_id": None,
        }

    valid_priorities = ["low", "normal", "high"]
    if priority not in valid_priorities:
        priority = "normal"

    logger.info(
        "Delegation intent: %s → %s (priority=%s)", agent_name, task_description[:80], priority
    )

    return {
        "success": True,
        "task_id": None,
        "agent": agent_name,
        "role": available[agent_name],
        "status": "noted",
        "message": f"Delegation to {agent_name} ({available[agent_name]}): {task_description[:100]}",
    }


def list_swarm_agents() -> dict[str, Any]:
    """List all available sub-agents and their roles.

    Reads from agents.yaml — no hardcoded roster.

    Returns:
        Dict with agent list
    """
    try:
        from timmy.agents.loader import list_agents

        agents = list_agents()
        return {
            "success": True,
            "agents": [
                {
                    "name": a["name"],
                    "id": a["id"],
                    "role": a["role"],
                    "status": a.get("status", "available"),
                    "capabilities": ", ".join(a.get("tools", [])),
                }
                for a in agents
            ],
        }
    except Exception as e:
        logger.debug("Agent list unavailable: %s", e)
        return {
            "success": False,
            "error": str(e),
            "agents": [],
        }
