"""Tools registry for all available Agno tools.

This module provides a centralized registry for all tools that can be used by agents.
Tools are registered by string name and can be instantiated dynamically when loading agents.
"""

from __future__ import annotations

from typing import TYPE_CHECKING

from mindroom.tool_system.declarations import (
    ConfigField,
    SetupType,
    ToolCategory,
    ToolManagedInitArg,
    ToolStatus,
)
from mindroom.tool_system.registration import register_tool_with_metadata
from mindroom.tools import (
    compact_context,  # noqa: F401
    delegate,  # noqa: F401
    dynamic_tools,  # noqa: F401
    dynamic_workflow,  # noqa: F401
    memory,  # noqa: F401
    report_publishing,  # noqa: F401
    self_config,  # noqa: F401
)
from mindroom.tools.agentql import agentql_tools
from mindroom.tools.airflow import airflow_tools
from mindroom.tools.apify import apify_tools
from mindroom.tools.approved_egress import approved_egress_tools
from mindroom.tools.arxiv import arxiv_tools
from mindroom.tools.attachments import attachments_tools
from mindroom.tools.aws_lambda import aws_lambda_tools
from mindroom.tools.aws_ses import aws_ses_tools
from mindroom.tools.baidusearch import baidusearch_tools
from mindroom.tools.bitbucket import bitbucket_tools
from mindroom.tools.brandfetch import brandfetch_tools
from mindroom.tools.brightdata import brightdata_tools
from mindroom.tools.browser import browser_tools
from mindroom.tools.browserbase import browserbase_tools
from mindroom.tools.cal_com import cal_com_tools
from mindroom.tools.calculator import calculator_tools
from mindroom.tools.callback_manager import callback_manager_tools
from mindroom.tools.cartesia import cartesia_tools
from mindroom.tools.claude_agent import claude_agent_tools
from mindroom.tools.clickup import clickup_tools
from mindroom.tools.coding import coding_tools
from mindroom.tools.composio import composio_tools
from mindroom.tools.config_manager import config_manager_tools
from mindroom.tools.confluence import confluence_tools
from mindroom.tools.crawl4ai import crawl4ai_tools
from mindroom.tools.csv import csv_tools
from mindroom.tools.custom_api import custom_api_tools
from mindroom.tools.dalle import dalle_tools
from mindroom.tools.daytona import daytona_tools
from mindroom.tools.desi_vocal import desi_vocal_tools
from mindroom.tools.desktop import desktop_tools
from mindroom.tools.discord import discord_tools
from mindroom.tools.docker import docker_tools
from mindroom.tools.duckdb import duckdb_tools
from mindroom.tools.duckduckgo import duckduckgo_tools
from mindroom.tools.e2b import e2b_tools
from mindroom.tools.eleven_labs import eleven_labs_tools
from mindroom.tools.email import email_tools
from mindroom.tools.exa import exa_tools
from mindroom.tools.external_trigger_manager import external_trigger_manager_tools
from mindroom.tools.fal import fal_tools
from mindroom.tools.file import file_tools
from mindroom.tools.file_generation import file_generation_tools
from mindroom.tools.financial_datasets_api import financial_datasets_api_tools
from mindroom.tools.firecrawl import firecrawl_tools
from mindroom.tools.gemini import gemini_tools
from mindroom.tools.giphy import giphy_tools
from mindroom.tools.github import github_tools
from mindroom.tools.gmail import gmail_tools
from mindroom.tools.google_bigquery import google_bigquery_tools
from mindroom.tools.google_calendar import google_calendar_tools
from mindroom.tools.google_docs import google_docs_tools
from mindroom.tools.google_drive import google_drive_tools
from mindroom.tools.google_maps import google_maps_tools
from mindroom.tools.google_scholar import google_scholar_tools
from mindroom.tools.google_sheets import google_sheets_tools
from mindroom.tools.googlesearch import googlesearch_tools
from mindroom.tools.groq import groq_tools
from mindroom.tools.hackernews import hackernews_tools
from mindroom.tools.jina import jina_tools
from mindroom.tools.jira import jira_tools
from mindroom.tools.linear import linear_tools
from mindroom.tools.linkup import linkup_tools
from mindroom.tools.lumalabs import lumalabs_tools
from mindroom.tools.matrix_api import matrix_api_tools
from mindroom.tools.matrix_message import matrix_message_tools
from mindroom.tools.matrix_room import matrix_room_tools
from mindroom.tools.matrix_voice_message import matrix_voice_message_tools
from mindroom.tools.mem0 import mem0_tools
from mindroom.tools.modelslabs import modelslabs_tools
from mindroom.tools.moviepy_video_tools import moviepy_video_tools
from mindroom.tools.neo4j import neo4j_tools
from mindroom.tools.newspaper4k import newspaper4k_tools
from mindroom.tools.notion import notion_tools
from mindroom.tools.openai import openai_tools
from mindroom.tools.openbb import openbb_tools
from mindroom.tools.openweather import openweather_tools
from mindroom.tools.oxylabs import oxylabs_tools
from mindroom.tools.pandas import pandas_tools
from mindroom.tools.postgres import postgres_tools
from mindroom.tools.pubmed import pubmed_tools
from mindroom.tools.python import python_tools
from mindroom.tools.reasoning import reasoning_tools
from mindroom.tools.reddit import reddit_tools
from mindroom.tools.redshift import redshift_tools
from mindroom.tools.replicate import replicate_tools
from mindroom.tools.resend import resend_tools
from mindroom.tools.scheduler import scheduler_tools
from mindroom.tools.scrapegraph import scrapegraph_tools
from mindroom.tools.searxng import searxng_tools
from mindroom.tools.serpapi import serpapi_tools
from mindroom.tools.serper import serper_tools
from mindroom.tools.shell import shell_tools
from mindroom.tools.shopify import shopify_tools
from mindroom.tools.slack import slack_tools
from mindroom.tools.sleep import sleep_tools
from mindroom.tools.spider import spider_tools
from mindroom.tools.spotify import spotify_tools
from mindroom.tools.sql import sql_tools
from mindroom.tools.subagents import subagents_tools
from mindroom.tools.tavily import tavily_tools
from mindroom.tools.telegram import telegram_tools
from mindroom.tools.thread_model import thread_model_tools
from mindroom.tools.thread_resolution import thread_resolution_tools
from mindroom.tools.thread_summary import register_thread_summary_tools
from mindroom.tools.thread_tags import thread_tags_tools
from mindroom.tools.todo import todo_tools
from mindroom.tools.todoist import todoist_tools
from mindroom.tools.trafilatura import trafilatura_tools
from mindroom.tools.trello import trello_tools
from mindroom.tools.twilio import twilio_tools
from mindroom.tools.unsplash import unsplash_tools
from mindroom.tools.update_awareness import update_awareness_tools
from mindroom.tools.visualization import visualization_tools
from mindroom.tools.web_browser_tools import web_browser_tools
from mindroom.tools.webex import webex_tools
from mindroom.tools.website import website_tools
from mindroom.tools.whatsapp import whatsapp_tools
from mindroom.tools.wikipedia import wikipedia_tools
from mindroom.tools.x import x_tools
from mindroom.tools.yfinance import yfinance_tools
from mindroom.tools.youtube import youtube_tools
from mindroom.tools.zendesk import zendesk_tools
from mindroom.tools.zep import zep_tools
from mindroom.tools.zoom import zoom_tools

