"""
pytest-compatible API tests for REST API endpoints.
Tests basic functionality and programmatic access integration.
"""

import pytest
import requests

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


class TestRestAPIBasic:
    """Basic tests for REST API endpoints."""

    def test_health_check(self):
        """Test the health check endpoint returns OK status."""
        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
        assert isinstance(data["timestamp"], (int, float))

    def test_api_documentation(self):
        """Test the API documentation endpoint returns proper structure."""
        response = requests.get(f"{BASE_URL}/", timeout=5)
        assert response.status_code == 200

        data = response.json()
        assert data["api_version"] == "v1"
        assert "description" in data
        assert "endpoints" in data
        assert isinstance(data["endpoints"], list)
        assert len(data["endpoints"]) >= 3

    def test_quick_summary_validation(self):
        """Test quick_summary endpoint validates required parameters."""
        # 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()

    def test_analyze_documents_validation(self):
        """Test analyze_documents endpoint validates required parameters."""
        # Test missing collection_name
        payload = {"query": "test"}
        response = requests.post(
            f"{BASE_URL}/analyze_documents", json=payload, timeout=5
        )
        assert response.status_code == 400

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

    def test_generate_report_validation(self):
        """Test generate_report endpoint validates required parameters."""
        # Test missing query parameter
        response = requests.post(
            f"{BASE_URL}/generate_report", json={}, timeout=5
        )
        assert response.status_code == 400

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

    def test_api_accepts_valid_requests(self):
        """Test that API accepts properly formatted requests."""
        payload = {
            "query": "test",
            "search_tool": "wikipedia",
            "iterations": 1,
            "temperature": 0.7,
        }

        try:
            # Short timeout to just verify request format is accepted
            requests.post(f"{BASE_URL}/quick_summary", json=payload, timeout=2)
            # If we reach here, request was accepted
            assert True
        except requests.exceptions.Timeout:
            # Timeout is expected - request was accepted but processing takes time
            assert True
        except requests.exceptions.HTTPError as e:
            if e.response.status_code == 400:
                pytest.fail("Request format was rejected by API")
            else:
                # Other HTTP errors during processing are acceptable
                assert True


@pytest.mark.integration
class TestRestAPIIntegration:
    """Integration tests that may take longer."""

    @pytest.mark.timeout(30)
    @pytest.mark.requires_llm
    def test_quick_summary_test_endpoint(self):
        """Test the quick_summary_test endpoint with minimal query."""
        payload = {"query": "AI"}

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

            if response.status_code == 200:
                data = response.json()
                assert "query" in data
                assert "summary" in data
                assert data["query"] == "AI"
                assert len(data["summary"]) > 0
            elif response.status_code >= 500:
                # Server error might indicate configuration issues
                pytest.skip(f"Server error: {response.status_code}")
            else:
                pytest.fail(f"Unexpected status code: {response.status_code}")

        except requests.exceptions.Timeout:
            pytest.skip("Request timed out - research may be taking too long")


if __name__ == "__main__":
    pytest.main([__file__, "-v"])
