"""
Basic API tests - only test endpoints that should respond quickly.
Focus on verifying the API is working without doing actual research.
"""

import requests
import pytest

BASE_URL = "http://localhost:5000/api/v1"


def test_health_check():
    """Test health check endpoint."""
    response = requests.get(f"{BASE_URL}/health", timeout=5)
    assert response.status_code == 200
    data = response.json()
    assert data["status"] == "ok"
    assert "timestamp" in data
    print("✅ Health check: API is responding")


def test_api_documentation():
    """Test API documentation endpoint."""
    response = requests.get(f"{BASE_URL}/", timeout=5)
    assert response.status_code == 200
    data = response.json()
    assert data["api_version"] == "v1"
    assert "endpoints" in data
    assert len(data["endpoints"]) >= 3
    print("✅ API documentation: All endpoints documented")


def test_error_handling():
    """Test error handling for malformed requests."""
    # Test missing query parameter
    response = requests.post(f"{BASE_URL}/quick_summary", json={}, timeout=5)
    assert response.status_code == 400
    data = response.json()
    assert "error" in data
    assert "required" in data["error"].lower()
    print("✅ Error handling: Proper validation for missing query")

    # Test missing collection_name
    response = requests.post(
        f"{BASE_URL}/analyze_documents", json={"query": "test"}, timeout=5
    )
    assert response.status_code == 400
    data = response.json()
    assert "error" in data
    print("✅ Error handling: Proper validation for missing collection_name")


def test_api_structure():
    """Test that API accepts properly formatted requests (without waiting for results)."""
    # We'll send a request but not wait for the full response
    # Just verify the API accepts the request format

    payload = {
        "query": "test",
        "search_tool": "wikipedia",
        "iterations": 1,
        "temperature": 0.7,
    }

    try:
        # Use a very short timeout to just test if the API accepts the request
        requests.post(f"{BASE_URL}/quick_summary", json=payload, timeout=2)
        # If we get here, the API at least accepted the request format
        print(
            "✅ API structure: Request format accepted by quick_summary endpoint"
        )
        return
    except requests.exceptions.Timeout:
        # Timeout is expected - the API accepted the request but research takes time
        print(
            "✅ API structure: Request format accepted (timed out during processing, which is expected)"
        )
        return
    except requests.exceptions.ConnectionError as e:
        print(f"❌ API structure: Connection error - {e}")
        pytest.fail("API connection error")
        return
    except Exception as e:
        if "400" in str(e) or "Bad Request" in str(e):
            print(f"❌ API structure: Request format rejected - {e}")
            pytest.fail("API structure: Request format rejected")
            return
        else:
            # Other errors during processing are acceptable
            print(
                "✅ API structure: Request format accepted (processing error, which is normal)"
            )
            return
