# Local Provider Adapter
# Implements the standard provider interface for Local (bare metal/existing servers)

use nulib/local/env.nu
use nulib/local/servers.nu *

# Provider metadata
export def get-provider-metadata []: nothing -> record {
    {
        name: "local"
        version: "1.0.0"
        description: "Local provider for bare metal and existing servers"
        capabilities: {
            server_management: true
            network_management: false  # Limited network management
            storage_management: true
            load_balancer: false
            dns_management: false
            cdn: false
            backup_service: false
            monitoring: false
            logging: false
            auto_scaling: false
            spot_instances: false
            containers: true
            serverless: false
            multi_region: false
            encryption_at_rest: false
            encryption_in_transit: false
            compliance_certifications: []
        }
    }
}

# Server query operations
export def query_servers [
    find?: string
    cols?: string
]: nothing -> list {
    let str_find = if $find != null { $find } else { "" }
    let str_cols = if $cols != null { $cols } else { "" }
    local_query_servers $str_find $str_cols
}

# Server information operations
export def server_info [
    server: record
    check: bool
    find?: string
    cols?: string
]: nothing -> record {
    local_server_info $server $check
}

# Server existence and status operations
export def server_exists [
    server: record
    error_exit: bool
]: nothing -> bool {
    local_server_exists $server $error_exit
}

export def server_is_running [
    server: record
    error_exit: bool
]: nothing -> bool {
    local_server_is_running $server $error_exit
}

# Server lifecycle operations
export def check_server_requirements [
    settings: record
    server: record
    check: bool
]: nothing -> bool {
    local_check_server_requirements $settings $server $check
}

export def create_server [
    settings: record
    server: record
    check: bool
    wait: bool
]: nothing -> bool {
    # Local provider doesn't "create" servers, they already exist
    # This just validates the server is accessible
    try {
        local_server_state $server "started" true $wait $settings
        true
    } catch {
        false
    }
}

export def delete_server [
    settings: record
    server: record
    keep_storage: bool
    error_exit: bool
]: nothing -> bool {
    local_delete_server $settings $server $keep_storage $error_exit
}

export def delete_server_storage [
    settings: record
    server: record
    error_exit: bool
]: nothing -> bool {
    local_delete_server_storage $settings $server $error_exit
}

export def post_create_server [
    settings: record
    server: record
    check: bool
]: nothing -> bool {
    local_post_create_server $settings $server $check
}

export def modify_server [
    settings: record
    server: record
    new_values: list
    error_exit: bool
]: nothing -> bool {
    local_modify_server $settings $server $new_values $error_exit
}

# Server state management
export def server_state [
    server: record
    new_state: string
    error_exit: bool
    wait: bool
    settings: record
]: nothing -> bool {
    local_server_state $server $new_state $error_exit $wait $settings
}

# Network operations
export def get_ip [
    settings: record
    server: record
    ip_type: string
    error_exit: bool
]: nothing -> string {
    let use_type = match $ip_type {
        "$network_public_ip" => "public"
        "$network_private_ip" => "private"
        _ => $ip_type
    }

    let result = (local_server [ "get_ip", $use_type ] --server $server --settings $settings)
    $result | str trim
}

export def servers_ips [
    settings: record
    data: list
    prov?: string
    serverpos?: int
]: nothing -> list {
    mut result = []
    mut index = -1

    for srv in $data {
        $index += 1
        let settings_server = ($settings.data.servers | where {|it| $it.hostname == $srv.hostname})
        if ($settings_server | length) == 0 { continue }
        let provider = ($settings_server | get -o 0 | get -o provider | default "")
        if $prov != null and $provider != "local" { continue }
        if $serverpos != null and $serverpos != $index { continue }

        if $srv.ip_addresses? != null {
            $result = ($result | append ($srv.ip_addresses? |
                each {|it| { hostname: $srv.hostname, ip: $it.address, access: $it.access, family: $it.family }} |
                flatten
            ))
        }
    }

    $result
}

# Infrastructure operations (simplified for local)
export def load_infra_servers_info [
    settings: record
    server: record
    error_exit: bool
]: nothing -> record {
    # Local provider doesn't have cloud infrastructure info
    {
        provider: "local"
        server: $server.hostname
        info: "Local servers don't have cloud infrastructure info"
    }
}

export def load_infra_storages_info [
    settings: record
    server: record
    error_exit: bool
]: nothing -> record {
    # Local storage info is basic
    {
        provider: "local"
        server: $server.hostname
        storage: "Local storage managed directly on server"
    }
}

export def get_infra_storage [
    server: record
    settings: record
    cloud_data: record
    error_exit: bool
]: nothing -> list {
    # Return empty list for local provider
    []
}

export def get_infra_item [
    server: record
    settings: record
    cloud_data: record
    error_exit: bool
]: nothing -> record {
    # Return basic server info
    {
        provider: "local"
        hostname: $server.hostname
        type: "local-server"
    }
}

export def get_infra_price [
    server: record
    data: record
    key: string
    error_exit: bool
    price_col?: string
]: nothing -> float {
    # Local servers don't have pricing
    0.0
}

# Cache operations (local doesn't use cloud caching)
export def start_cache_info [
    settings: record
    server: record
]: nothing -> nothing {
    # No-op for local provider
}

export def create_cache [
    settings: record
    server: record
    error_exit: bool
]: nothing -> nothing {
    # No-op for local provider
}

export def read_cache [
    settings: record
    server: record
    error_exit: bool
]: nothing -> nothing {
    # No-op for local provider
}

export def clean_cache [
    settings: record
    server: record
    error_exit: bool
]: nothing -> nothing {
    # No-op for local provider
}

export def ip_from_cache [
    settings: record
    server: record
    error_exit: bool
]: nothing -> nothing {
    # For local, just return the configured IP
    $server | get -o network_public_ip | default ""
}

# Provider metadata operations
export def on_prov_server [
    server: record
]: nothing -> string {
    local_on_prov_server $server
}

# Provider validation
export def validate_provider []: nothing -> record {
    {
        provider: "local"
        valid: true
        interface_version: "1.0.0"
        capabilities: (get-provider-metadata).capabilities
        last_validated: (date now)
    }
}