#!/usr/bin/env python3
"""
Fast health check test for all web endpoints
Tests that pages return 200 status without requiring browser automation
"""

import time
from concurrent.futures import ThreadPoolExecutor, as_completed

import pytest
import requests

# Base URL for the application
BASE_URL = "http://localhost:5000"

# All endpoints to test
ENDPOINTS = [
    # Main pages
    "/",
    "/research",
    "/research/results/1",  # Results need research_id
    "/research/api/history",  # History is under research/api
    "/research/settings",  # Settings is under research
    # Metrics pages
    "/metrics",
    "/metrics/costs",
    "/metrics/star-reviews",
    # API endpoints (should return JSON)
    "/metrics/api/cost-analytics",
    "/metrics/api/pricing",
    "/research/settings/api/available-models",  # Settings blueprint has /research/settings prefix
    "/research/settings/api/available-search-engines",  # Settings blueprint has /research/settings prefix
]


def check_single_endpoint(endpoint):
    """Check a single endpoint and return results"""
    url = f"{BASE_URL}{endpoint}"
    start_time = time.time()

    try:
        response = requests.get(url, timeout=10)
        duration = time.time() - start_time

        return {
            "endpoint": endpoint,
            "status": response.status_code,
            "success": response.status_code == 200,
            "duration": round(duration * 1000, 2),  # milliseconds
            "content_type": response.headers.get("content-type", ""),
            "error": None,
        }
    except requests.exceptions.RequestException as e:
        duration = time.time() - start_time
        return {
            "endpoint": endpoint,
            "status": None,
            "success": False,
            "duration": round(duration * 1000, 2),
            "content_type": "",
            "error": str(e),
        }


@pytest.fixture(scope="module")
def server_check():
    """Check if server is running before tests"""
    try:
        response = requests.get(BASE_URL, timeout=5)
        return response.status_code == 200
    except requests.exceptions.RequestException:
        pytest.skip("Server not running at localhost:5000")
        return False


@pytest.mark.parametrize("endpoint", ENDPOINTS)
def test_endpoint_health(endpoint, server_check):
    """Test that each endpoint returns 200 OK"""
    if not server_check:
        pytest.skip("Server not available")

    url = f"{BASE_URL}{endpoint}"
    try:
        response = requests.get(url, timeout=10)
        assert response.status_code == 200, (
            f"Expected 200, got {response.status_code} for {endpoint}"
        )
    except requests.exceptions.RequestException as e:
        pytest.fail(f"Failed to reach {endpoint}: {str(e)}")


def test_all_endpoints_summary(server_check, capsys):
    """Run comprehensive health check and print summary"""
    if not server_check:
        pytest.skip("Server not available")

    print(f"\n🏥 Starting health check for {len(ENDPOINTS)} endpoints...")
    print(f"🌐 Base URL: {BASE_URL}")
    print("-" * 60)

    results = []

    # Test endpoints concurrently for speed
    with ThreadPoolExecutor(max_workers=5) as executor:
        future_to_endpoint = {
            executor.submit(check_single_endpoint, endpoint): endpoint
            for endpoint in ENDPOINTS
        }

        for future in as_completed(future_to_endpoint):
            result = future.result()
            results.append(result)

            # Print result immediately
            status_icon = "✅" if result["success"] else "❌"
            status_text = f"{result['status']}" if result["status"] else "FAIL"
            duration_text = f"{result['duration']:>6.0f}ms"

            print(
                f"{status_icon} {status_text:>3} {duration_text} {result['endpoint']}"
            )

            if result["error"]:
                print(f"    ⚠️  Error: {result['error']}")

    # Summary
    print("-" * 60)
    successful = sum(1 for r in results if r["success"])
    total = len(results)
    success_rate = (successful / total) * 100
    avg_duration = sum(r["duration"] for r in results) / total

    print(
        f"📊 Results: {successful}/{total} endpoints successful ({success_rate:.1f}%)"
    )
    print(f"⏱️  Average response time: {avg_duration:.0f}ms")

    # Failed endpoints details
    failed = [r for r in results if not r["success"]]
    if failed:
        print(f"\n❌ Failed endpoints ({len(failed)}):")
        for result in failed:
            error_msg = result["error"] or f"Status {result['status']}"
            print(f"   • {result['endpoint']} - {error_msg}")

    # API endpoints check
    api_results = [
        r
        for r in results
        if r["endpoint"].startswith("/api") or "/api/" in r["endpoint"]
    ]
    api_successful = sum(1 for r in api_results if r["success"])
    if api_results:
        print(
            f"\n🔌 API endpoints: {api_successful}/{len(api_results)} working"
        )

    # Assert all endpoints are healthy
    assert success_rate == 100.0, (
        f"Only {successful}/{total} endpoints are healthy"
    )


# Keep the script runnable standalone
if __name__ == "__main__":
    import sys

    print("Running health check as standalone script...")
    try:
        # Quick connectivity test first
        print("🔍 Testing connectivity...")
        response = requests.get(BASE_URL, timeout=5)
        print(f"✅ Server is reachable (status: {response.status_code})")
    except requests.exceptions.RequestException as e:
        print(f"❌ Cannot reach server at {BASE_URL}")
        print(f"   Error: {e}")
        print("   Make sure the application is running on localhost:5000")
        sys.exit(1)

    print("\n🏥 Starting health check for all endpoints...")
    print(f"🌐 Base URL: {BASE_URL}")
    print("-" * 60)

    all_success = True
    for endpoint in ENDPOINTS:
        result = check_single_endpoint(endpoint)
        status_icon = "✅" if result["success"] else "❌"
        status_text = f"{result['status']}" if result["status"] else "FAIL"
        duration_text = f"{result['duration']:>6.0f}ms"
        print(
            f"{status_icon} {status_text:>3} {duration_text} {result['endpoint']}"
        )
        if not result["success"]:
            all_success = False

    if all_success:
        print("\n🎉 All endpoints are healthy!")
        sys.exit(0)
    else:
        print("\n⚠️  Some endpoints have issues")
        sys.exit(1)
