"""Pytest fixtures for gRPC tests.

Provides shared fixtures for gRPC servicer testing including mock contexts,
repositories, and servicer host implementations.

Note: Common fixtures like `mock_grpc_context` are inherited from tests/conftest.py.
Only gRPC-specific fixtures are defined here.
"""

from __future__ import annotations

from unittest.mock import AsyncMock
from uuid import uuid4

import pytest

from noteflow.domain.webhooks import (
    DeliveryResult,
    WebhookConfig,
    WebhookDelivery,
    WebhookEventType,
)


@pytest.fixture
def mock_meetings_repo() -> AsyncMock:
    """Create mock meetings repository.

    Returns:
        AsyncMock with get method returning None.
    """
    repo = AsyncMock()
    repo.get = AsyncMock(return_value=None)
    return repo


@pytest.fixture
def mock_preferences_repo() -> AsyncMock:
    """Create mock preferences repository with common methods.

    Returns:
        AsyncMock with get_all_with_metadata, set_bulk, and delete methods.
    """
    repo = AsyncMock()
    repo.get_all_with_metadata = AsyncMock(return_value=[])
    repo.set_bulk = AsyncMock()
    repo.delete = AsyncMock()
    return repo


@pytest.fixture
def mock_webhook_repo() -> AsyncMock:
    """Create mock webhook repository with common methods.

    Returns:
        AsyncMock with all webhook repository methods.
    """
    repo = AsyncMock()
    repo.create = AsyncMock()
    repo.get_by_id = AsyncMock(return_value=None)
    repo.get_all = AsyncMock(return_value=[])
    repo.get_all_enabled = AsyncMock(return_value=[])
    repo.update = AsyncMock()
    repo.delete = AsyncMock(return_value=False)
    repo.add_delivery = AsyncMock()
    repo.get_deliveries = AsyncMock(return_value=[])
    return repo


@pytest.fixture
def sample_webhook_config() -> WebhookConfig:
    """Create sample webhook config with all optional fields for testing."""
    return WebhookConfig.create(
        workspace_id=uuid4(),
        url="https://example.com/webhook",
        events=[WebhookEventType.MEETING_COMPLETED],
        name="Test Webhook",
        secret="test-secret",
        timeout_ms=15000,
        max_retries=5,
    )


@pytest.fixture
def sample_webhook_delivery(sample_webhook_config: WebhookConfig) -> WebhookDelivery:
    """Create sample webhook delivery for testing."""
    result = DeliveryResult(status_code=200, response_body='{"ok": true}', duration_ms=150)
    return WebhookDelivery.create(
        webhook_id=sample_webhook_config.id,
        event_type=WebhookEventType.MEETING_COMPLETED,
        payload={"event": "meeting.completed", "meeting_id": str(uuid4())},
        result=result,
    )
