#!/bin/bash

# IP Commands Handler
# Handles all IP subcommands by calling core functions

cliHandleIPCommands()
{
    local command="$initial_command2"
    local param1="$initial_command3"
    local param2="$initial_command4"
    
    if [[ -z "$command" ]]; then
        cliShowIPHelp
    fi
    
    case "$command" in
        "scan")
            if [[ -z "$param1" ]]; then
                isNotice "Scan type required."
                cliShowIPHelp
            else
                scanIPs "$param1" "$param2"
            fi
            ;;
            
        "allocate")
            if [[ -z "$param1" ]]; then
                isQuestion "Enter app name to allocate IP: "
                read -r app_name
            else
                app_name="$param1"
            fi
            
            if [[ -z "$param2" ]]; then
                isQuestion "Enter IP to allocate: "
                read -r ip_address
            else
                ip_address="$param2"
            fi
            
            allocateIP "$app_name" "$ip_address"
            ;;
            
        "release")
            if [[ -z "$param1" ]]; then
                isQuestion "Enter app name to release IP: "
                read -r app_name
            else
                app_name="$param1"
            fi
            releaseIP "$app_name"
            ;;
            
        "status")
            initializeIPAllocation
            if [[ -n "$param1" ]]; then
                local ip=$(getAppIP "$param1")
                if [[ -n "$ip" ]]; then
                    isSuccessful "$param1 has IP: $ip"
                else
                    isNotice "$param1 has no IP allocated"
                fi
            else
                showIPAllocationStatus
            fi
            ;;
            
        "conflicts")
            initializeIPAllocation
            case "$param1" in
                "detect")
                    detectIPConflicts
                    ;;
                "resolve")
                    resolveIPConflicts
                    ;;
                *)
                    echo "Usage: libreportal ip conflicts [detect|resolve]"
                    ;;
            esac
            ;;
            
        "update-tags")
            if [[ -z "$param1" ]]; then
                isNotice "App name required."
                cliShowIPHelp
            else
                updateIPAddressTags "$param1"
            fi
            ;;
            
        "validate")
            if [[ -z "$param1" ]]; then
                isNotice "App name required."
                cliShowIPHelp
            else
                validateIPTags "$param1"
            fi
            ;;
            
        "report")
            if [[ -z "$param1" ]]; then
                isNotice "Report file required."
                cliShowIPHelp
            else
                generateIPReport "$param1"
            fi
            ;;
            
        *)
            isNotice "Invalid IP command: $command"
            cliShowIPHelp
            ;;
    esac
}
