"""
Simple REST API tests with ultra-minimal queries and longer timeouts.
Focus on basic functionality verification.
"""

import pytest
import requests

# Base URL for API
BASE_URL = "http://localhost:5000/api/v1"

# Extended timeout for research operations
RESEARCH_TIMEOUT = 120  # 2 minutes


def test_health_and_docs():
    """Test basic non-research endpoints."""
    print("🔍 Testing health and documentation endpoints...")

    # Health check
    response = requests.get(f"{BASE_URL}/health", timeout=5)
    assert response.status_code == 200
    data = response.json()
    assert data["status"] == "ok"
    print("✅ Health check passed")

    # API documentation
    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
    print("✅ API documentation passed")


def test_error_handling():
    """Test error handling for malformed requests."""
    print("🔍 Testing error handling...")

    # 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
    print("✅ Error handling for missing query passed")

    # Missing parameters for analyze_documents
    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 for missing collection_name passed")


@pytest.mark.requires_llm
def test_quick_summary_ultra_minimal():
    """Test quick summary with the most minimal possible query."""
    print("🔍 Testing quick summary with ultra-minimal query...")

    payload = {
        "query": "cat",  # Single word, very common
        "search_tool": "wikipedia",
        "iterations": 1,
        "temperature": 0.7,
    }

    try:
        print(f"Making request with {RESEARCH_TIMEOUT}s timeout...")
        response = requests.post(
            f"{BASE_URL}/quick_summary", json=payload, timeout=RESEARCH_TIMEOUT
        )

        if response.status_code == 200:
            data = response.json()

            # Basic structure validation
            required_fields = ["query", "summary", "findings"]
            for field in required_fields:
                assert field in data, f"Missing field: {field}"

            assert data["query"] == "cat"
            assert len(data["summary"]) > 0, "Summary should not be empty"
            assert isinstance(data["findings"], list), (
                "Findings should be a list"
            )

            print(
                f"✅ Quick summary passed - got {len(data['summary'])} chars of summary"
            )
            print(f"   Found {len(data['findings'])} findings")
        else:
            pytest.fail(
                f"Quick summary failed with status {response.status_code}"
            )
            print(f"   Response: {response.text[:200]}")
    except requests.exceptions.Timeout:
        pytest.fail(f"Quick summary timed out after {RESEARCH_TIMEOUT}s")
    except Exception as e:
        pytest.fail(f"Quick summary failed with error: {str(e)}")


@pytest.mark.requires_llm
def test_quick_summary_test_minimal():
    """Test the test endpoint with minimal query."""
    print("🔍 Testing quick summary test endpoint...")

    payload = {"query": "dog"}  # Another simple, common word

    try:
        print(f"Making request with {RESEARCH_TIMEOUT}s timeout...")
        response = requests.post(
            f"{BASE_URL}/quick_summary_test",
            json=payload,
            timeout=RESEARCH_TIMEOUT,
        )

        if response.status_code == 200:
            data = response.json()

            # Basic structure validation
            assert "query" in data
            assert "summary" in data
            assert data["query"] == "dog"
            assert len(data["summary"]) > 0

            print(
                f"✅ Quick summary test passed - got {len(data['summary'])} chars of summary"
            )
        else:
            pytest.fail(
                f"Quick summary test failed with status {response.status_code}"
            )
            print(f"   Response: {response.text[:200]}")
    except requests.exceptions.Timeout:
        pytest.fail(f"Quick summary test timed out after {RESEARCH_TIMEOUT}s")
    except Exception as e:
        pytest.fail(f"Quick summary test failed with error: {str(e)}")
