#!/usr/bin/env python3
from pathlib import Path
from infiagent import infiagent
from tool_server_lite.tools.file_tools import BaseTool
import sys

APP_ROOT = Path(__file__).resolve().parents[2]
if str(APP_ROOT) not in sys.path:
    sys.path.insert(0, str(APP_ROOT))

from tool_runtime_helpers import get_user_data_root, inspect_agent_system_blocked_tools

class CheapClawListAgentSystemsTool(BaseTool):
    name = "cheapclaw_list_agent_systems"
    def execute(self, task_id, parameters):
        user_root = get_user_data_root()
        agent_root = user_root / "agent_library"
        visible_names = set()
        if agent_root.exists():
            visible_names = {item.name for item in agent_root.iterdir() if item.is_dir()}
        agent = infiagent(user_data_root=str(user_root), seed_builtin_resources=False)
        payload = agent.list_agent_systems()
        items = payload.get("agent_systems")
        if not isinstance(items, list):
            items = []
        filtered = []
        blocked = []
        for item in items:
            if not isinstance(item, dict):
                continue
            name = str(item.get("name") or "").strip()
            if name.endswith("_tmp"):
                continue
            if not name or name not in visible_names:
                continue
            inspection = inspect_agent_system_blocked_tools(name, user_data_root=user_root)
            if inspection.get("blocked"):
                blocked.append({
                    "name": name,
                    "blocked_tool_names": list(inspection.get("blocked_tool_names") or []),
                    "hits": list(inspection.get("hits") or []),
                })
                continue
            filtered.append(item)
        payload["agent_systems"] = filtered
        payload["runtime_agent_library_dir"] = str(agent_root)
        payload["visible_agent_system_names"] = [
            str(item.get("name") or "") for item in payload["agent_systems"] if str(item.get("name") or "")
        ]
        payload["blocked_agent_systems"] = blocked
        payload["recommended_defaults"] = {
            "CheapClawSupervisor": "supervisor_agent",
            "CheapClawWorkerGeneral": "worker_agent",
        }
        return payload
