"""
Pytest configuration and fixtures for backend tests.
"""
import os
import pytest
from fastapi.testclient import TestClient
from server import app


# Override MEMO_DB_PATH for tests before importing sqlite_client
os.environ.setdefault("MEMO_DB_PATH", ":memory:")


@pytest.fixture
def client():
    """FastAPI test client."""
    with TestClient(app, raise_server_exceptions=True) as client:
        yield client


@pytest.fixture
async def async_client():
    """Async HTTP client for testing."""
    from httpx import AsyncClient
    async with AsyncClient(app=app, base_url="http://test") as ac:
        yield ac


@pytest.fixture
def test_user_data():
    """Test user data for creating users."""
    return {
        "email": "test@example.com",
        "password": "testpassword123",
        "username": "testuser",
    }


@pytest.fixture
def test_memo_data():
    """Test memo data for creating memos."""
    return {
        "content": "This is a test memo",
        "visibility": "PUBLIC",
        "tags": ["test", "memo"],
    }


@pytest.fixture
def auth_token():
    """Generate a valid JWT token for testing."""
    from common.jwt_auth import create_access_token
    # Create token with user ID "1" to match test URLs
    token = create_access_token(data={"sub": "1"})
    return token


@pytest.fixture
def authenticated_headers(auth_token):
    """Headers with authentication token."""
    if auth_token:
        return {"Authorization": f"Bearer {auth_token}"}
    return {}


@pytest.fixture
async def cleanup_test_data():
    """Cleanup test data after tests."""
    yield
    # Clean up test memos, comments, etc.
    pass


