"""
Central function registry — FunctionSchema and ToolsSchema definitions.

Tools:
  - generate_image: Replicate (google/nano-banana-pro)
  - generate_video: Replicate (wan-video/wan-2.2-t2v-fast)
  - web_search: Google Gemini grounded search (general information)
  - shopping_search: SerpAPI Google Shopping (product catalog)
"""

from pipecat.adapters.schemas.function_schema import FunctionSchema
from pipecat.adapters.schemas.tools_schema import ToolsSchema

# ---------------------------------------------------------------------------
# 1. Image Generation (Replicate — google/nano-banana-pro)
# ---------------------------------------------------------------------------
generate_image_function = FunctionSchema(
    name="generate_image",
    description=(
        "Generate images from text descriptions using AI. "
        "Call this when the user asks to create, generate, or make an image, "
        "picture, photo, illustration, or artwork. "
        "Always generate at least 2 images. Each image MUST have a DIFFERENT "
        "prompt with a unique angle, view, style, or perspective. "
        "For example if the user says 'Taj Mahal images', provide prompts like: "
        "'Taj Mahal front view at sunrise with golden light', "
        "'Taj Mahal aerial view from above with gardens', "
        "'Taj Mahal close-up of marble details and arches'. "
        "NEVER use the same prompt for multiple images."
    ),
    properties={
        "prompts": {
            "type": "array",
            "items": {"type": "string"},
            "description": (
                "An array of 2-7 DIFFERENT detailed English prompts, one per image. "
                "Each prompt should describe a unique angle, view, style, mood, "
                "or perspective of what the user asked for. Translate to English "
                "if needed. Minimum 2 prompts, maximum 7."
            ),
        },
        "aspect_ratio": {
            "type": "string",
            "enum": ["1:1", "16:9", "9:16", "4:3", "3:4"],
            "description": (
                "The aspect ratio of the generated images. "
                "Default to 1:1 if the user does not specify."
            ),
        },
    },
    required=["prompts"],
)

# ---------------------------------------------------------------------------
# 2. Video Generation (Replicate — wan-video/wan-2.2-t2v-fast)
# ---------------------------------------------------------------------------
generate_video_function = FunctionSchema(
    name="generate_video",
    description=(
        "Generate a short video from a text description using AI. "
        "Call this when the user asks to create, generate, or make a video, "
        "animation, or clip. Video generation takes 30-60 seconds."
    ),
    properties={
        "prompt": {
            "type": "string",
            "description": (
                "A detailed English description of the video to generate. "
                "Translate the user's request to English if needed."
            ),
        },
    },
    required=["prompt"],
)

# ---------------------------------------------------------------------------
# 3. Web Search (Google Gemini with search grounding — general info)
# ---------------------------------------------------------------------------
web_search_function = FunctionSchema(
    name="web_search",
    description=(
        "Search the web for real-time information using Google Search. "
        "Call this when the user asks to search for something, look up "
        "information, find news, check facts, get weather, or asks any "
        "factual question you are unsure about. Do NOT use this for "
        "shopping or product searches — use shopping_search instead."
    ),
    properties={
        "query": {
            "type": "string",
            "description": (
                "The search query in English. Translate the user's request "
                "to English if needed for better search results."
            ),
        },
    },
    required=["query"],
)

# ---------------------------------------------------------------------------
# 4. Shopping Search (SerpAPI Google Shopping — product catalog)
# ---------------------------------------------------------------------------
shopping_search_function = FunctionSchema(
    name="shopping_search",
    description=(
        "Search for products to buy using Google Shopping. Returns a catalog "
        "of products with images, prices, ratings, and buy links. "
        "Call this when the user wants to shop, buy something, find products, "
        "compare prices, look for deals, or asks about product availability. "
        "Examples: 'buy Nike shoes', 'iPhone price', 'best headphones under "
        "5000', 'find me a laptop'."
    ),
    properties={
        "query": {
            "type": "string",
            "description": (
                "The product search query in English. Translate the user's "
                "request to English if needed. Be specific — include brand, "
                "product type, and any filters like price range."
            ),
        },
    },
    required=["query"],
)

# ---------------------------------------------------------------------------
# 5. Edit Image (Replicate — flux-2-klein-4b with uploaded image)
# ---------------------------------------------------------------------------
edit_image_function = FunctionSchema(
    name="edit_image",
    description=(
        "Edit a user-uploaded photo based on their spoken instruction. "
        "Call this ONLY when the user has uploaded a photo and asks to edit, "
        "modify, change, or transform it. Examples: 'background change karo', "
        "'make it black and white', 'add sunglasses', 'make it look like a painting'. "
        "Do NOT call this if no photo has been uploaded."
    ),
    properties={
        "edit_instruction": {
            "type": "string",
            "description": (
                "The user's edit instruction in English. Translate from Hindi "
                "if needed. Be specific about what to change. For example: "
                "'Change the background to a tropical beach with sunset', "
                "'Convert to black and white pencil sketch style'."
            ),
        },
    },
    required=["edit_instruction"],
)

# ---------------------------------------------------------------------------
# 6. Generate Video from Uploaded Image (Replicate — lightricks/ltx-2-distilled)
# ---------------------------------------------------------------------------
generate_video_from_image_function = FunctionSchema(
    name="generate_video_from_image",
    description=(
        "Generate a short video from a user-uploaded photo. "
        "Call this ONLY when the user has uploaded a photo and asks to create "
        "a video or animation from it. The uploaded photo will be used as the "
        "starting frame. Video generation takes 15-30 seconds. "
        "Do NOT call this if no photo has been uploaded."
    ),
    properties={
        "prompt": {
            "type": "string",
            "description": (
                "A description of the motion/animation to apply to the photo, "
                "in English. Translate from Hindi if needed. Describe how the "
                "scene should move or animate. For example: 'The person smiles "
                "and waves at the camera', 'Camera slowly zooms out revealing "
                "the landscape', 'Leaves start blowing in the wind'."
            ),
        },
    },
    required=["prompt"],
)

# ---------------------------------------------------------------------------
# Combined ToolsSchema
# ---------------------------------------------------------------------------
all_tools = ToolsSchema(
    standard_tools=[
        generate_image_function,
        generate_video_function,
        edit_image_function,
        generate_video_from_image_function,
        web_search_function,
        shopping_search_function,
    ],
)
