# Hetzner Cloud utility functions
use env.nu *

# Parse record or string to server name
export def parse_server_identifier [input: any]: nothing -> string {
    if ($input | describe) == "string" {
        $input
    } else if ($input | has hostname) {
        $input.hostname
    } else if ($input | has name) {
        $input.name
    } else if ($input | has id) {
        ($input.id | into string)
    } else {
        ($input | into string)
    }
}

# Check if IP is valid IPv4
export def is_valid_ipv4 [ip: string]: nothing -> bool {
    $ip =~ '^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$'
}

# Check if IP is valid IPv6
export def is_valid_ipv6 [ip: string]: nothing -> bool {
    $ip =~ ':[a-f0-9]{0,4}:' or $ip =~ '^[a-f0-9]{0,4}:[a-f0-9]{0,4}:'
}

# Format record as table for display
export def format_server_table [servers: list]: nothing -> nothing {
    let columns = ["id", "name", "status", "public_net", "server_type"]

    let formatted = $servers | map {|s|
        {
            ID: ($s.id | into string)
            Name: $s.name
            Status: ($s.status | str capitalize)
            IP: ($s.public_net.ipv4.ip | default "-")
            Type: ($s.server_type.name | default "-")
            Location: ($s.location.name | default "-")
        }
    }

    $formatted | table
    null
}

# Get error message from API response
export def extract_api_error [response: any]: nothing -> string {
    if ($response | has error) {
        if ($response.error | has message) {
            $response.error.message
        } else {
            ($response.error | into string)
        }
    } else if ($response | has message) {
        $response.message
    } else {
        ($response | into string)
    }
}

# Validate server configuration
export def validate_server_config [server: record]: nothing -> bool {
    let required = ["hostname", "server_type", "location"]
    let missing = $required | where {|f| not ($server | has $f)}

    if not ($missing | is-empty) {
        error make {msg: $"Missing required fields: ($missing | str join ", ")"}
    }

    true
}

# Convert timestamp to human readable format
export def format_timestamp [timestamp: int]: nothing -> string {
    let date = (now | format date "%Y-%m-%dT%H:%M:%SZ")
    $"($timestamp) (UTC)"
}

# Retry function with exponential backoff (no try-catch)
export def retry_with_backoff [closure: closure, max_attempts: int = 3, initial_delay: int = 1]: nothing -> any {
    mut attempts = 0
    mut delay = $initial_delay

    loop {
        let result = (do { $closure | call } | complete)
        if $result.exit_code == 0 {
            return ($result.stdout)
        }

        $attempts += 1

        if $attempts >= $max_attempts {
            error make {msg: $"Operation failed after ($attempts) attempts: ($result.stderr)"}
        }

        print $"Attempt ($attempts) failed, retrying in ($delay) seconds..."
        sleep ($delay | into duration)
        $delay = $delay * 2
    }
}
