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 ..lead_ads_tools import (
    create_leadgen_form_handler,
    get_leadgen_form_handler,
    update_leadgen_form_handler,
    delete_leadgen_form_handler,
    list_leadgen_leads_handler,
    export_leadgen_leads_handler,
    preview_leadgen_form_handler,
    test_leadgen_form_handler
)

logger = get_logger(__name__)

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

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

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

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

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

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

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

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

def get_tools(client: MetaClient) -> Dict[str, Tuple[mcp_types.Tool, ToolHandler]]:
    return {
        "meta.leads.create_form": (
            mcp_types.Tool(
                name="meta.leads.create_form",
                description="Create lead generation form for Facebook/Instagram Lead Ads. Define questions, privacy policy, and thank you screen. Use for lead capture setup.",
                inputSchema={
                    "type": "object",
                    "properties": {
                        "page_id": {"type": "string"},
                        "name": {"type": "string"},
                        "questions": {"type": "array"},
                        "privacy_policy_url": {"type": "string"},
                        "locale": {"type": "string"},
                    },
                    "required": ["page_id", "name", "privacy_policy_url"],
                },
            ),
            partial(wrap_create_leadgen_form, client),
        ),
        "meta.leads.get_form": (
            mcp_types.Tool(
                name="meta.leads.get_form",
                description="Get lead generation form details and configuration. View questions, status, and settings. Use for form management.",
                inputSchema={
                    "type": "object",
                    "properties": {
                        "form_id": {"type": "string"},
                    },
                    "required": ["form_id"],
                },
            ),
            partial(wrap_get_leadgen_form, client),
        ),
        "meta.leads.update_form": (
            mcp_types.Tool(
                name="meta.leads.update_form",
                description="Update lead generation form name or status. Modify form settings without recreating. Use for form maintenance.",
                inputSchema={
                    "type": "object",
                    "properties": {
                        "form_id": {"type": "string"},
                        "name": {"type": "string"},
                        "status": {"type": "string"},
                    },
                    "required": ["form_id"],
                },
            ),
            partial(wrap_update_leadgen_form, client),
        ),
        "meta.leads.delete_form": (
            mcp_types.Tool(
                name="meta.leads.delete_form",
                description="Delete lead generation form and remove from campaigns. Archive inactive forms. Use for form cleanup.",
                inputSchema={
                    "type": "object",
                    "properties": {
                        "form_id": {"type": "string"},
                    },
                    "required": ["form_id"],
                },
            ),
            partial(wrap_delete_leadgen_form, client),
        ),
        "meta.leads.list_leads": (
            mcp_types.Tool(
                name="meta.leads.list_leads",
                description="List leads from form with contact details and responses. View lead data for CRM sync. Use for lead retrieval and management.",
                inputSchema={
                    "type": "object",
                    "properties": {
                        "form_id": {"type": "string"},
                        "limit": {"type": "integer"},
                    },
                    "required": ["form_id"],
                },
            ),
            partial(wrap_list_leadgen_leads, client),
        ),
        "meta.leads.export": (
            mcp_types.Tool(
                name="meta.leads.export",
                description="Export leads from form to CSV or JSON format. Bulk download for CRM import. Use for data export and integration.",
                inputSchema={
                    "type": "object",
                    "properties": {
                        "form_id": {"type": "string"},
                        "format": {"type": "string", "enum": ["csv", "json"]},
                    },
                    "required": ["form_id"],
                },
            ),
            partial(wrap_export_leadgen_leads, client),
        ),
        "meta.leads.preview_form": (
            mcp_types.Tool(
                name="meta.leads.preview_form",
                description="Get preview URL for lead form to test appearance and flow. Preview before launching ads. Use for form QA testing.",
                inputSchema={
                    "type": "object",
                    "properties": {
                        "form_id": {"type": "string"},
                    },
                    "required": ["form_id"],
                },
            ),
            partial(wrap_preview_leadgen_form, client),
        ),
        "meta.leads.test_submit": (
            mcp_types.Tool(
                name="meta.leads.test_submit",
                description="Test submit lead to form for webhook and CRM integration testing. Validate data flow. Use for integration testing.",
                inputSchema={
                    "type": "object",
                    "properties": {
                        "form_id": {"type": "string"},
                        "field_data": {"type": "array"},
                    },
                    "required": ["form_id"],
                },
            ),
            partial(wrap_test_leadgen_form, client),
        ),
    }
