#!/usr/bin/env python3
"""
Quick Health Check for WHOOSH Council System

Performs rapid health checks on WHOOSH and CHORUS services.
Useful for monitoring and CI/CD pipelines.

Usage:
    python quick_health_check.py
    python quick_health_check.py --json  # JSON output for monitoring tools
"""

import requests
import sys
import argparse
import json
from datetime import datetime


def check_whoosh(url: str = "http://localhost:8800") -> dict:
    """Check WHOOSH API health"""
    try:
        response = requests.get(f"{url}/api/health", timeout=5)
        return {
            "service": "WHOOSH",
            "status": "healthy" if response.status_code == 200 else "unhealthy",
            "status_code": response.status_code,
            "url": url,
            "error": None
        }
    except Exception as e:
        return {
            "service": "WHOOSH",
            "status": "unreachable",
            "status_code": None,
            "url": url,
            "error": str(e)
        }


def check_project_count(url: str = "http://localhost:8800") -> dict:
    """Check how many projects exist"""
    try:
        headers = {"Authorization": "Bearer dev-token"}
        response = requests.get(f"{url}/api/v1/projects", headers=headers, timeout=5)

        if response.status_code == 200:
            data = response.json()
            projects = data.get("projects", [])
            return {
                "metric": "projects",
                "count": len(projects),
                "status": "ok",
                "error": None
            }
        else:
            return {
                "metric": "projects",
                "count": 0,
                "status": "error",
                "error": f"HTTP {response.status_code}"
            }
    except Exception as e:
        return {
            "metric": "projects",
            "count": 0,
            "status": "error",
            "error": str(e)
        }


def check_p2p_discovery(url: str = "http://localhost:8800") -> dict:
    """Check P2P discovery is finding agents"""
    # Note: This would require a dedicated endpoint
    # For now, we'll return a placeholder
    return {
        "metric": "p2p_discovery",
        "status": "not_implemented",
        "note": "Add /api/v1/p2p/agents endpoint to WHOOSH"
    }


def main():
    parser = argparse.ArgumentParser(description="Quick health check for WHOOSH")
    parser.add_argument("--whoosh-url", default="http://localhost:8800",
                        help="WHOOSH base URL")
    parser.add_argument("--json", action="store_true",
                        help="Output JSON for monitoring tools")

    args = parser.parse_args()

    # Perform checks
    results = {
        "timestamp": datetime.now().isoformat(),
        "checks": {
            "whoosh": check_whoosh(args.whoosh_url),
            "projects": check_project_count(args.whoosh_url),
            "p2p": check_p2p_discovery(args.whoosh_url)
        }
    }

    # Calculate overall health
    whoosh_healthy = results["checks"]["whoosh"]["status"] == "healthy"
    projects_ok = results["checks"]["projects"]["status"] == "ok"

    results["overall_status"] = "healthy" if whoosh_healthy and projects_ok else "degraded"

    if args.json:
        # JSON output for monitoring
        print(json.dumps(results, indent=2))
        sys.exit(0 if results["overall_status"] == "healthy" else 1)
    else:
        # Human-readable output
        print("="*60)
        print("WHOOSH SYSTEM HEALTH CHECK")
        print("="*60)
        print(f"Timestamp: {results['timestamp']}\n")

        # WHOOSH Service
        whoosh = results["checks"]["whoosh"]
        status_symbol = "✓" if whoosh["status"] == "healthy" else "✗"
        print(f"{status_symbol} WHOOSH API: {whoosh['status']}")
        if whoosh["error"]:
            print(f"  Error: {whoosh['error']}")
        print(f"  URL: {whoosh['url']}\n")

        # Projects
        projects = results["checks"]["projects"]
        print(f"📊 Projects: {projects['count']}")
        if projects["error"]:
            print(f"  Error: {projects['error']}")
        print()

        # Overall
        print("="*60)
        overall = results["overall_status"]
        print(f"Overall Status: {overall.upper()}")
        print("="*60)

        sys.exit(0 if overall == "healthy" else 1)


if __name__ == "__main__":
    main()
