"""Shared test fixtures for BadNote tests."""

import os
import shutil
import sys
import tempfile

import pytest
import pytest_asyncio
from httpx import ASGITransport, AsyncClient

# Ensure the server package is importable
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))

os.environ["BADNOTE_STORAGE_PATH"] = "/tmp/badnote_test_storage"
os.environ["BADNOTE_QUEUE_PATH"] = "/tmp/badnote_test_queue"
os.environ["BADNOTE_JWT_SECRET"] = "test-secret-key-for-testing-only"

from badnote_server.config import settings  # noqa: E402
from badnote_server.main import app  # noqa: E402
from badnote_server.database import get_db, close_db, init_db  # noqa: E402


@pytest_asyncio.fixture(autouse=True)
async def setup_db(tmp_path):
    """Fresh DB and clean queue for each test."""
    db_path = str(tmp_path / "test.db")
    settings.db_path = db_path
    # Clean the queue directory before each test
    queue_path = settings.queue_path
    if os.path.exists(queue_path):
        shutil.rmtree(queue_path)
    await init_db()
    yield
    await close_db()


@pytest_asyncio.fixture
async def client():
    """Async test client for the FastAPI app."""
    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as ac:
        yield ac
