"""Shared fixtures for email tests — encryption key + DB session."""

from __future__ import annotations

import secrets
from collections.abc import AsyncGenerator

import pytest
import pytest_asyncio
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import sessionmaker
from sqlmodel import SQLModel

from awaithumans.server.core import encryption
from awaithumans.server.core.config import settings

# Register models so create_all picks up the tables.
from awaithumans.server.db.models import (  # noqa: F401
    AuditEntry,
    EmailSenderIdentity,
    SlackInstallation,
    Task,
)


@pytest.fixture(autouse=True)
def _encryption_key() -> None:
    """Per-test encryption key so EncryptedString columns work.

    EmailSenderIdentity.transport_config is encrypted; without a key,
    any upsert raises EncryptionNotConfiguredError.
    """
    original = settings.PAYLOAD_KEY
    settings.PAYLOAD_KEY = secrets.token_urlsafe(32)
    encryption.reset_key_cache()
    yield
    settings.PAYLOAD_KEY = original
    encryption.reset_key_cache()


@pytest_asyncio.fixture
async def session() -> AsyncGenerator[AsyncSession, None]:
    engine = create_async_engine("sqlite+aiosqlite:///:memory:")
    async with engine.begin() as conn:
        await conn.run_sync(SQLModel.metadata.create_all)
    factory = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
    async with factory() as s:
        yield s
    await engine.dispose()
