#!/bin/bash

# Scan network health
ip_scan_network_health()
{
    echo "🏥 Scanning Network Health..."
    echo ""
    
    # Overall statistics
    local total_services=$(sqlite3 "$docker_dir/$db_file" "SELECT COUNT(*) FROM network_services;" 2>/dev/null)
    local active_services=$(sqlite3 "$docker_dir/$db_file" "SELECT COUNT(*) FROM network_services WHERE status = 'active';" 2>/dev/null)
    local conflict_services=$(sqlite3 "$docker_dir/$db_file" "SELECT COUNT(*) FROM network_services WHERE status = 'conflict';" 2>/dev/null)
    local total_conflicts=$(sqlite3 "$docker_dir/$db_file" "SELECT COUNT(*) FROM network_conflicts WHERE resolution_status = 'pending';" 2>/dev/null)
    
    echo "📊 Overall Statistics:"
    echo "   Total Services: $total_services"
    echo "   Active Services: $active_services"
    echo "   Conflicted Services: $conflict_services"
    echo "   Pending Conflicts: $total_conflicts"
    echo ""
    
    # Health score
    local health_score=0
    if [[ $total_services -gt 0 ]]; then
        health_score=$((active_services * 100 / total_services))
    fi
    
    local health_status="Excellent"
    if [[ $total_conflicts -gt 0 ]]; then
        health_status="Needs Attention"
    elif [[ $health_score -lt 80 ]]; then
        health_status="Poor"
    elif [[ $health_score -lt 95 ]]; then
        health_status="Good"
    fi
    
    echo "🏥 Overall Health: $health_status ($health_score%)"
    echo ""
    
    # Recommendations
    if [[ $total_conflicts -gt 0 ]]; then
        echo "💡 Recommendations:"
        echo "   • Run 'ip_scan_network_conflicts' to identify issues"
        echo "   • Check service configurations for errors"
    fi
    
    if [[ $conflict_services -gt 0 ]]; then
        echo "   • Investigate $conflict_services services in conflict state"
    fi
    
    echo ""
}
