"""Pytest fixtures for stress and fuzz tests.

Note: Common fixtures like `crypto`, `meetings_dir`, `mockasr_engine`, and
`memory_servicer` are inherited from tests/conftest.py. Only stress-test-specific
fixtures are defined here.
"""

from __future__ import annotations

from typing import TYPE_CHECKING

import pytest

from support.db_utils import (
    cleanup_test_schema,
    create_test_engine,
    create_test_session_factory,
    get_or_create_container,
    initialize_test_schema,
    stop_container,
)

if TYPE_CHECKING:
    from collections.abc import AsyncGenerator

    from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker


@pytest.fixture
async def postgressession_factory() -> AsyncGenerator[async_sessionmaker[AsyncSession], None]:
    """Create PostgreSQL session factory using testcontainers.

    Uses a local container helper to avoid importing test modules.
    """
    _, database_url = get_or_create_container()

    engine = create_test_engine(database_url)

    async with engine.begin() as conn:
        await initialize_test_schema(conn)

    yield create_test_session_factory(engine)

    async with engine.begin() as conn:
        await cleanup_test_schema(conn)

    await engine.dispose()


def pytest_sessionfinish(session: pytest.Session, exitstatus: int) -> None:
    """Cleanup container after stress tests complete."""
    stop_container()
