from typing import Any, Dict, Tuple
from functools import partial

from mcp import types as mcp_types

from ..logging import get_logger
from ..meta_client import MetaClient
from ..schemas import ToolResult
from .base import ToolHandler

# Import from the refactored module
from ..business_objects import (
    list_businesses_handler,
    get_business_handler,
    list_business_users_handler,
    list_business_pages_handler,
    list_business_adaccounts_handler
)

logger = get_logger(__name__)

# We can reuse the handlers directly since they now accept client and return ToolResult.
# But we need to ensure the signatures match what get_tools expects (Callable[[MetaClient, Dict], ToolResult] wrapped via partial)

# The imported handlers are: handler(arguments, client=None)
# We want to use partial(handler, client=client) which will pass client as the keyword argument?
# Or partial(handler, client=client) ??
# Wait, partial(func, arg1) passes arg1 as the first argument.
# My handlers are defined as: async def handler(arguments, client=None).
# If I do partial(handler, client), it will pass client as `arguments`. That's WRONG.
#
# I should wrap them to switch argument order or use keyword args correctly.
# Standard signature in tools/base.py or other modules I made:
# async def handler(client: MetaClient, arguments: Dict[str, Any]) -> ToolResult
#
# So I should write wrappers here to swap the arguments.

async def wrap_list_businesses(client: MetaClient, arguments: Dict[str, Any]) -> ToolResult:
    return await list_businesses_handler(arguments, client=client)

async def wrap_get_business(client: MetaClient, arguments: Dict[str, Any]) -> ToolResult:
    return await get_business_handler(arguments, client=client)

async def wrap_list_business_users(client: MetaClient, arguments: Dict[str, Any]) -> ToolResult:
    return await list_business_users_handler(arguments, client=client)

async def wrap_list_business_pages(client: MetaClient, arguments: Dict[str, Any]) -> ToolResult:
    return await list_business_pages_handler(arguments, client=client)

async def wrap_list_business_adaccounts(client: MetaClient, arguments: Dict[str, Any]) -> ToolResult:
    return await list_business_adaccounts_handler(arguments, client=client)

def get_tools(client: MetaClient) -> Dict[str, Tuple[mcp_types.Tool, ToolHandler]]:
    return {
        "meta.business.list": (
            mcp_types.Tool(
                name="meta.business.list",
                description="List all businesses accessible to your account. View Business Manager portfolio. Use for business discovery.",
                inputSchema={
                    "type": "object",
                    "properties": {},
                },
            ),
            partial(wrap_list_businesses, client),
        ),
        "meta.business.get": (
            mcp_types.Tool(
                name="meta.business.get",
                description="Get business details including name, verification status, and settings. View Business Manager configuration. Use for business inspection.",
                inputSchema={
                    "type": "object",
                    "properties": {
                        "business_id": {"type": "string", "description": "Business ID"},
                    },
                    "required": ["business_id"],
                },
            ),
            partial(wrap_get_business, client),
        ),
        "meta.business.list_users": (
            mcp_types.Tool(
                name="meta.business.list_users",
                description="List business users with roles and permissions. View team members and access levels. Use for team management.",
                inputSchema={
                    "type": "object",
                    "properties": {
                        "business_id": {"type": "string", "description": "Business ID"},
                    },
                    "required": ["business_id"],
                },
            ),
            partial(wrap_list_business_users, client),
        ),
        "meta.business.list_pages": (
            mcp_types.Tool(
                name="meta.business.list_pages",
                description="List Facebook Pages owned or managed by the business. View page portfolio. Use for page discovery.",
                inputSchema={
                    "type": "object",
                    "properties": {
                        "business_id": {"type": "string", "description": "Business ID"},
                    },
                    "required": ["business_id"],
                },
            ),
            partial(wrap_list_business_pages, client),
        ),
        "meta.business.list_ad_accounts": (
            mcp_types.Tool(
                name="meta.business.list_ad_accounts",
                description="List ad accounts owned or shared with the business. View account portfolio. Use for account discovery.",
                inputSchema={
                    "type": "object",
                    "properties": {
                        "business_id": {"type": "string", "description": "Business ID"},
                    },
                    "required": ["business_id"],
                },
            ),
            partial(wrap_list_business_adaccounts, client),
        ),
    }
