"""PostgreSQL tool configuration."""

from __future__ import annotations

from typing import TYPE_CHECKING

from mindroom.tool_system.declarations import ConfigField, SetupType, ToolCategory, ToolStatus
from mindroom.tool_system.registration import register_tool_with_metadata

if TYPE_CHECKING:
    from agno.tools.postgres import PostgresTools


@register_tool_with_metadata(
    name="postgres",
    display_name="PostgreSQL",
    description="Query PostgreSQL databases - list tables, describe schemas, run SQL queries, and export data",
    category=ToolCategory.DEVELOPMENT,
    status=ToolStatus.REQUIRES_CONFIG,
    setup_type=SetupType.SPECIAL,
    icon="SiPostgresql",
    icon_color="text-blue-700",
    config_fields=[
        ConfigField(
            name="connection",
            label="Connection",
            type="text",
            required=False,
            default=None,
            description="Programmatic only: pass an existing psycopg connection object (not usable from UI).",
        ),
        ConfigField(
            name="host",
            label="Host",
            type="url",
            required=True,
            placeholder="localhost",
            description="PostgreSQL server hostname",
        ),
        ConfigField(
            name="port",
            label="Port",
            type="number",
            required=False,
            default=5432,
        ),
        ConfigField(
            name="db_name",
            label="Database Name",
            type="text",
            required=True,
            placeholder="mydb",
        ),
        ConfigField(
            name="user",
            label="Username",
            type="text",
            required=True,
            placeholder="postgres",
        ),
        ConfigField(
            name="password",
            label="Password",
            type="password",
            required=True,
        ),
        ConfigField(
            name="table_schema",
            label="Table Schema",
            type="text",
            required=False,
            default="public",
        ),
    ],
    dependencies=["psycopg"],
    docs_url="https://docs.agno.com/tools/toolkits/others/postgres",
    function_names=(
        "describe_table",
        "export_table_to_path",
        "inspect_query",
        "run_query",
        "show_tables",
        "summarize_table",
    ),
)
def postgres_tools() -> type[PostgresTools]:
    """Return PostgreSQL tools for database operations."""
    from agno.tools.postgres import PostgresTools

    return PostgresTools
