import sys
from pathlib import Path

import pytest

sys.path.insert(0, str(Path(__file__).resolve().parent.parent))

import database  # noqa: E402


@pytest.fixture
async def testdb(tmp_path, monkeypatch):
    """Fresh sqlite file per test; resets the module-global connection."""
    monkeypatch.setattr(database, "DB_PATH", tmp_path / "test.db")
    database._db = None
    await database.init_db()
    yield database
    await database.close_db()


@pytest.fixture
async def fake_welt(testdb, tmp_path, monkeypatch):
    """E2E ohne LLM: run_agent überall durch die Fake-Welt ersetzt, Tempo-Bremsen raus.
    Alle echten Schichten (_race, Quorum, Panels, Producer, QA-Gate) laufen mit."""
    import qa
    from fake_agents import Welt, aktivieren

    welt = Welt()
    aktivieren(welt, setattr_fn=monkeypatch.setattr)
    monkeypatch.setattr(qa, "QA_DIR", tmp_path / "qa")  # Reports nie in echte Nutzdaten
    return welt
