"""
Health Check and Readiness Endpoints for Kubernetes

This module provides endpoints for monitoring application health,
readiness, and liveness probes required by Kubernetes.
"""

import time
from datetime import datetime
from typing import Dict, Any

import reflex as rx
from sqlalchemy import text

# Import your database session
# from src.infrastructure.database.unit_of_work import get_db_session


class HealthStatus:
    """Health check status manager"""
    
    start_time = time.time()
    
    @staticmethod
    def get_uptime() -> float:
        """Get application uptime in seconds"""
        return time.time() - HealthStatus.start_time
    
    @staticmethod
    def check_database() -> Dict[str, Any]:
        """Check database connectivity"""
        try:
            # TODO: Uncomment when database is configured
            # with get_db_session() as session:
            #     session.execute(text("SELECT 1"))
            return {
                "status": "healthy",
                "message": "Database connection OK",
                "latency_ms": 0
            }
        except Exception as e:
            return {
                "status": "unhealthy",
                "message": f"Database connection failed: {str(e)}",
                "latency_ms": None
            }
    
    @staticmethod
    def check_redis() -> Dict[str, Any]:
        """Check Redis connectivity"""
        try:
            # TODO: Uncomment when Redis is configured
            # from src.config.cache import get_redis_client
            # redis_client = get_redis_client()
            # redis_client.ping()
            return {
                "status": "healthy",
                "message": "Redis connection OK",
                "latency_ms": 0
            }
        except Exception as e:
            return {
                "status": "unhealthy",
                "message": f"Redis connection failed: {str(e)}",
                "latency_ms": None
            }


def ping_endpoint() -> Dict[str, Any]:
    """
    Basic health check endpoint - /ping
    
    This is the simplest health check that returns immediately.
    Used by load balancers and monitoring systems.
    
    Returns:
        dict: Basic health status
    """
    return {
        "status": "ok",
        "service": "peikarband",
        "timestamp": datetime.utcnow().isoformat(),
        "uptime_seconds": round(HealthStatus.get_uptime(), 2)
    }


def health_endpoint() -> Dict[str, Any]:
    """
    Detailed health check endpoint - /health
    
    Returns comprehensive health information including:
    - Application status
    - Database connectivity
    - Redis connectivity
    - System resources
    
    Returns:
        dict: Detailed health status
    """
    health_data = {
        "status": "healthy",
        "service": "peikarband",
        "version": "0.1.0",
        "timestamp": datetime.utcnow().isoformat(),
        "uptime_seconds": round(HealthStatus.get_uptime(), 2),
        "checks": {}
    }
    
    # Check database
    db_status = HealthStatus.check_database()
    health_data["checks"]["database"] = db_status
    
    # Check Redis
    redis_status = HealthStatus.check_redis()
    health_data["checks"]["redis"] = redis_status
    
    # Determine overall health
    if (db_status["status"] == "unhealthy" or 
        redis_status["status"] == "unhealthy"):
        health_data["status"] = "degraded"
    
    return health_data


def ready_endpoint() -> Dict[str, Any]:
    """
    Readiness probe endpoint - /ready
    
    Used by Kubernetes to determine if the pod is ready to receive traffic.
    Checks if all dependencies are available.
    
    Returns:
        dict: Readiness status
    """
    ready_data = {
        "ready": True,
        "service": "peikarband",
        "timestamp": datetime.utcnow().isoformat(),
        "checks": {}
    }
    
    # Check database
    db_status = HealthStatus.check_database()
    ready_data["checks"]["database"] = db_status
    if db_status["status"] == "unhealthy":
        ready_data["ready"] = False
    
    # Check Redis
    redis_status = HealthStatus.check_redis()
    ready_data["checks"]["redis"] = redis_status
    if redis_status["status"] == "unhealthy":
        ready_data["ready"] = False
    
    return ready_data


def live_endpoint() -> Dict[str, Any]:
    """
    Liveness probe endpoint - /live
    
    Used by Kubernetes to determine if the pod should be restarted.
    This should only fail if the application is completely stuck.
    
    Returns:
        dict: Liveness status
    """
    return {
        "alive": True,
        "service": "peikarband",
        "timestamp": datetime.utcnow().isoformat(),
        "uptime_seconds": round(HealthStatus.get_uptime(), 2)
    }


def metrics_endpoint() -> Dict[str, Any]:
    """
    Basic metrics endpoint - /metrics
    
    Returns basic application metrics.
    For production, use Prometheus metrics format.
    
    Returns:
        dict: Application metrics
    """
    import psutil
    import os
    
    process = psutil.Process(os.getpid())
    
    return {
        "service": "peikarband",
        "timestamp": datetime.utcnow().isoformat(),
        "uptime_seconds": round(HealthStatus.get_uptime(), 2),
        "system": {
            "cpu_percent": psutil.cpu_percent(interval=0.1),
            "memory_percent": psutil.virtual_memory().percent,
            "disk_percent": psutil.disk_usage('/').percent
        },
        "process": {
            "memory_mb": round(process.memory_info().rss / 1024 / 1024, 2),
            "cpu_percent": process.cpu_percent(interval=0.1),
            "threads": process.num_threads()
        }
    }


# Export functions for use in API routes
__all__ = [
    "ping_endpoint",
    "health_endpoint",
    "ready_endpoint",
    "live_endpoint",
    "metrics_endpoint"
]

