"""
Test suite for REST API endpoints using minimal queries.
Tests programmatic access functionality with fast, simple requests.
"""

import pytest
import requests

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

# Test timeout in seconds
TEST_TIMEOUT = 30


class TestRestAPI:
    """Test REST API endpoints with minimal queries."""

    def test_health_check(self):
        """Test the 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 passed")

    def test_api_documentation(self):
        """Test the 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  # Should have at least 3 endpoints
        print("✅ API documentation passed")

    @pytest.mark.requires_llm
    def test_quick_summary_minimal(self):
        """Test quick summary with minimal query."""
        payload = {
            "query": "Python",
            "search_tool": "wikipedia",
            "iterations": 1,
            "temperature": 0.7,
        }

        response = requests.post(
            f"{BASE_URL}/quick_summary", json=payload, timeout=TEST_TIMEOUT
        )

        assert response.status_code == 200
        data = response.json()

        # Verify response structure
        assert "query" in data
        assert "summary" in data
        assert "findings" in data
        assert data["query"] == "Python"
        assert len(data["summary"]) > 10  # Should have actual content
        assert isinstance(data["findings"], list)

        print(
            f"✅ Quick summary passed - got {len(data['summary'])} chars of summary"
        )

    @pytest.mark.requires_llm
    def test_quick_summary_test_endpoint(self):
        """Test the quick summary test endpoint with minimal query."""
        payload = {"query": "AI"}

        response = requests.post(
            f"{BASE_URL}/quick_summary_test", json=payload, timeout=TEST_TIMEOUT
        )

        assert response.status_code == 200
        data = response.json()

        # Verify response structure
        assert "query" in data
        assert "summary" in data
        assert "findings" in data
        assert data["query"] == "AI"
        assert len(data["summary"]) > 10  # Should have actual content

        print(
            f"✅ Quick summary test passed - got {len(data['summary'])} chars of summary"
        )

    def test_error_handling_missing_query(self):
        """Test error handling when query is missing."""
        payload = {}

        response = requests.post(
            f"{BASE_URL}/quick_summary", json=payload, timeout=5
        )

        assert response.status_code == 400
        data = response.json()
        assert "error" in data
        assert "required" in data["error"].lower()

        print("✅ Error handling passed")

    @pytest.mark.requires_llm
    def test_analyze_documents_error(self):
        """Test analyze_documents endpoint error handling (should fail without collection)."""
        payload = {"query": "test", "collection_name": "nonexistent_collection"}

        response = requests.post(
            f"{BASE_URL}/analyze_documents", json=payload, timeout=10
        )

        # This should either work (if collection exists) or return an error
        # We just check it doesn't crash the server
        assert response.status_code in [200, 400, 404, 500]

        data = response.json()
        assert isinstance(data, dict)

        print("✅ Analyze documents error handling passed")
