"""
tools/cli - CLI tool package for Mugi.

Consolidates all CLI-related agent tools: Homebrew management, ffmpeg media
processing, a generic CLI runner, and isolated pip venvs.

Entry point: get_cli_tools() — returns all @tool callables in this package.
Called lazily by tools/__init__.py when include_cli=True.

Install paths managed here:
  ~/.mugi/homebrew/         — Mugi's isolated Homebrew (brew-installed CLIs)
  ~/.mugi/cli-venvs/<name>/ — isolated pip venvs, one per Python CLI package

See persistence.CLI_VENVS_DIR and tools/cli/venvs.py for pip venv helpers.
See tools/cli/runner.py for cli_rich_path() (shared PATH used by shell_execute too).
"""
from __future__ import annotations


def get_cli_tools() -> list:
    """Return all @tool callables from the CLI submodules.
    Imported lazily by tools/__init__.py; only loads when include_cli=True."""
    from tools.cli import brew, ffmpeg, runner

    _BREW_TOOLS = ["ensure_mugi_homebrew", "mugi_brew_install"]
    _FFMPEG_TOOLS = [
        "get_media_info", "convert_video", "extract_audio", "convert_audio",
        "trim_media", "concat_media", "extract_frame", "merge_audio_video",
        "remove_audio", "extract_subtitles", "burn_subtitles", "split_media",
        "change_speed", "rotate_video", "images_to_video", "extract_frames_interval",
        "normalize_audio",
    ]
    _RUNNER_TOOLS = ["run_installed_cli", "install_cli_package"]

    seen: set[str] = set()
    tools: list = []

    for mod, names in [
        (brew, _BREW_TOOLS),
        (ffmpeg, _FFMPEG_TOOLS),
        (runner, _RUNNER_TOOLS),
    ]:
        for name in names:
            obj = getattr(mod, name, None)
            if obj is None:
                continue
            if not (hasattr(obj, "args_schema") and (callable(obj) or hasattr(obj, "invoke"))):
                continue
            tool_name = getattr(obj, "name", name)
            if tool_name not in seen:
                seen.add(tool_name)
                tools.append(obj)

    return tools


# Re-export helpers used by external modules (e.g. mugi_backend, searxng_docker)
from tools.cli.runner import cli_rich_path, find_cli_binary
from tools.cli.venvs import ensure_cli_venv, get_cli_venv_dir, get_cli_venv_python, get_cli_venv_bin, list_cli_venvs

__all__ = [
    "get_cli_tools",
    "cli_rich_path",
    "find_cli_binary",
    "ensure_cli_venv",
    "get_cli_venv_dir",
    "get_cli_venv_python",
    "get_cli_venv_bin",
    "list_cli_venvs",
]
