"""Shared pytest fixtures and configuration."""

import asyncio
import os
from typing import Generator

import pytest

# Ensure the project root is importable
import sys

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))


@pytest.fixture(autouse=True)
def _reset_settings(monkeypatch: pytest.MonkeyPatch) -> Generator[None, None, None]:
    """Reset navi.config.settings before every test so mutations don't leak."""
    from navi.config import Settings

    # Clear env vars that would leak from the host environment / .env
    for key in ("NAVI_PERSONA", "NAVI_PERSONA_FILE"):
        monkeypatch.delenv(key, raising=False)

    # Re-create settings from defaults (no .env file during tests)
    fresh = Settings(_env_file=None, navi_persona_file="")
    import navi.config as _config_mod

    _config_mod.settings = fresh
    yield
    _config_mod.settings = fresh


@pytest.fixture
def event_loop():
    """Provide a consistent event loop for async tests."""
    loop = asyncio.get_event_loop_policy().new_event_loop()
    yield loop
    loop.close()
