"""Example: Client using prompts-as-tools.

This client demonstrates calling the list_prompts and get_prompt tools
generated by the PromptsAsTools transform.

Run with:
    uv run python examples/prompts_as_tools/client.py
"""

import asyncio
import json

from fastmcp.client import Client


async def main():
    # Connect to the server
    async with Client("examples/prompts_as_tools/server.py") as client:
        # List all available tools
        print("=== Available Tools ===")
        tools = await client.list_tools()
        for tool in tools:
            print(f"  - {tool.name}: {tool.description}")
        print()

        # Use list_prompts tool to see what's available
        print("=== Listing Prompts ===")
        result = await client.call_tool("list_prompts", {})
        prompts = json.loads(result.data)

        for prompt in prompts:
            print(f"  {prompt['name']}")
            print(f"    Description: {prompt.get('description', 'N/A')}")
            if prompt["arguments"]:
                print("    Arguments:")
                for arg in prompt["arguments"]:
                    required = "required" if arg["required"] else "optional"
                    print(
                        f"      - {arg['name']} ({required}): {arg.get('description', 'N/A')}"
                    )
            print()

        # Get a prompt without optional arguments
        print("=== Getting Simple Prompt ===")
        result = await client.call_tool(
            "get_prompt",
            {"name": "explain_concept", "arguments": {"concept": "recursion"}},
        )
        response = json.loads(result.data)
        print("Messages:")
        for msg in response["messages"]:
            print(f"  Role: {msg['role']}")
            print(f"  Content: {msg['content'][:100]}...")
        print()

        # Get a prompt with optional arguments
        print("=== Getting Prompt with Optional Arguments ===")
        result = await client.call_tool(
            "get_prompt",
            {
                "name": "analyze_code",
                "arguments": {
                    "code": "def factorial(n):\n    return n * factorial(n-1)",
                    "language": "python",
                    "focus": "bugs",
                },
            },
        )
        response = json.loads(result.data)
        print("Messages:")
        for msg in response["messages"]:
            print(f"  Role: {msg['role']}")
            print(f"  Content: {msg['content'][:150]}...")


if __name__ == "__main__":
    asyncio.run(main())
