# Module: Configuration Accessor System
# Reads platform service endpoints from deployment-mode.ncl via the platform target module.
# All other paths return their default values.

use ../../platform/target.nu [load-deployment-mode]

# Build a service URL from a service config record (server.{host,port} or endpoint field).
def service-cfg-url [cfg: record]: nothing -> string {
    let explicit = ($cfg | get -o endpoint | default "")
    if ($explicit | is-not-empty) { return $explicit }
    let srv = ($cfg | get -o server)
    if $srv == null { return "" }
    let host = ($srv | get -o host | default "127.0.0.1")
    let port = ($srv | get -o port | default 0)
    if $port == 0 { "" } else { $"http://($host):($port)" }
}

# Resolve known platform URL paths from deployment-mode.ncl.
# Returns null for unrecognised paths so config-get falls back to default_value.
def resolve-platform-path [deployment: record, path: string]: nothing -> any {
    match $path {
        "platform.orchestrator.url" => {
            let svc = ($deployment | get -o orchestrator)
            if $svc == null { null } else { let u = (service-cfg-url $svc); if ($u | is-empty) { null } else { $u } }
        }
        "platform.orchestrator.endpoint" => {
            let svc = ($deployment | get -o orchestrator)
            if $svc == null { null } else { let u = (service-cfg-url $svc); if ($u | is-empty) { null } else { $"($u)/health" } }
        }
        "platform.control_center.url" => {
            let svc = ($deployment | get -o control_center)
            if $svc == null { null } else { let u = (service-cfg-url $svc); if ($u | is-empty) { null } else { $u } }
        }
        "platform.kms.url" => {
            let svc = ($deployment | get -o vault_service)
            if $svc == null { null } else { let u = (service-cfg-url $svc); if ($u | is-empty) { null } else { $u } }
        }
        "platform.kms.endpoint" => {
            let svc = ($deployment | get -o vault_service)
            if $svc == null { null } else { let u = (service-cfg-url $svc); if ($u | is-empty) { null } else { $"($u)/health" } }
        }
        _ => { null }
    }
}

export def get-config []: nothing -> record {
    load-deployment-mode
}

export def config-get [path: string, default_value: any = null, --config: any = null]: nothing -> any {
    let deployment = if ($config != null) { $config } else { load-deployment-mode }
    let val = (resolve-platform-path $deployment $path)
    if $val == null { $default_value } else { $val }
}

export def get-full-config []: nothing -> record {
    load-deployment-mode
}

# Selective re-export (ADR-025 Phase 3 Layer 3).
export use ./functions.nu [
    get-provisioning-url get-components-path get-taskservs-path
    get-run-taskservs-path get-provisioning-wk-format get-use-nickel
    get-keys-path get-provisioning-vars get-provisioning-wk-env-path
    get-providers-path get-prov-lib-path get-core-nulib-path
    get-provisioning-generate-dirpath get-provisioning-generate-defsfile
    get-provisioning-req-versions
]
