"""Tests for MCP server functionality.

Generated by GRIMLOCK MCP Factory
"""

import pytest
from videogen_mcp.server import server, list_tools, call_tool


class TestServer:
    """Test cases for MCP server."""

    @pytest.mark.asyncio
    async def test_server_name(self):
        """Server should have correct name."""
        assert server.name == "videogen-mcp"

    @pytest.mark.asyncio
    async def test_list_tools_returns_all_tools(self):
        """Should return all 11 tools."""
        tools = await list_tools()

        assert len(tools) == 11

        tool_names = [t.name for t in tools]
        assert "generate_video" in tool_names
        assert "get_video_status" in tool_names
        assert "download_video" in tool_names
        assert "list_avatars" in tool_names
        assert "list_voices" in tool_names
        assert "create_avatar_video" in tool_names
        assert "list_templates" in tool_names
        assert "get_template_details" in tool_names
        assert "generate_from_template" in tool_names
        assert "generate_creative_video" in tool_names
        assert "generate_video_from_image" in tool_names

    @pytest.mark.asyncio
    async def test_list_tools_have_descriptions(self):
        """All tools should have descriptions."""
        tools = await list_tools()

        for tool in tools:
            assert tool.description
            assert len(tool.description) > 20

    @pytest.mark.asyncio
    async def test_list_tools_have_schemas(self):
        """All tools should have input schemas."""
        tools = await list_tools()

        for tool in tools:
            assert tool.inputSchema
            assert "type" in tool.inputSchema

    @pytest.mark.asyncio
    async def test_call_unknown_tool_raises_error(self):
        """Should raise ValueError for unknown tool."""
        with pytest.raises(ValueError, match="Unknown tool"):
            await call_tool("unknown_tool", {})

    @pytest.mark.asyncio
    async def test_tool_descriptions_are_concise(self):
        """Tool descriptions should be concise but informative."""
        tools = await list_tools()

        for tool in tools:
            # Should be under 500 characters for readability
            assert len(tool.description) < 500, f"{tool.name} description too long"


class TestToolSchemas:
    """Test cases for tool input schemas."""

    @pytest.mark.asyncio
    async def test_generate_video_schema(self):
        """generate_video should have correct schema."""
        tools = await list_tools()
        tool = next(t for t in tools if t.name == "generate_video")

        schema = tool.inputSchema
        assert schema["type"] == "object"
        assert "prompt" in schema.get("properties", {})
        assert "service_hint" in schema.get("properties", {})
        assert "aspect_ratio" in schema.get("properties", {})
        assert "duration" in schema.get("properties", {})

    @pytest.mark.asyncio
    async def test_get_video_status_schema(self):
        """get_video_status should have correct schema."""
        tools = await list_tools()
        tool = next(t for t in tools if t.name == "get_video_status")

        schema = tool.inputSchema
        assert "job_id" in schema.get("properties", {})
        assert "service" in schema.get("properties", {})
        assert "job_id" in schema.get("required", [])
        assert "service" in schema.get("required", [])

    @pytest.mark.asyncio
    async def test_create_avatar_video_schema(self):
        """create_avatar_video should have correct schema."""
        tools = await list_tools()
        tool = next(t for t in tools if t.name == "create_avatar_video")

        schema = tool.inputSchema
        assert "script" in schema.get("properties", {})
        assert "avatar_id" in schema.get("properties", {})
        assert "voice_id" in schema.get("properties", {})
        assert "background" in schema.get("properties", {})

    @pytest.mark.asyncio
    async def test_generate_creative_video_schema(self):
        """generate_creative_video should have correct schema."""
        tools = await list_tools()
        tool = next(t for t in tools if t.name == "generate_creative_video")

        schema = tool.inputSchema
        assert "prompt" in schema.get("properties", {})
        assert "negative_prompt" in schema.get("properties", {})
        assert "model" in schema.get("properties", {})
        assert "seed" in schema.get("properties", {})
        assert "sample_count" in schema.get("properties", {})

    @pytest.mark.asyncio
    async def test_generate_video_from_image_schema(self):
        """generate_video_from_image should have correct schema."""
        tools = await list_tools()
        tool = next(t for t in tools if t.name == "generate_video_from_image")

        schema = tool.inputSchema
        assert "image_url" in schema.get("properties", {})
        assert "prompt" in schema.get("properties", {})
        assert "image_url" in schema.get("required", [])
        assert "prompt" in schema.get("required", [])