if TYPE_CHECKING:
    from agno.tools import Toolkit


__all__ = [
    "agentql_tools",
    "airflow_tools",
    "apify_tools",
    "approved_egress_tools",
    "arxiv_tools",
    "attachments_tools",
    "aws_lambda_tools",
    "aws_ses_tools",
    "baidusearch_tools",
    "bitbucket_tools",
    "brandfetch_tools",
    "brightdata_tools",
    "browser_tools",
    "browserbase_tools",
    "cal_com_tools",
    "calculator_tools",
    "callback_manager_tools",
    "cartesia_tools",
    "claude_agent_tools",
    "clickup_tools",
    "coding_tools",
    "composio_tools",
    "config_manager_tools",
    "confluence_tools",
    "crawl4ai_tools",
    "csv_tools",
    "custom_api_tools",
    "dalle_tools",
    "daytona_tools",
    "desi_vocal_tools",
    "desktop_tools",
    "discord_tools",
    "docker_tools",
    "duckdb_tools",
    "duckduckgo_tools",
    "e2b_tools",
    "eleven_labs_tools",
    "email_tools",
    "exa_tools",
    "external_trigger_manager_tools",
    "fal_tools",
    "file_generation_tools",
    "file_tools",
    "financial_datasets_api_tools",
    "firecrawl_tools",
    "gemini_tools",
    "giphy_tools",
    "github_tools",
    "gmail_tools",
    "google_bigquery_tools",
    "google_calendar_tools",
    "google_docs_tools",
    "google_drive_tools",
    "google_maps_tools",
    "google_scholar_tools",
    "google_sheets_tools",
    "googlesearch_tools",
    "groq_tools",
    "hackernews_tools",
    "jina_tools",
    "jira_tools",
    "linear_tools",
    "linkup_tools",
    "lumalabs_tools",
    "matrix_api_tools",
    "matrix_message_tools",
    "matrix_room_tools",
    "matrix_voice_message_tools",
    "mem0_tools",
    "modelslabs_tools",
    "moviepy_video_tools",
    "neo4j_tools",
    "newspaper4k_tools",
    "notion_tools",
    "openai_tools",
    "openbb_tools",
    "openweather_tools",
    "oxylabs_tools",
    "pandas_tools",
    "postgres_tools",
    "pubmed_tools",
    "python_tools",
    "reasoning_tools",
    "reddit_tools",
    "redshift_tools",
    "register_thread_summary_tools",
    "replicate_tools",
    "resend_tools",
    "scheduler_tools",
    "scrapegraph_tools",
    "searxng_tools",
    "serpapi_tools",
    "serper_tools",
    "shell_tools",
    "shopify_tools",
    "slack_tools",
    "sleep_tools",
    "spider_tools",
    "spotify_tools",
    "sql_tools",
    "subagents_tools",
    "tavily_tools",
    "telegram_tools",
    "thread_model_tools",
    "thread_resolution_tools",
    "thread_tags_tools",
    "todo_tools",
    "todoist_tools",
    "trafilatura_tools",
    "trello_tools",
    "twilio_tools",
    "unsplash_tools",
    "update_awareness_tools",
    "visualization_tools",
    "web_browser_tools",
    "webex_tools",
    "website_tools",
    "whatsapp_tools",
    "wikipedia_tools",
    "x_tools",
    "yfinance_tools",
    "youtube_tools",
    "zendesk_tools",
    "zep_tools",
    "zoom_tools",
]


