#!/bin/bash

# Scan Traefik services
ip_scan_traefik_services()
{
    echo "🌍 Scanning Traefik Services..."
    echo ""
    
    local sql="SELECT app_name, service_name, domain_name, ip_address, external_port, traefik_router, ssl_enabled, status FROM network_services WHERE service_type = 'traefik' ORDER BY domain_name;"
    
    echo "🌍 Domain | 📦 App/Service | 🌐 IP | 🔌 Port | 🛣️ Router | 🔒 SSL | 📊 Status"
    echo "---------|---------------|------|--------|-----------|------|----------"
    
    while IFS='|' read -r app_name service_name domain_name ip_address external_port traefik_router ssl_enabled status; do
        local ssl_icon="🔓"
        if [[ "$ssl_enabled" == "1" ]]; then
            ssl_icon="🔒"
        fi
        
        local status_icon="🟢"
        case "$status" in
            "inactive") status_icon="🔴" ;;
            "conflict") status_icon="🟡" ;;
            "migrating") status_icon="🔄" ;;
        esac
        
        local port_display="${external_port:-"int"}"
        
        printf "%-9s | %-13s | %-4s | %-6s | %s | %-4s | %s\n" \
            "$domain_name" "$app_name/$service_name" "${ip_address##*.}" "$port_display" "$traefik_router" "$ssl_icon" "$status_icon"
    done < <(sqlite3 "$docker_dir/$db_file" "$sql" 2>/dev/null)
    
    echo ""
}
