#!/bin/bash

# Show network services by app
port_show_network_services_by_app()
{
    local app_name="$1"
    
    if [[ -z "$app_name" ]]; then
        echo "Available apps:"
        local sql="SELECT DISTINCT app_name FROM network_services WHERE status = 'active' ORDER BY app_name;"
        sqlite3 "$docker_dir/$db_file" "$sql" 2>/dev/null
    fi
    
    echo ""
    echo "📦 Network Services for App: $app_name"
    echo "====================================="
    echo ""
    
    local sql="SELECT service_name, ip_address, internal_port, external_port, service_type, access_type, category, status FROM network_services WHERE app_name = '$app_name' AND status = 'active' ORDER BY service_name;"
    
    echo "🔌 Service | 🌐 IP | 🔌 Port | 🏷️ Type | 🔐 Access | 📂 Category | 📊 Status"
    echo "-----------|------|--------|----------|-----------|------------|----------"
    
    while IFS='|' read -r service_name ip_address internal_port external_port service_type access_type category 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 "%-9s | %-4s | %-6s | %-8s | %-9s | %-10s | %s\n" \
            "$service_name" "${ip_address##*.}" "$port_display" "$service_type" "$access_type" "$category" "$status_icon"
    done < <(sqlite3 "$docker_dir/$db_file" "$sql" 2>/dev/null)
    
    echo ""
}