@register_tool_with_metadata(
    name="openclaw_compat",
    display_name="OpenClaw Compat",
    description="Convenience bundle that implies shell, coding, browser, and other common tools",
    category=ToolCategory.DEVELOPMENT,
    icon="Workflow",
    icon_color="text-orange-500",
    helper_text="Implies: shell, coding, duckduckgo, website, browser, scheduler, subagents, matrix_message, attachments.",
)
def _openclaw_compat_tools() -> type[Toolkit]:
    """Return an empty toolkit — the real tools are loaded via tool preset expansion."""
    from agno.tools import Toolkit

    return Toolkit


@register_tool_with_metadata(
    name="homeassistant",
    display_name="Home Assistant",
    description="Control and monitor smart home devices",
    category=ToolCategory.SMART_HOME,
    icon="Home",
    icon_color="text-blue-500",
    dependencies=["httpx"],
    status=ToolStatus.REQUIRES_CONFIG,
    setup_type=SetupType.SPECIAL,
    managed_init_args=(
        ToolManagedInitArg.CREDENTIALS_MANAGER,
        ToolManagedInitArg.WORKER_TARGET,
    ),
    config_fields=[
        ConfigField(
            name="HOMEASSISTANT_URL",
            label="Home Assistant URL",
            type="url",
            required=True,
            placeholder="http://homeassistant.local:8123",
            description="URL to your Home Assistant instance",
        ),
        ConfigField(
            name="HOMEASSISTANT_TOKEN",
            label="Access Token",
            type="password",
            required=True,
            placeholder="Bearer token",
            description="Long-lived access token from Home Assistant",
        ),
        ConfigField(
            name="HOMEASSISTANT_ALLOW_PRIVATE_URL",
            label="Allow Private Home Assistant URL",
            type="boolean",
            required=False,
            default=False,
            description="Allow a trusted self-hosted Home Assistant URL on a private, local, or loopback network.",
        ),
    ],
    docs_url="https://www.home-assistant.io/integrations/",
    function_names=(
        "activate_scene",
        "call_service",
        "get_entity_state",
        "list_entities",
        "set_brightness",
        "set_color",
        "set_temperature",
        "toggle",
        "trigger_automation",
        "turn_off",
        "turn_on",
    ),
)
def _homeassistant_tools() -> type[Toolkit]:
    """Return Home Assistant tools for smart home control."""
    from mindroom.custom_tools.homeassistant import HomeAssistantTools

    return HomeAssistantTools


