"""
Test for Random Template Middleware on_get_prompt functionality.

This test follows the standardized client testing framework and validates that
the enhanced template middleware can automatically render random templates
from the prompts folder when specific prompt names are requested.
"""

import pytest


@pytest.mark.service("template")
class TestRandomTemplateMiddleware:
    """Tests for the random template middleware on_get_prompt functionality."""

    @pytest.mark.asyncio
    async def test_prompts_available(self, client):
        """Test that prompts are available from the server."""
        prompts = await client.list_prompts()
        assert prompts is not None, "Should have prompts available"

        prompt_names = [prompt.name for prompt in prompts]
        print(f"🔍 Available prompts: {prompt_names}")

        # Should have at least the prompts we set up
        expected_prompt_categories = [
            "gchat",
            "gmail",
            "gsheets",
            "chat_app",
            "structured",
        ]
        available_categories = [
            name
            for name in prompt_names
            if any(cat in name for cat in expected_prompt_categories)
        ]

        assert len(available_categories) > 0, (
            f"Should have prompts from expected categories. Found: {prompt_names}"
        )

    @pytest.mark.asyncio
    async def test_random_template_request(self, client):
        """Test requesting a random template via prompt system."""
        try:
            # Request a random template using the middleware
            result = await client.get_prompt("random_template")

            # Should get a result (either from middleware or normal prompt handling)
            assert result is not None, (
                "Should get a result from random template request"
            )

            # Check if we got messages
            if result.messages:
                print(f"✅ Random template returned {len(result.messages)} messages")
                for i, message in enumerate(result.messages):
                    print(f"   Message {i + 1}: {message.role}")
                    content = (
                        message.content.text
                        if hasattr(message.content, "text")
                        else str(message.content)
                    )
                    print(f"   Content length: {len(content)} characters")
                    print(f"   FULL CONTENT:\n{content}\n" + "=" * 80)

                # Basic validation
                assert len(result.messages) > 0, "Should have at least one message"

                # Check that content looks like a rendered template
                first_message = result.messages[0]
                content = (
                    first_message.content.text
                    if hasattr(first_message.content, "text")
                    else str(first_message.content)
                )

                # Should contain some indicators it's a rendered template
                template_indicators = [
                    "Request ID:",
                    "Generated:",
                    "FastMCP",
                    "Gmail",
                    "Chat",
                    "Sheets",
                    "template",
                    "demo",
                ]

                has_indicators = any(
                    indicator in content for indicator in template_indicators
                )
                assert has_indicators, (
                    f"Content should contain template indicators. Content: {content[:200]}"
                )

                print("✅ Random template middleware working correctly!")
                return True
            else:
                print("⚠️ No messages returned, but request succeeded")
                return False

        except Exception as e:
            print(f"⚠️ Random template request failed: {e}")
            # This is expected if the prompt doesn't exist - that's okay for testing
            return False

    @pytest.mark.asyncio
    async def test_specific_template_categories(self, client):
        """Test getting prompts from specific categories to verify template system."""
        # First get the actual available prompts
        prompts = await client.list_prompts()
        available_prompt_names = [prompt.name for prompt in prompts]
        print(f"📋 All available prompts: {available_prompt_names}")

        # Test a few key prompts that should exist
        categories_to_test = [
            "quick_email_demo",
            "google_chat_complex_card_advanced",
            "structured_tool_showcase",
        ]

        successful_tests = 0

        for prompt_name in categories_to_test:
            try:
                result = await client.get_prompt(prompt_name)

                if result and result.messages:
                    successful_tests += 1
                    print(f"✅ {prompt_name}: {len(result.messages)} messages")

                    # Quick validation
                    first_message = result.messages[0]
                    content = (
                        first_message.content.text
                        if hasattr(first_message.content, "text")
                        else str(first_message.content)
                    )

                    # Should contain service-specific content
                    service_indicators = {
                        "email": ["Gmail", "email", "@", "message"],
                        "chat": ["Chat", "card", "space", "webhook"],
                        "sheets": ["Sheets", "spreadsheet", "data", "chart"],
                    }

                    for service, indicators in service_indicators.items():
                        if service in prompt_name:
                            has_service_content = any(
                                indicator in content for indicator in indicators
                            )
                            if has_service_content:
                                print(f"   Contains {service} content indicators")
                            break
                else:
                    print(f"⚠️ {prompt_name}: No messages returned")

            except Exception as e:
                print(f"⚠️ {prompt_name}: Failed with {e}")

        print(
            f"📊 Successfully tested {successful_tests}/{len(categories_to_test)} prompt categories"
        )

        # At least one should work to verify the system is functional
        assert successful_tests > 0, "At least one prompt category should work"

    @pytest.mark.asyncio
    async def test_template_with_jinja2_features(self, client):
        """Test that templates can use Jinja2 features with resource resolution."""
        # This tests the template file rendering specifically
        try:
            # Try to get a template that we know uses Jinja2 features
            result = await client.get_prompt("quick_email_demo")

            if result and result.messages:
                content = (
                    result.messages[0].content.text
                    if hasattr(result.messages[0].content, "text")
                    else str(result.messages[0].content)
                )

                # Should contain Jinja2 processed content
                jinja2_indicators = [
                    "Request ID:",  # Should have been processed
                    "Generated:",  # Should have timestamp
                    "{{user://",  # Should contain resource URIs (or resolved values)
                    "✅",  # Should have emoji/formatting
                    "Status:",  # Should have structured content
                ]

                indicators_found = [
                    indicator for indicator in jinja2_indicators if indicator in content
                ]
                print(f"🎭 Jinja2 indicators found: {indicators_found}")

                # Should have at least some processed template content
                assert len(indicators_found) >= 2, (
                    f"Should contain Jinja2 processed content. Found: {indicators_found}"
                )

                print("✅ Template Jinja2 processing working!")
                return True
            else:
                print("⚠️ No content returned for Jinja2 template test")
                return False

        except Exception as e:
            print(f"⚠️ Jinja2 template test failed: {e}")
            return False


if __name__ == "__main__":
    print(
        "🧪 Run with: uv run pytest tests/client/test_random_template_middleware.py -v"
    )
