#!/bin/bash

# Show network services by category
port_show_network_services_by_category()
{
    local category="$1"
    
    if [[ -z "$category" ]]; then
        echo "Available categories:"
        local sql="SELECT DISTINCT category FROM network_services WHERE status = 'active' ORDER BY category;"
        sqlite3 "$docker_dir/$db_file" "$sql" 2>/dev/null
    fi
    
    echo ""
    echo "📂 Network Services in Category: $category"
    echo "=========================================="
    echo ""
    
    local sql="SELECT app_name, service_name, ip_address, internal_port, external_port, service_type, access_type, status FROM network_services WHERE category = '$category' AND status = 'active' ORDER BY app_name, service_name;"
    
    echo "📦 App/Service | 🌐 IP | 🔌 Port | 🏷️ Type | 🔐 Access | 📊 Status"
    echo "---------------|------|--------|----------|-----------|----------"
    
    while IFS='|' read -r app_name service_name ip_address internal_port external_port service_type access_type status; do
        local port_display="${external_port:-"int"}"
        local status_icon="🟢"
        
        case "$status" in
            "inactive") status_icon="🔴" ;;
            "conflict") status_icon="🟡" ;;
            "migrating") status_icon="🔄" ;;
        esac
        
        printf "%-13s | %-4s | %-6s | %-8s | %-9s | %s\n" \
            "$app_name/$service_name" "${ip_address##*.}" "$port_display" "$service_type" "$access_type" "$status_icon"
    done < <(sqlite3 "$docker_dir/$db_file" "$sql" 2>/dev/null)
    
    echo ""
}
