"""Integration tests for List Queues Operation (Story 3.1 Task 10)."""

import pytest

from rabbitmq_mcp_connection.http_client import HTTPClientManager
from rabbitmq_mcp_connection.operations.topology import ListQueuesOperation
from rabbitmq_mcp_connection.schemas.topology import QueueDetails

pytestmark = pytest.mark.integration


@pytest.fixture
async def http_client():
    """Create HTTPClientManager instance for testing."""
    client = HTTPClientManager(
        host="localhost",
        port=15672,
        username="guest",
        password="guest",
        use_tls=False,
        timeout=30,
    )
    yield client
    await client.close()


@pytest.fixture
def list_operation(http_client):
    """Create ListQueuesOperation instance."""
    return ListQueuesOperation(http_client)


class TestListQueuesIntegration:
    """Integration tests with actual RabbitMQ instance (AC #1-8).

    Note: Requires RabbitMQ running on localhost:15672
    """

    @pytest.mark.asyncio
    @pytest.mark.skip(reason="Requires running RabbitMQ instance")
    async def test_list_queues_default_vhost(self, list_operation):
        """Test listing queues in default vhost (AC #1, #2, #3)."""
        # Execute
        queues = await list_operation.execute(vhost="/")

        # Verify
        assert isinstance(queues, list)
        assert all(isinstance(q, QueueDetails) for q in queues)

        # Each queue should have required fields
        for queue in queues:
            assert queue.name
            assert queue.vhost == "/"
            assert isinstance(queue.durable, bool)
            assert isinstance(queue.auto_delete, bool)
            assert isinstance(queue.messages, int)
            assert isinstance(queue.consumers, int)

    @pytest.mark.asyncio
    @pytest.mark.skip(reason="Requires running RabbitMQ instance")
    async def test_list_queues_empty_vhost(self, list_operation):
        """Test listing queues in empty vhost returns empty list (AC #5)."""
        # Note: Would need to create empty test vhost first
        queues = await list_operation.execute(vhost="/test-empty")

        assert queues == []

    @pytest.mark.asyncio
    @pytest.mark.skip(reason="Requires running RabbitMQ instance")
    async def test_list_queues_nonexistent_vhost(self, list_operation):
        """Test listing queues in non-existent vhost returns error (AC #6)."""
        with pytest.raises(
            ValueError, match="Virtual host '/nonexistent' does not exist"
        ):
            await list_operation.execute(vhost="/nonexistent")

    @pytest.mark.asyncio
    @pytest.mark.skip(reason="Requires running RabbitMQ instance")
    async def test_list_queues_with_filtering(self, list_operation):
        """Test filtering queues by name pattern (AC #7)."""
        # Assumes some queues exist with "test-" prefix
        queues = await list_operation.execute(vhost="/", name_pattern="test-.*")

        assert all("test-" in q.name for q in queues)

    @pytest.mark.asyncio
    @pytest.mark.skip(reason="Requires running RabbitMQ instance")
    async def test_list_queues_with_sorting(self, list_operation):
        """Test sorting queues by messages (AC #8)."""
        queues = await list_operation.execute(
            vhost="/", sort_by="messages", reverse=True
        )

        # Verify descending order
        for i in range(len(queues) - 1):
            assert queues[i].messages >= queues[i + 1].messages

    @pytest.mark.asyncio
    @pytest.mark.skip(reason="Requires running RabbitMQ instance")
    async def test_list_queues_performance(self, list_operation):
        """Test performance target <200ms for up to 1000 queues (AC #4)."""
        import time

        start = time.time()
        queues = await list_operation.execute(vhost="/")
        duration_ms = (time.time() - start) * 1000

        # Performance target
        if len(queues) <= 1000:
            assert duration_ms < 200, f"Expected <200ms, got {duration_ms:.2f}ms"


# Note: To run these tests, start RabbitMQ:
# docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3-management
# Then remove @pytest.mark.skip decorators
