#!/bin/bash

# Check if IP is available
ipIsAvailable()
{
    local test_ip="$1"
        
    # Validate IP format using regex (same pattern as scanning function)
    if [[ ! "$test_ip" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
        ip_available=""
    elif [[ -z "$test_ip" ]]; then
        ip_available=""
    else
        local existing=$(sqlite3 "$docker_dir/$db_file" "SELECT resource_value FROM network_resources WHERE resource_type = 'ip' AND resource_value = '$test_ip' AND status = 'active';" 2>/dev/null)
        if [[ -n "$existing" ]]; then
            ip_available=""
        elif runFileOp docker network inspect $CFG_NETWORK_NAME --format '{{range .Containers}}{{.IPv4Address}} {{end}}' 2>/dev/null | grep -q "$test_ip"; then
            ip_available=""
        else
            ip_available="$test_ip"
        fi
    fi
}
