#!/usr/bin/env python3
import requests
import json
import time

def test_api():
    base_url = "http://localhost:8080"

    print("Testing API endpoints...")
    print("=" * 40)

    # Test endpoints
    endpoints = [
        ("/", "Root endpoint"),
        ("/health", "Health check"),
        ("/api/v1/health", "API health"),
        ("/test", "Test endpoint"),
        ("/info", "Info endpoint")
    ]

    results = {}

    for endpoint, description in endpoints:
        try:
            print(f"Testing {description}: {endpoint}")
            response = requests.get(f"{base_url}{endpoint}", timeout=5)

            if response.status_code == 200:
                data = response.json()
                print(f"  SUCCESS: {data}")
                results[endpoint] = {"status": "success", "data": data}
            else:
                print(f"  FAILED: Status code {response.status_code}")
                results[endpoint] = {"status": "failed", "code": response.status_code}

        except requests.exceptions.ConnectionError:
            print(f"  FAILED: Connection error")
            results[endpoint] = {"status": "connection_error"}
        except requests.exceptions.Timeout:
            print(f"  FAILED: Timeout")
            results[endpoint] = {"status": "timeout"}
        except Exception as e:
            print(f"  FAILED: {e}")
            results[endpoint] = {"status": "error", "error": str(e)}

    # Summary
    print("\n" + "=" * 40)
    print("Test Summary:")

    success_count = sum(1 for r in results.values() if r.get("status") == "success")
    total_count = len(results)

    print(f"Total tests: {total_count}")
    print(f"Successful: {success_count}")
    print(f"Failed: {total_count - success_count}")
    print(f"Success rate: {(success_count/total_count*100):.1f}%")

    if success_count == total_count:
        print("\n🎉 All API tests passed!")
        print("✅ Server is running correctly on port 8080")
        print("✅ You can access:")
        print("   - http://localhost:8080/docs (Swagger UI)")
        print("   - http://localhost:8080/redoc (ReDoc)")
    else:
        print(f"\n⚠️  {total_count - success_count} tests failed")

    return results

if __name__ == "__main__":
    # Wait a bit for server to fully start
    time.sleep(2)
    test_api()