#!/usr/bin/env python3
"""
CI Test for HTTP API Examples

This script tests the HTTP API examples to ensure they work correctly in CI.
It runs the examples with proper error handling and timeouts.

Requirements:
- LDR server running with fallback LLM
- Beautiful Soup installed
"""

import sys
import os
import subprocess
import time
import requests
from pathlib import Path

# Add src directory to Python path
sys.path.insert(0, str(Path(__file__).parent.parent.parent / "src"))


def check_server_health(base_url="http://localhost:5000", timeout=30):
    """Check if the LDR server is running and healthy."""
    print(f"Checking server health at {base_url}...")

    start_time = time.time()
    while time.time() - start_time < timeout:
        try:
            response = requests.get(f"{base_url}/auth/check", timeout=5)
            if response.status_code in [
                200,
                401,
            ]:  # 401 is expected when not authenticated
                print("✅ Server is healthy")
                return True
        except requests.exceptions.ConnectionError:
            pass

        print(
            f"Waiting for server... ({int(time.time() - start_time)}/{timeout}s)"
        )
        time.sleep(1)

    print("❌ Server health check failed")
    return False


def run_example(example_path, timeout=300):
    """Run a single HTTP example with timeout."""
    print(f"\n=== Testing {example_path.name} ===")

    try:
        # Run the example with timeout
        result = subprocess.run(
            [sys.executable, str(example_path)],
            capture_output=True,
            text=True,
            timeout=timeout,
            env={**os.environ, "LDR_USE_FALLBACK_LLM": "true"},
        )

        print(f"Exit code: {result.returncode}")
        if result.stdout:
            print(f"STDOUT:\n{result.stdout}")
        if result.stderr:
            print(f"STDERR:\n{result.stderr}")

        # Check for success indicators
        success_indicators = [
            "✅ User created successfully",
            "🎉 SUCCESS! API is working correctly",
            "✅ Login successful",
            "Research started successfully!",
        ]

        if result.returncode == 0 and any(
            indicator in result.stdout for indicator in success_indicators
        ):
            print(f"✅ {example_path.name} passed")
            return True
        else:
            print(f"❌ {example_path.name} failed")
            return False

    except subprocess.TimeoutExpired:
        print(f"❌ {example_path.name} timed out after {timeout} seconds")
        return False
    except Exception as e:
        print(f"❌ {example_path.name} failed with exception: {e}")
        return False


def main():
    """Run all HTTP API examples tests."""
    print("=== HTTP API Examples CI Test ===")

    # Check server health first
    if not check_server_health():
        sys.exit(1)

    # Define examples to test
    examples_dir = (
        Path(__file__).parent.parent.parent / "examples" / "api_usage" / "http"
    )
    advanced_dir = examples_dir / "advanced"
    examples_to_test = [
        "simple_working_example.py",
        advanced_dir / "http_api_examples.py",
        # Note: We skip simple_http_example.py in CI as it takes too long
    ]

    results = {}

    for example_name in examples_to_test:
        example_path = examples_dir / example_name
        if not example_path.exists():
            print(f"⚠️  Example {example_name} not found, skipping")
            continue

        results[example_name] = run_example(example_path)

    # Summary
    print("\n=== Test Summary ===")
    passed = sum(results.values())
    total = len(results)

    for example, success in results.items():
        status = "✅ PASS" if success else "❌ FAIL"
        print(f"{status} {example}")

    print(f"\nTotal: {passed}/{total} examples passed")

    if passed == total:
        print("🎉 All HTTP API examples tests passed!")
        return 0
    else:
        print("❌ Some HTTP API examples tests failed!")
        return 1


if __name__ == "__main__":
    sys.exit(main())
