# backend/tests/conftest.py
import pytest
from fastapi.testclient import TestClient
from sqlmodel import Session, SQLModel, create_engine
from sqlmodel.pool import StaticPool

from app import seed as seed_mod
from app.db import get_session
from app.llm.heuristic import HeuristicProvider
from app.llm.provider import get_provider
from app.main import app


@pytest.fixture()
def session():
    engine = create_engine(
        "sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool
    )
    SQLModel.metadata.create_all(engine)
    with Session(engine) as s:
        yield s, engine


@pytest.fixture()
def client(session):
    s, engine = session
    # 테스트 세션을 주입해 시드(정본 시그니처: run_seed(session=..., reset=True)).
    with Session(engine) as seed_s:
        seed_mod.run_seed(session=seed_s, reset=True)

    def _get_session():
        with Session(engine) as ss:
            yield ss

    app.dependency_overrides[get_session] = _get_session
    # LLM은 기본적으로 heuristic 강제(테스트 결정성).
    app.dependency_overrides[get_provider] = lambda: HeuristicProvider()
    yield TestClient(app)
    app.dependency_overrides.clear()
