import sys
from pathlib import Path
import pytest
from fastapi.testclient import TestClient

# Ensure backend folder is in python path
sys.path.insert(0, str(Path(__file__).parent.parent))

from app.config import settings
from app.database import db_helper, InMemoryDatabase
from app.main import app

# Force in-memory database mode for pytest runs
settings.database_name = "chatbot_test_db"
db_helper.use_fallback = True
db_helper.in_memory_db = InMemoryDatabase(seed_default=False)

@pytest.fixture(autouse=True)
def reset_in_memory_db():
    # Fresh in-memory state per test
    db_helper.in_memory_db = InMemoryDatabase(seed_default=False)

@pytest.fixture
def client():
    with TestClient(app) as c:
        yield c
