#!/bin/bash

# Scan network conflicts
ip_scan_network_conflicts()
{
    echo "⚠️  Scanning Network Conflicts..."
    echo ""
    
    # IP conflicts
    echo "🌐 IP Conflicts:"
    local ip_conflicts=$(sqlite3 "$docker_dir/$db_file" "SELECT COUNT(*) FROM network_conflicts WHERE conflict_type = 'ip_address' AND resolution_status = 'pending';" 2>/dev/null)
    echo "   IP address conflicts: $ip_conflicts"
    
    # Port conflicts
    echo "🔌 Port Conflicts:"
    local port_conflicts=$(sqlite3 "$docker_dir/$db_file" "SELECT COUNT(*) FROM network_conflicts WHERE conflict_type = 'ip_port' AND resolution_status = 'pending';" 2>/dev/null)
    echo "   Port conflicts: $port_conflicts"
    
    # Domain conflicts
    echo "🌍 Domain Conflicts:"
    local domain_conflicts=$(sqlite3 "$docker_dir/$db_file" "SELECT COUNT(*) FROM network_conflicts WHERE conflict_type = 'domain' AND resolution_status = 'pending';" 2>/dev/null)
    echo "   Domain conflicts: $domain_conflicts"
    
    # Subnet conflicts
    echo "🏠 Subnet Conflicts:"
    local subnet_conflicts=$(sqlite3 "$docker_dir/$db_file" "SELECT COUNT(*) FROM network_conflicts WHERE conflict_type = 'subnet' AND resolution_status = 'pending';" 2>/dev/null)
    echo "   Subnet conflicts: $subnet_conflicts"
    
    local total_conflicts=$((ip_conflicts + port_conflicts + domain_conflicts + subnet_conflicts))
    echo ""
    echo "📊 Total conflicts: $total_conflicts"
    
    if [[ $total_conflicts -gt 0 ]]; then
        echo ""
        echo "🔍 Conflict Details:"
        local sql="SELECT conflict_type, severity, resource_1_value, resource_2_value, detected_date FROM network_conflicts WHERE resolution_status = 'pending' ORDER BY severity DESC, detected_date DESC;"
        
        echo "Type | Severity | Resource 1 | Resource 2 | Date"
        echo "------|----------|------------|------------|------"
        while IFS='|' read -r type severity res1 res2 date; do
            printf "%-6s | %-8s | %-10s | %-10s | %s\n" "$type" "$severity" "$res1" "$res2" "$date"
        done < <(sqlite3 "$docker_dir/$db_file" "$sql" 2>/dev/null)
    fi
    
    echo ""
}
