"""
WhatsApp Business & Messaging Tools
Wraps the WhatsAppBusinessEngine to provide tools for messaging commerce and chatbots.
"""

from typing import Dict, Any, Tuple
from functools import partial
import mcp.types as mcp_types

from ..meta_client import MetaClient
from ..schemas import ToolResult
from ..whatsapp_business import get_whatsapp_business_engine
from .base import ToolHandler

async def setup_whatsapp_business(
    client: MetaClient,
    arguments: Dict[str, Any]
) -> ToolResult:
    """Setup WhatsApp Business API."""
    engine = get_whatsapp_business_engine()
    engine.client = client
    return await engine.setup_business_api(arguments)

async def sync_product_catalog_to_whatsapp(
    client: MetaClient,
    arguments: Dict[str, Any]
) -> ToolResult:
    """Sync product catalog to WhatsApp."""
    engine = get_whatsapp_business_engine()
    engine.client = client
    return await engine.sync_catalog_to_whatsapp(arguments)

async def build_whatsapp_chatbot(
    client: MetaClient,
    arguments: Dict[str, Any]
) -> ToolResult:
    """Build automated chatbot flows."""
    engine = get_whatsapp_business_engine()
    engine.client = client
    return await engine.build_chatbot(arguments)

async def get_messaging_commerce_analytics(
    client: MetaClient,
    arguments: Dict[str, Any]
) -> ToolResult:
    """Get messaging funnel metrics."""
    engine = get_whatsapp_business_engine()
    engine.client = client
    return await engine.get_commerce_analytics(arguments)

def get_tools(client: MetaClient) -> Dict[str, Tuple[mcp_types.Tool, ToolHandler]]:
    """Register messaging tools"""
    return {
        "meta.messaging.setup": (
            mcp_types.Tool(
                name="meta.messaging.setup",
                description="Configure WhatsApp Business API for click-to-message ads. Enable templates, catalogs, and commerce features. Use for WhatsApp advertising setup.",
                inputSchema={
                    "type": "object",
                    "properties": {
                        "phone_number": {"type": "string", "description": "Business phone number"},
                        "business_account_id": {"type": "string", "description": "Business Manager ID"}
                    },
                    "required": ["phone_number", "business_account_id"]
                }
            ),
            partial(setup_whatsapp_business, client)
        ),
        "meta.messaging.sync_catalog": (
            mcp_types.Tool(
                name="meta.messaging.sync_catalog",
                description="Sync product catalog to WhatsApp for in-chat shopping. Enable product browsing and ordering. Use for WhatsApp commerce setup.",
                inputSchema={
                    "type": "object",
                    "properties": {
                        "catalog_id": {"type": "string", "description": "Product catalog ID"},
                        "product_count": {"type": "integer", "description": "Number of products to sync"}
                    },
                    "required": ["catalog_id"]
                }
            ),
            partial(sync_product_catalog_to_whatsapp, client)
        ),
        "meta.messaging.build_chatbot": (
            mcp_types.Tool(
                name="meta.messaging.build_chatbot",
                description="Create automated WhatsApp chatbot flows for support and commerce. Build greeting, FAQ, and order flows. Use for conversational automation.",
                inputSchema={
                    "type": "object",
                    "properties": {
                        "chatbot_name": {"type": "string", "description": "Name of the chatbot"},
                        "flows": {"type": "array", "items": {"type": "string"}, "description": "List of flows (greeting, support, etc)"}
                    },
                    "required": ["chatbot_name"]
                }
            ),
            partial(build_whatsapp_chatbot, client)
        ),
        "meta.messaging.get_analytics": (
            mcp_types.Tool(
                name="meta.messaging.get_analytics",
                description="Get WhatsApp messaging funnel metrics including message volume, response rates, and conversions. Track messaging ROI. Use for messaging performance analysis.",
                inputSchema={
                    "type": "object",
                    "properties": {
                        "phone_number_id": {"type": "string", "description": "WhatsApp Phone ID"},
                        "time_range": {"type": "object", "description": "Time range for analysis"}
                    },
                    "required": ["phone_number_id"]
                }
            ),
            partial(get_messaging_commerce_analytics, client)
        )
    }
