# Hetzner Cloud caching operations

# Initialize cache directory
export def hetzner_start_cache_info [settings: record, server: string]: nothing -> nothing {
    if not ($settings | has provider) or not ($settings.provider | has paths) {
        return
    }

    let cache_dir = $"($settings.provider.paths.cache)"

    if not ($cache_dir | path exists) {
        ^mkdir $cache_dir
    }
}

# Create cache entry for server
export def hetzner_create_cache [settings: record, server: string, error_exit: bool = true]: nothing -> nothing {
    hetzner_start_cache_info $settings $server

    let cache_dir = $"($settings.provider.paths.cache)"
    if not ($cache_dir | path exists) {
        if $error_exit {
            error make {msg: $"Cache directory not available: ($cache_dir)"}
        }
        return
    }

    let cache_file = $"($cache_dir)/($server).json"
    let cache_data = {
        server: $server
        timestamp: (now | into int)
        cached_at: (now | format date "%Y-%m-%dT%H:%M:%SZ")
    }

    $cache_data | to json | save --force $cache_file
}

# Read cache entry
export def hetzner_read_cache [settings: record, server: string, error_exit: bool = true]: nothing -> record {
    let cache_dir = $"($settings.provider.paths.cache)"
    let cache_file = $"($cache_dir)/($server).json"

    if not ($cache_file | path exists) {
        if $error_exit {
            error make {msg: $"Cache file not found: ($cache_file)"}
        }
        return {}
    }

    open $cache_file | from json
}

# Clean cache entry
export def hetzner_clean_cache [settings: record, server: string, error_exit: bool = true]: nothing -> nothing {
    let cache_dir = $"($settings.provider.paths.cache)"
    let cache_file = $"($cache_dir)/($server).json"

    if ($cache_file | path exists) {
        rm $cache_file
    }
}

# Get IP from cache
export def hetzner_ip_from_cache [settings: record, server: string, error_exit: bool = true]: nothing -> string {
    let cache = (hetzner_read_cache $settings $server false)

    if ($cache | has ip) {
        $cache.ip
    } else {
        ""
    }
}

# Update cache with server data
export def hetzner_update_cache [settings: record, server: record, error_exit: bool = true]: nothing -> nothing {
    hetzner_start_cache_info $settings $server.hostname

    let cache_dir = $"($settings.provider.paths.cache)"
    if not ($cache_dir | path exists) {
        if $error_exit {
            error make {msg: $"Cache directory not available: ($cache_dir)"}
        }
        return
    }

    let cache_file = $"($cache_dir)/($server.hostname).json"

    let cache_data = {
        server: $server.hostname
        server_id: ($server.id | default "")
        ipv4: ($server.public_net.ipv4.ip | default "")
        ipv6: ($server.public_net.ipv6.ip | default "")
        status: ($server.status | default "")
        location: ($server.location.name | default "")
        server_type: ($server.server_type.name | default "")
        timestamp: (now | into int)
        cached_at: (now | format date "%Y-%m-%dT%H:%M:%SZ")
    }

    $cache_data | to json | save --force $cache_file
}

# Clean all cache
export def hetzner_clean_all_cache [settings: record, error_exit: bool = true]: nothing -> nothing {
    let cache_dir = $"($settings.provider.paths.cache)"

    if ($cache_dir | path exists) {
        rm -r $cache_dir
    }

    ^mkdir $cache_dir
}

# Get cache age in seconds
export def hetzner_cache_age [cache_data: record]: nothing -> int {
    if not ($cache_data | has timestamp) {
        -1
    } else {
        let cached_ts = ($cache_data.timestamp | into int)
        let now_ts = (now | into int)
        $now_ts - $cached_ts
    }
}

# Check if cache is still valid
export def hetzner_cache_valid [cache_data: record, ttl_seconds: int = 3600]: nothing -> bool {
    let age = (hetzner_cache_age $cache_data)
    if $age < 0 {
        false
    } else {
        $age < $ttl_seconds
    }
}
