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


@pytest.fixture
def client():
    """FastAPI test client."""
    return TestClient(app)


@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
async def auth_token(client, test_user_data):
    """Get authentication token for test user."""
    # Sign up test user
    signup_response = client.post(
        "/api/v1/auth/signup",
        json=test_user_data
    )
    
    if signup_response.status_code == 200:
        return signup_response.json().get("token")
    
    # If user exists, sign in
    signin_response = client.post(
        "/api/v1/auth/signin",
        json={
            "email": test_user_data["email"],
            "password": test_user_data["password"],
        }
    )
    
    if signin_response.status_code == 200:
        return signin_response.json().get("token")
    
    return None


@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.
    # This will be implemented based on test data patterns
    pass

