#!/bin/bash

# Show IP allocations
ip_show_allocations()
{
    local filter="$1"  # "all", "active", "released", app_name
    
    echo ""
    echo "🌐 IP Allocations"
    echo "================="
    echo ""
    
    local sql="SELECT app_name, ip_address, subnet, status, purpose, allocated_date FROM ip_allocations"
    
    case "$filter" in
        "active")
            sql="$sql WHERE status = 'active'"
            ;;
        "released")
            sql="$sql WHERE status = 'released'"
            ;;
        "all")
            # No filter
            ;;
        *)
            if [[ -n "$filter" ]]; then
                sql="$sql WHERE app_name = '$filter'"
            fi
            ;;
    esac
    
    sql="$sql ORDER BY status DESC, allocated_date DESC;"
    
    echo "📦 App | 🌐 IP Address | 🏠 Subnet | 📊 Status | 🎯 Purpose | 📅 Date"
    echo "------|--------------|----------|----------|-----------|----------"
    
    while IFS='|' read -r app_name ip_address subnet status purpose allocated_date; do
        local status_icon="🟢"
        case "$status" in
            "released") status_icon="🔴" ;;
            "conflict") status_icon="🟡" ;;
        esac
        
        printf "%-6s | %-12s | %-8s | %-8s | %-9s | %s\n" \
            "$app_name" "$ip_address" "$subnet" "$status_icon $status" "$purpose" "$allocated_date"
    done < <(sqlite3 "$docker_dir/$db_file" "$sql" 2>/dev/null)
    
    echo ""
}
