#!/usr/bin/env python3
"""
Test script for Phase 7.2 Enhanced Health Monitoring

Tests:
1. Health monitor initialization
2. Metrics recording
3. Health checks
4. System metrics
5. Alert thresholds
6. Metrics export
"""

import asyncio
import json
import os
import sys

# Fix Windows console encoding
if sys.platform == "win32":
    sys.stdout.reconfigure(encoding="utf-8")
    sys.stderr.reconfigure(encoding="utf-8")

# Add project root to path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

from core import AuditLogger, initialize_health_monitor


async def test_health_monitor():
    """Test health monitoring system."""
    print("=" * 60)
    print("Phase 7.2 - Enhanced Health Monitoring Tests")
    print("=" * 60)

    # Initialize components
    print("\n1. Initializing components...")
    audit_logger = AuditLogger()

    health_monitor = initialize_health_monitor(
        audit_logger=audit_logger,
        metrics_retention_hours=24,
        max_metrics_per_project=1000,
    )

    print("✅ Health monitor initialized")
    print("   Retention: 24 hours")
    print("   Max metrics per project: 1000")

    # Test 2: Record some sample metrics
    print("\n2. Recording sample metrics...")

    # Simulate successful requests
    for i in range(5):
        health_monitor.record_request(
            project_id="wordpress_site1", response_time_ms=100.0 + (i * 10), success=True
        )
    print("✅ Recorded 5 successful requests for wordpress_site1")

    # Simulate failed requests
    for i in range(2):
        health_monitor.record_request(
            project_id="wordpress_site1",
            response_time_ms=500.0,
            success=False,
            error_message="Connection timeout",
        )
    print("✅ Recorded 2 failed requests for wordpress_site1")

    # Record for another project
    health_monitor.record_request(project_id="wordpress_site2", response_time_ms=80.0, success=True)
    print("✅ Recorded 1 successful request for wordpress_site2")

    # Test 3: Get project metrics
    print("\n3. Getting project metrics...")
    metrics = health_monitor.get_project_metrics("wordpress_site1", hours=1)
    print("✅ Metrics for wordpress_site1:")
    print(f"   Total requests: {metrics['total_requests']}")
    print(f"   Successful: {metrics['successful_requests']}")
    print(f"   Failed: {metrics['failed_requests']}")
    print(f"   Error rate: {metrics['error_rate_percent']}%")
    print(f"   Avg response time: {metrics['response_time']['average_ms']}ms")

    # Test 4: Get system metrics
    print("\n4. Getting system metrics...")
    system_metrics = health_monitor.get_system_metrics()
    print("✅ System metrics:")
    print(f"   Uptime: {system_metrics.uptime_seconds:.2f}s")
    print(f"   Total requests: {system_metrics.total_requests}")
    print(f"   Successful: {system_metrics.successful_requests}")
    print(f"   Failed: {system_metrics.failed_requests}")
    print(f"   Error rate: {system_metrics.error_rate_percent}%")
    print(f"   Avg response time: {system_metrics.average_response_time_ms}ms")

    # Test 5: Get uptime
    print("\n5. Getting uptime...")
    uptime = health_monitor.get_uptime()
    print(f"✅ Uptime: {uptime['uptime_formatted']}")

    # Test 6: Test alert thresholds
    print("\n6. Testing alert thresholds...")

    # Record a slow request to trigger alert
    health_monitor.record_request(
        project_id="wordpress_site3", response_time_ms=6000.0, success=True  # > 5000ms threshold
    )
    print("✅ Recorded slow request (6000ms)")

    # Record many failures to trigger error rate alert
    for i in range(10):
        health_monitor.record_request(
            project_id="wordpress_site3",
            response_time_ms=200.0,
            success=False,
            error_message="API error",
        )
    print("✅ Recorded 10 failures to trigger error rate alert")

    # Test 7: Check if project exists in manager
    print("\n7. Checking project health (simulation)...")

    # Since we don't have actual WordPress sites running,
    # we'll just show the metrics we collected
    site3_metrics = health_monitor.get_project_metrics("wordpress_site3", hours=1)
    print("✅ wordpress_site3 metrics:")
    print(f"   Total requests: {site3_metrics['total_requests']}")
    print(f"   Error rate: {site3_metrics['error_rate_percent']}%")
    print(f"   Max response time: {site3_metrics['response_time']['max_ms']}ms")

    # Check for alerts
    alert_data = {
        "response_time_ms": site3_metrics["response_time"]["max_ms"],
        "error_rate_percent": site3_metrics["error_rate_percent"],
    }

    alerts = health_monitor._check_alerts("wordpress_site3", alert_data)
    if alerts:
        print("⚠️  Alerts triggered:")
        for alert in alerts:
            print(f"   {alert}")
    else:
        print("✅ No alerts")

    # Test 8: Export metrics
    print("\n8. Exporting metrics...")
    export_path = "logs/test_metrics_export.json"
    exported_file = health_monitor.export_metrics(output_path=export_path)
    print(f"✅ Metrics exported to: {exported_file}")

    # Verify export file
    if os.path.exists(export_path):
        with open(export_path, encoding="utf-8") as f:
            export_data = json.load(f)
        print("   Export contains:")
        print("   - System metrics: ✅")
        print("   - Uptime info: ✅")
        print(f"   - {len(export_data['projects'])} projects")

    # Test 9: Custom alert threshold
    print("\n9. Testing custom alert thresholds...")
    health_monitor.add_alert_threshold(
        project_id="wordpress_site1",
        name="Custom Response Time",
        metric="response_time_ms",
        threshold=150.0,
        comparison="gt",
        severity="warning",
    )
    print("✅ Added custom alert threshold for wordpress_site1")

    # Summary
    print("\n" + "=" * 60)
    print("TEST SUMMARY")
    print("=" * 60)
    print("✅ Health monitor initialization - PASS")
    print("✅ Metrics recording - PASS")
    print("✅ Project metrics retrieval - PASS")
    print("✅ System metrics retrieval - PASS")
    print("✅ Uptime tracking - PASS")
    print("✅ Alert threshold checking - PASS")
    print("✅ Metrics export - PASS")
    print("✅ Custom alert thresholds - PASS")
    print("\n🎉 All tests passed!")
    print("=" * 60)

    return True


if __name__ == "__main__":
    try:
        asyncio.run(test_health_monitor())
        sys.exit(0)
    except Exception as e:
        print(f"\n❌ Test failed with error: {e}")
        import traceback

        traceback.print_exc()
        sys.exit(1)