@register_tool_with_metadata(
    name="agent_vault_access",
    display_name="Agent Vault Access",
    description="Grant yourself UI access to manage this agent's Agent Vault secrets",
    category=ToolCategory.INTEGRATIONS,
    icon="Lock",
    icon_color="text-amber-600",
    dependencies=["httpx"],
    status=ToolStatus.REQUIRES_CONFIG,
    setup_type=SetupType.SPECIAL,
    managed_init_args=(
        ToolManagedInitArg.RUNTIME_PATHS,
        ToolManagedInitArg.WORKER_TARGET,
    ),
    config_fields=[
        ConfigField(
            name="MINDROOM_AGENT_VAULT_ACCESS_API_URL",
            label="Agent Vault API URL",
            type="url",
            required=True,
            placeholder="http://agent-vault:14321",
            description="Base URL of the Agent Vault server API.",
        ),
        ConfigField(
            name="MINDROOM_AGENT_VAULT_ACCESS_ADMIN_TOKEN",
            label="Agent Vault Admin Token",
            type="password",
            required=False,
            description=(
                "Instance-owner session or agent token used to create vaults, join them as "
                "vault-admin (the /join step is owner-only), and grant vault admin access. "
                "Provide this or the admin token file."
            ),
        ),
        ConfigField(
            name="MINDROOM_AGENT_VAULT_ACCESS_ADMIN_TOKEN_FILE",
            label="Agent Vault Admin Token File",
            type="text",
            required=False,
            placeholder="/etc/agent-vault-access/token",
            description=(
                "Path to a file holding the admin token, re-read on every call so an in-place "
                "Secret rotation takes effect without a restart. Takes precedence over the inline token."
            ),
        ),
        ConfigField(
            name="MINDROOM_AGENT_VAULT_ACCESS_UI_BASE_URL",
            label="Agent Vault UI Base URL",
            type="url",
            required=True,
            placeholder="https://example.com/agent-vault",
            description="Public base URL of the gated Agent Vault UI.",
        ),
        ConfigField(
            name="MINDROOM_AGENT_VAULT_ACCESS_EMAIL_DOMAIN",
            label="Account Email Domain",
            type="text",
            required=True,
            placeholder="example.com",
            description="Domain used to map a requester's Matrix localpart to their Agent Vault account email.",
        ),
        ConfigField(
            name="MINDROOM_AGENT_VAULT_ACCESS_VAULT_NAME_PREFIX",
            label="Vault Name Prefix",
            type="text",
            required=False,
            default="agent-vault",
            description="Prefix used to derive the per-worker vault name; must match workers.kubernetes.agentVault.vaultNamePrefix.",
        ),
        ConfigField(
            name="MINDROOM_AGENT_VAULT_ACCESS_OWNER_EMAIL",
            label="Agent Vault Owner Email",
            type="text",
            required=False,
            description=(
                "Agent Vault owner account used by Kubernetes worker init when minting proxy tokens. "
                "When set, self-service grants keep this account admin on the worker vault too."
            ),
        ),
    ],
    function_names=("request_vault_access",),
)
def _agent_vault_access_tools() -> type[Toolkit]:
    """Return the Agent Vault self-service access tool."""
    from mindroom.custom_tools.agent_vault_access import AgentVaultAccessTools

    return AgentVaultAccessTools
