"""Shared fixtures for notification module tests."""

from __future__ import annotations

from unittest.mock import AsyncMock, MagicMock
from uuid import uuid4

import pytest

import notifications.config as cfg
from notifications.config import NotificationConfig


@pytest.fixture(autouse=True)
def _reset_config():
    """Reset the global config and pool before each test."""
    cfg._config = None
    cfg._pool = None
    yield
    cfg._config = None
    cfg._pool = None


@pytest.fixture()
def notification_config() -> NotificationConfig:
    """Return a standard test configuration."""
    return NotificationConfig(
        database_url="postgresql://test:test@localhost:5432/test_notifications",
        max_per_page=20,
        retention_days=30,
    )


@pytest.fixture()
def mock_pool() -> MagicMock:
    """Return a mock asyncpg pool with an acquire context manager."""
    pool = MagicMock()
    conn = AsyncMock()

    # Make pool.acquire() return an async context manager yielding conn
    acquire_cm = AsyncMock()
    acquire_cm.__aenter__ = AsyncMock(return_value=conn)
    acquire_cm.__aexit__ = AsyncMock(return_value=False)
    pool.acquire.return_value = acquire_cm

    return pool


@pytest.fixture()
def mock_conn(mock_pool: MagicMock) -> AsyncMock:
    """Return the mock connection from the mock pool."""
    return mock_pool.acquire.return_value.__aenter__.return_value


@pytest.fixture()
def initialized_config(notification_config: NotificationConfig, mock_pool: MagicMock) -> NotificationConfig:
    """Return a config that has been initialized with a mock pool."""
    cfg._config = notification_config
    cfg._pool = mock_pool
    return notification_config
