"""Tests for the SNS provider (P1-17 through P1-21)."""

from __future__ import annotations

from unittest.mock import AsyncMock

import httpx
import pytest

from lws.interfaces import ICompute, InvocationResult
from lws.interfaces.queue import IQueue
from lws.providers.sns.provider import (
    SnsProvider,
    TopicConfig,
)

# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------


def _make_compute_mock(payload: dict | None = None, error: str | None = None) -> ICompute:
    """Return a mock ICompute whose ``invoke`` resolves to the given result."""
    mock = AsyncMock(spec=ICompute)
    mock.invoke.return_value = InvocationResult(
        payload=payload,
        error=error,
        duration_ms=1.0,
        request_id="test-request-id",
    )
    return mock


def _make_queue_mock() -> IQueue:
    """Return a mock IQueue."""
    mock = AsyncMock(spec=IQueue)
    mock.send_message.return_value = "mock-sqs-message-id"
    return mock


def _topic_configs() -> list[TopicConfig]:
    return [
        TopicConfig(
            topic_name="my-topic",
            topic_arn="arn:aws:sns:us-east-1:000000000000:my-topic",
        ),
        TopicConfig(
            topic_name="other-topic",
            topic_arn="arn:aws:sns:us-east-1:000000000000:other-topic",
        ),
    ]


async def _started_provider(
    topics: list[TopicConfig] | None = None,
) -> SnsProvider:
    provider = SnsProvider(topics=topics or _topic_configs())
    await provider.start()
    return provider


def _client(app) -> httpx.AsyncClient:
    transport = httpx.ASGITransport(app=app)  # type: ignore[arg-type]
    return httpx.AsyncClient(transport=transport, base_url="http://testserver")


# ===========================================================================
# LocalTopic tests
# ===========================================================================


# ===========================================================================
# SnsProvider lifecycle tests
# ===========================================================================


# ===========================================================================
# SnsProvider publish and subscribe
# ===========================================================================


# ===========================================================================
# Lambda subscription dispatch
# ===========================================================================


# ===========================================================================
# SQS subscription dispatch
# ===========================================================================


# ===========================================================================
# Fan-out to multiple subscribers
# ===========================================================================


# ===========================================================================
# Filter policy matching tests
# ===========================================================================


# ===========================================================================
# SNS event format construction
# ===========================================================================


# ===========================================================================
# SNS routes tests (wire protocol)
# ===========================================================================


class TestSnsProviderPublishSubscribe:
    """Test SnsProvider publish and subscribe operations."""

    @pytest.mark.asyncio
    async def test_subscribe_returns_arn(self) -> None:
        # Arrange
        provider = await _started_provider()
        topic_name = "my-topic"

        # Act
        actual_arn = await provider.subscribe(
            topic_name=topic_name,
            protocol="lambda",
            endpoint="my-func",
        )

        # Assert
        assert topic_name in actual_arn

    @pytest.mark.asyncio
    async def test_publish_returns_message_id(self) -> None:
        # Arrange
        provider = await _started_provider()
        expected_uuid_parts = 5

        # Act
        actual_message_id = await provider.publish(
            topic_name="my-topic",
            message="hello world",
        )

        # Assert
        assert actual_message_id
        actual_parts = actual_message_id.split("-")
        assert len(actual_parts) == expected_uuid_parts

    @pytest.mark.asyncio
    async def test_publish_nonexistent_topic_raises(self) -> None:
        provider = await _started_provider()
        with pytest.raises(KeyError, match="Topic not found"):
            await provider.publish(topic_name="no-such-topic", message="oops")
