"""Shared pytest fixtures.

We set the required env vars before any ``notify_bridge_server`` module is
imported so ``Settings()`` passes its startup validation and opens the DB
in a writable temp directory instead of the production ``/data`` default.
"""

from __future__ import annotations

import os
import tempfile
from pathlib import Path

import pytest

# Provision a writable temp data dir BEFORE the server package is imported —
# Settings() materializes at import time, so env-var overrides have to land
# here (conftest.py) to be effective.
_TMP = Path(tempfile.mkdtemp(prefix="notify-bridge-tests-"))
os.environ["NOTIFY_BRIDGE_DATA_DIR"] = str(_TMP)

os.environ.setdefault(
    "NOTIFY_BRIDGE_SECRET_KEY",
    "pytest-secret-key-" + "x" * 40,
)
os.environ.setdefault("NOTIFY_BRIDGE_DEBUG", "false")
os.environ.setdefault("NOTIFY_BRIDGE_CORS_ALLOWED_ORIGINS", "http://localhost:8420")


@pytest.fixture(scope="session")
def tmp_data_dir() -> Path:
    """Expose the already-provisioned temp dir to tests that want the path."""
    return _TMP
