# DigitalOcean Provider Extension
# Example implementation of provider functions

# Create servers on DigitalOcean
export def digitalocean_create_servers [
    settings: record
    servers: table
    check: bool
    wait: bool
]: nothing -> table {
    $servers | each {|server|
        if $check {
            print $"Would create DigitalOcean droplet: ($server.hostname)"
            {
                hostname: $server.hostname
                provider: "digitalocean"
                status: "simulated"
                public_ip: "203.0.113.1"
                private_ip: "10.0.0.1"
            }
        } else {
            print $"Creating DigitalOcean droplet: ($server.hostname)"

            # Example doctl command (would be actual implementation)
            let result = try {
                ^doctl compute droplet create $server.hostname
                    --size $server.size
                    --image $server.image
                    --region $server.region
                    --ssh-keys $server.ssh_key_fingerprint
                    --wait
            } catch {
                { error: "Failed to create droplet" }
            }

            if ($result | get -o error | is-empty) {
                {
                    hostname: $server.hostname
                    provider: "digitalocean"
                    status: "created"
                    public_ip: "203.0.113.1"  # Would extract from doctl output
                    private_ip: "10.0.0.1"
                }
            } else {
                {
                    hostname: $server.hostname
                    provider: "digitalocean"
                    status: "failed"
                    error: $result.error
                }
            }
        }
    }
}

# Delete servers on DigitalOcean
export def digitalocean_delete_servers [
    settings: record
    servers: table
    check: bool
]: nothing -> table {
    $servers | each {|server|
        if $check {
            print $"Would delete DigitalOcean droplet: ($server.hostname)"
            {
                hostname: $server.hostname
                provider: "digitalocean"
                status: "would_delete"
            }
        } else {
            print $"Deleting DigitalOcean droplet: ($server.hostname)"

            let result = try {
                ^doctl compute droplet delete $server.hostname --force
            } catch {
                { error: "Failed to delete droplet" }
            }

            {
                hostname: $server.hostname
                provider: "digitalocean"
                status: "deleted"
            }
        }
    }
}

# Query servers on DigitalOcean
export def digitalocean_query_servers [
    find: string
    cols: string
]: nothing -> table {
    let droplets = try {
        ^doctl compute droplet list --output json | from json
    } catch {
        []
    }

    $droplets | where ($find | is-empty) or (name =~ $find) | each {|droplet|
        {
            hostname: $droplet.name
            provider: "digitalocean"
            status: $droplet.status
            public_ip: ($droplet.networks.v4 | where type == "public" | get ip | first?)
            private_ip: ($droplet.networks.v4 | where type == "private" | get ip | first?)
            region: $droplet.region.slug
            size: $droplet.size.slug
        }
    }
}

# Get IP address for a server
export def digitalocean_get_ip [
    settings: record
    server: record
    ip_type: string
    public_fallback: bool
]: nothing -> string {
    match $ip_type {
        "public" => {
            $server.public_ip? | default ""
        }
        "private" => {
            let private = ($server.private_ip? | default "")
            if ($private | is-empty) and $public_fallback {
                $server.public_ip? | default ""
            } else {
                $private
            }
        }
        _ => ""
    }
}