"""Notification module configuration and initialization."""

from __future__ import annotations

import asyncpg
from pydantic import BaseModel

_config: NotificationConfig | None = None
_pool: asyncpg.Pool | None = None


class NotificationConfig(BaseModel):
    """Configuration for the notification module."""

    database_url: str
    max_per_page: int = 50
    retention_days: int = 90


async def init_notifications(config: NotificationConfig) -> None:
    """Initialize the notification module with the given configuration.

    Creates an asyncpg connection pool and stores the config globally.
    Must be called before any store or service operations.
    """
    global _config, _pool
    _config = config
    _pool = await asyncpg.create_pool(dsn=config.database_url, min_size=2, max_size=10)


def get_config() -> NotificationConfig:
    """Return the current module configuration.

    Raises:
        RuntimeError: If init_notifications() has not been called.
    """
    if _config is None:
        raise RuntimeError(
            "Notification module not initialized. Call init_notifications() first."
        )
    return _config


def get_pool() -> asyncpg.Pool:
    """Return the current connection pool.

    Raises:
        RuntimeError: If init_notifications() has not been called.
    """
    if _pool is None:
        raise RuntimeError(
            "Notification module not initialized. Call init_notifications() first."
        )
    return _pool
