# Module: Configuration Accessor Functions
# Purpose: Provides 60+ specific accessor functions for individual configuration paths (debug, sops, paths, output, etc.)
# Dependencies: accessor_core for get-config and config-get

# Get provisioning URL
export def get-provisioning-url [] : nothing -> string {
    $env.PROVISIONING_URL? | default "https://provisioning.systems"
}

# Get components library path (extensions/components — flat structure post-migration).
# Resolution order: PROVISIONING_COMPONENTS_PATH env → paths.components config →
#   derived as sibling of PROVISIONING_TASKSERVS_PATH → PROVISIONING/extensions/components
export def get-components-path [] : nothing -> string {
    let from_env = ($env.PROVISIONING_COMPONENTS_PATH? | default "")
    if ($from_env | is-not-empty) and ($from_env | path exists) { return $from_env }
    let configured = (config-get "paths.components" "")
    if ($configured | is-not-empty) and ($configured | path exists) { return $configured }
    # Derive from PROVISIONING root
    let prov = ($env.PROVISIONING? | default "")
    if ($prov | is-not-empty) {
        let derived = ($prov | path join "extensions" | path join "components")
        if ($derived | path exists) { return $derived }
    }
    ""
}

# Get taskservs library path.
# Post-migration: extensions/components/ is the primary source.
# Falls back to extensions/taskservs/ for non-migrated workspaces.
# Resolution order: PROVISIONING_TASKSERVS_PATH (if exists on disk) →
#   components path → PROVISIONING/extensions/taskservs
export def get-taskservs-path [] : nothing -> string {
    # Env var set by env.nu — already points to components/ post-migration
    let from_env = ($env.PROVISIONING_TASKSERVS_PATH? | default "")
    if ($from_env | is-not-empty) and ($from_env | path exists) { return $from_env }
    # components/ explicit
    let components = (get-components-path)
    if ($components | is-not-empty) and ($components | path exists) { return $components }
    # Legacy taskservs/
    let prov = ($env.PROVISIONING? | default "")
    if ($prov | is-not-empty) {
        let ts = ($prov | path join "extensions" | path join "taskservs")
        if ($ts | path exists) { return $ts }
    }
    $from_env
}

# Get run-taskservs path (workspace-side generated taskserv files)
export def get-run-taskservs-path [] : nothing -> string {
    config-get "paths.run_taskservs" "taskservs"
}

# Get workspace vars format: "json" or "yaml"
export def get-provisioning-wk-format [] : nothing -> string {
    $env.PROVISIONING_WK_FORMAT? | default (config-get "output.format" "yaml")
}

# Whether to use Nickel for taskserv templating — true by default, disable with PROVISIONING_USE_NICKEL=false
export def get-use-nickel [] : nothing -> bool {
    ($env.PROVISIONING_USE_NICKEL? | default "true") != "false"
}

# Path to SOPS keys file (for secrets decryption)
export def get-keys-path [] : nothing -> string {
    config-get "paths.files.keys" ".keys.k"
}

# Path to the vars file for the current taskserv run (set by run.nu make_cmd_env_temp)
export def get-provisioning-vars [] : nothing -> string {
    $env.PROVISIONING_VARS? | default ""
}


# Path to the working env directory for the current taskserv (set by run.nu make_cmd_env_temp)
export def get-provisioning-wk-env-path [] : nothing -> string {
    $env.PROVISIONING_WK_ENV_PATH? | default ""
}

# Path to the extensions/providers/ tree. Resolution order:
# PROVISIONING_PROVIDERS_PATH env → paths.providers config → PROVISIONING/extensions/providers → "".
# Empty result means "no providers available"; callers must guard with `| is-empty` or `| path exists`.
export def get-providers-path [] : nothing -> string {
    let from_env = ($env.PROVISIONING_PROVIDERS_PATH? | default "")
    if ($from_env | is-not-empty) and ($from_env | path exists) { return $from_env }
    let configured = (config-get "paths.providers" "")
    if ($configured | is-not-empty) and ($configured | path exists) { return $configured }
    let prov = ($env.PROVISIONING? | default "")
    if ($prov | is-not-empty) {
        let derived = ($prov | path join "extensions" | path join "providers")
        if ($derived | path exists) { return $derived }
    }
    ""
}

# Path to the shared provider library (extensions/providers/prov_lib/).
export def get-prov-lib-path [] : nothing -> string {
    let providers = (get-providers-path)
    if ($providers | is-empty) { return "" }
    $providers | path join "prov_lib"
}

# Path to provisioning/core/nulib/ from the PROVISIONING root.
export def get-core-nulib-path [] : nothing -> string {
    let prov = ($env.PROVISIONING? | default "")
    if ($prov | is-empty) { return "" }
    $prov | path join "core" | path join "nulib"
}

# Directory name where per-provider generated defs live (relative to a provider dir).
export def get-provisioning-generate-dirpath [] : nothing -> string {
    $env.PROVISIONING_GENERATE_DIRPATH? | default "generate"
}

# Filename for per-provider generated defs inside get-provisioning-generate-dirpath.
export def get-provisioning-generate-defsfile [] : nothing -> string {
    $env.PROVISIONING_GENERATE_DEFSFILE? | default "defs.ncl"
}

# Path to the tools required-versions file (nickel/yaml declaring required tool versions).
# Resolution: PROVISIONING_REQ_VERSIONS env → paths.req_versions config → PROVISIONING/resources/tools.yaml → "".
export def get-provisioning-req-versions [] : nothing -> string {
    let from_env = ($env.PROVISIONING_REQ_VERSIONS? | default "")
    if ($from_env | is-not-empty) and ($from_env | path exists) { return $from_env }
    let configured = (config-get "paths.req_versions" "")
    if ($configured | is-not-empty) and ($configured | path exists) { return $configured }
    let prov = ($env.PROVISIONING? | default "")
    if ($prov | is-not-empty) {
        let derived = ($prov | path join "resources" | path join "tools.yaml")
        if ($derived | path exists) { return $derived }
    }
    ""
}
