"""
Partnerships & Collaborative Ads Tools
Wraps the CollaborativeAdsEngine to provide tools for influencer marketing and UGC.
"""

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 ..collaborative_ads import get_collaborative_ads_engine
from .base import ToolHandler

async def create_branded_content_ad(
    client: MetaClient,
    arguments: Dict[str, Any]
) -> ToolResult:
    """Create ads from creator posts."""
    engine = get_collaborative_ads_engine()
    engine.client = client
    return await engine.create_branded_content_ad(arguments)

async def manage_creator_partnerships(
    client: MetaClient,
    arguments: Dict[str, Any]
) -> ToolResult:
    """Manage creator permissions."""
    engine = get_collaborative_ads_engine()
    engine.client = client
    return await engine.manage_creator_partnerships(arguments)

async def license_ugc_content(
    client: MetaClient,
    arguments: Dict[str, Any]
) -> ToolResult:
    """License UGC content."""
    engine = get_collaborative_ads_engine()
    engine.client = client
    return await engine.license_ugc_content(arguments)

async def track_influencer_performance(
    client: MetaClient,
    arguments: Dict[str, Any]
) -> ToolResult:
    """Track ROI and performance."""
    engine = get_collaborative_ads_engine()
    engine.client = client
    return await engine.track_influencer_performance(arguments)

async def discover_creators(
    client: MetaClient,
    arguments: Dict[str, Any]
) -> ToolResult:
    """Find creators via Marketplace."""
    engine = get_collaborative_ads_engine()
    engine.client = client
    return await engine.discover_creators_via_marketplace(arguments)

def get_tools(client: MetaClient) -> Dict[str, Tuple[mcp_types.Tool, ToolHandler]]:
    """Register partnership tools"""
    return {
        "meta.partnerships.create_ad": (
            mcp_types.Tool(
                name="meta.partnerships.create_ad",
                description="Create Partnership Ads from creator's organic posts. Run influencer content as paid ads with proper disclosure. Use for influencer marketing.",
                inputSchema={
                    "type": "object",
                    "properties": {
                        "ad_account_id": {"type": "string"},
                        "creator_post_id": {"type": "string"},
                        "campaign_name": {"type": "string"},
                        "daily_budget": {"type": "integer"},
                        "partnership_type": {"type": "string"}
                    },
                    "required": ["ad_account_id", "creator_post_id", "campaign_name"]
                }
            ),
            partial(create_branded_content_ad, client)
        ),
        "meta.partnerships.manage": (
            mcp_types.Tool(
                name="meta.partnerships.manage",
                description="Manage creator partnership invites and permissions. Grant/revoke branded content permissions. Use for creator relationship management.",
                inputSchema={
                    "type": "object",
                    "properties": {
                        "page_id": {"type": "string"},
                        "creator_instagram_id": {"type": "string"},
                        "permissions": {"type": "array", "items": {"type": "string"}}
                    },
                    "required": ["page_id", "creator_instagram_id"]
                }
            ),
            partial(manage_creator_partnerships, client)
        ),
        "meta.partnerships.license_ugc": (
            mcp_types.Tool(
                name="meta.partnerships.license_ugc",
                description="License user-generated content for advertising use. Obtain rights to customer reviews and posts. Use for UGC content licensing.",
                inputSchema={
                    "type": "object",
                    "properties": {
                        "content_owner_id": {"type": "string"},
                        "content_post_id": {"type": "string"},
                        "license_type": {"type": "string"}
                    },
                    "required": ["content_owner_id", "content_post_id"]
                }
            ),
            partial(license_ugc_content, client)
        ),
        "meta.partnerships.track_roi": (
            mcp_types.Tool(
                name="meta.partnerships.track_roi",
                description="Track influencer ROI, conversions, and audience overlap. Measure creator partnership performance. Use for influencer analytics.",
                inputSchema={
                    "type": "object",
                    "properties": {
                        "ad_account_id": {"type": "string"},
                        "creator_ids": {"type": "array", "items": {"type": "string"}}
                    },
                    "required": ["ad_account_id", "creator_ids"]
                }
            ),
            partial(track_influencer_performance, client)
        ),
        "meta.partnerships.discover": (
            mcp_types.Tool(
                name="meta.partnerships.discover",
                description="Find creators in Meta Creator Marketplace by category, audience, and budget. Discover influencer partners. Use for creator discovery.",
                inputSchema={
                    "type": "object",
                    "properties": {
                        "brand_category": {"type": "string"},
                        "budget_range": {"type": "object"},
                        "audience_size": {"type": "string"}
                    },
                    "required": ["brand_category"]
                }
            ),
            partial(discover_creators, client)
        )
    }
