use std
use lib_provisioning/config/accessor.nu *
export-env {
    # Detect active workspace BEFORE loading config
    let active_workspace = do {
        let user_config_path = ($env.HOME | path join "Library" | path join "Application Support" | path join "provisioning" | path join "user_config.yaml")

        if ($user_config_path | path exists) {
            let user_config = (open $user_config_path)
            if ($user_config.active_workspace != null) {
                let workspace_name = $user_config.active_workspace
                let workspaces = ($user_config.workspaces | where name == $workspace_name)
                if ($workspaces | length) > 0 {
                    ($workspaces | first).path
                } else {
                    ""
                }
            } else {
                ""
            }
        } else {
            ""
        }
    }

    $env.PROVISIONING_KLOUD_PATH = if ($active_workspace | is-not-empty) {
        $active_workspace
    } else {
        ($env.PROVISIONING_KLOUD_PATH? | default "")
    }

    # Don't load config during export-env to avoid hanging on module parsing
    # Config will be loaded on-demand when accessed later
    let config = {}

    # Try to get PROVISIONING path from config, environment, or detect from project structure
    let provisioning_from_config = (config-get "provisioning.path" "" --config $config)
    let provisioning_from_env = ($env.PROVISIONING? | default "")

    # Detect project root if not already configured
    let potential_roots = [
        ($env.PWD)
        (if ($provisioning_from_env | is-not-empty) { $provisioning_from_env } else { "" })
        (if ($provisioning_from_config | is-not-empty) { $provisioning_from_config } else { "" })
        "/usr/local/provisioning"
    ]

    let detected_root = ($potential_roots
        | where { |path| ($path | path join "core" "nulib" | path exists) }
        | first
        | default "/usr/local/provisioning")

    $env.PROVISIONING = if ($provisioning_from_config | is-not-empty) {
        $provisioning_from_config
    } else if ($provisioning_from_env | is-not-empty) {
        $provisioning_from_env
    } else {
        $detected_root
    }

    $env.PROVISIONING_CORE = ($env.PROVISIONING | path join "core")
    if ($env.PROVISIONING_CORE | path exists)  == false {
        # For workspace-exempt commands, we don't need valid paths - skip validation
        # The workspace enforcement will catch commands that actually need workspace
        # Just set it to a reasonable default
        $env.PROVISIONING_CORE = "/usr/local/provisioning/core"
    }
    $env.PROVISIONING_PROVIDERS_PATH  = ($env.PROVISIONING | path join "extensions" | path join "providers")
    $env.PROVISIONING_COMPONENTS_PATH = ($env.PROVISIONING | path join "extensions" | path join "components")
    # Keep for backward compat — points to taskservs/ if it exists, falls back to components/
    let _ts_path = ($env.PROVISIONING | path join "extensions" | path join "taskservs")
    $env.PROVISIONING_TASKSERVS_PATH = if ($env.PROVISIONING_COMPONENTS_PATH | path exists) {
        $env.PROVISIONING_COMPONENTS_PATH
    } else {
        $_ts_path
    }
    $env.PROVISIONING_CLUSTERS_PATH   = ($env.PROVISIONING | path join "extensions" | path join "clusters")
    $env.PROVISIONING_RESOURCES = ($env.PROVISIONING | path join "resources" )
    $env.PROVISIONING_NOTIFY_ICON = ($env.PROVISIONING_RESOURCES | path join "images"| path join "cloudnative.png")

    $env.PROVISIONING_DEBUG = (config-get "debug.enabled" false --config $config)
    $env.PROVISIONING_METADATA = (config-get "debug.metadata" false --config $config)

    $env.PROVISIONING_DEBUG_CHECK = (config-get "debug.check" false --config $config)
    $env.PROVISIONING_DEBUG_REMOTE = (config-get "debug.remote" false --config $config)
    $env.PROVISIONING_LOG_LEVEL = (config-get "debug.log_level" "" --config $config)

    $env.PROVISIONING_NO_TERMINAL = (config-get "debug.no_terminal" false --config $config)
    # Only set NO_TITLES from config if not already set via environment
    let no_titles_env = ($env.PROVISIONING_NO_TITLES? | default "")
    $env.PROVISIONING_NO_TITLES = if ($no_titles_env | is-not-empty) {
        ($no_titles_env == "true" or $no_titles_env == "1" or $no_titles_env == true)
    } else {
        (config-get "debug.no_titles" false --config $config)
    }
    $env.PROVISIONING_ARGS = ($env.PROVISIONING_ARGS? | default "")
    $env.PROVISIONING_MODULE = ($env.PROVISIONING_MODULE? | default "")
    $env.PROVISIONING_NAME = (config-get "core.name" "provisioning" --config $config)

    $env.PROVISIONING_FILEVIEWER = (config-get "output.file_viewer" "bat" --config $config)

    $env.PROVISIONING_METADATA = if ($env.PROVISIONING_ARGS? | str contains "--xm" ) { true } else { $env.PROVISIONING_METADATA }
    $env.PROVISIONING_DEBUG_CHECK = if ($env.PROVISIONING_ARGS? | str contains "--xc" ) { true } else { $env.PROVISIONING_DEBUG_CHECK }
    $env.PROVISIONING_DEBUG_REMOTE = if ($env.PROVISIONING_ARGS? | str contains "--xr" ) { true } else { $env.PROVISIONING_DEBUG_REMOTE }
    $env.PROVISIONING_LOG_LEVEL = if ($env.PROVISIONING_ARGS? | str contains "--xld" ) { "debug" } else {  $env.PROVISIONING_LOG_LEVEL }

    if $env.PROVISIONING_LOG_LEVEL == "debug" or $env.PROVISIONING_LOG_LEVEL == "DEBUG"  { $env.NU_LOG_LEVEL = "DEBUG" } else { $env.NU_LOG_LEVEL = ""}

    $env.PROVISIONING_INFRA_PATH = ($env.PROVISIONING_KLOUD_PATH? | default
        (config-get "paths.infra" | default $env.PWD ) | into string)

    $env.PROVISIONING_DFLT_SET = (config-get "paths.files.settings" | default  "settings.ncl" | into string)

    $env.NOW = (date now | format date "%Y_%m_%d_%H_%M_%S")
    $env.PROVISIONING_MATCH_DATE = ($env.PROVISIONING_MATCH_DATE? | default "%Y_%m")

    #$env.PROVISIONING_MATCH_CMD = "v"

    $env.PROVISIONING_WK_FORMAT = (config-get "output.format" | default "yaml" | into string)

    $env.PROVISIONING_REQ_VERSIONS = ($env.PROVISIONING | path join "core" | path join "versions.yaml")
    $env.PROVISIONING_TOOLS_PATH = ($env.PROVISIONING | path join "core" | path join "tools")
    $env.PROVISIONING_TEMPLATES_PATH = ($env.PROVISIONING | path join "templates")
    $env.SSH_OPS = [StrictHostKeyChecking=accept-new UserKnownHostsFile=(if $nu.os-info.name == "windows" { "NUL" } else { "/dev/null" })]

    # Path for cloud local tasks definition can not exist if all tasks are using library install mode from 'lib-tasks'
    $env.PROVISIONING_RUN_TASKSERVS_PATH = "taskservs"
    $env.PROVISIONING_RUN_CLUSTERS_PATH = "clusters"
    $env.PROVISIONING_GENERATE_DIRPATH = "generate"
    $env.PROVISIONING_GENERATE_DEFSFILE = "defs.toml"

    $env.PROVISIONING_KEYS_PATH = (config-get "paths.files.keys" ".keys.ncl" --config $config)

    $env.PROVISIONING_USE_NICKEL_PLUGIN = if ( (version).installed_plugins | str contains "nickel" ) { true } else { false }
    #$env.PROVISIONING_J2_PARSER = ($env.PROVISIONING_$TOOLS_PATH | path join "parsetemplate.py")
    #$env.PROVISIONING_J2_PARSER = (^bash -c "type -P tera")
    $env.PROVISIONING_USE_TERA_PLUGIN = if ( (version).installed_plugins | str contains "tera" ) { true } else { false }

    # Provisioning critical plugins (10-30x performance improvement)
    # These plugins provide native Rust performance for auth, KMS, and orchestrator operations
    let installed_plugins = ((version).installed_plugins | default "")
    $env.PROVISIONING_USE_AUTH_PLUGIN = ($installed_plugins | str contains "nu_plugin_auth")
    $env.PROVISIONING_USE_KMS_PLUGIN = ($installed_plugins | str contains "nu_plugin_kms")
    $env.PROVISIONING_USE_ORCH_PLUGIN = ($installed_plugins | str contains "nu_plugin_orchestrator")

    # Combined plugin availability flag
    $env.PROVISIONING_PLUGINS_AVAILABLE = ($env.PROVISIONING_USE_AUTH_PLUGIN
        or $env.PROVISIONING_USE_KMS_PLUGIN
        or $env.PROVISIONING_USE_ORCH_PLUGIN)

    # Plugin status check (variables set, but don't warn unless explicitly requested)
    # Users will be notified only if they try to use a plugin that's not available
    # This keeps the interactive experience clean while still supporting fallback to HTTP

    $env.PROVISIONING_URL = ($env.PROVISIONING_URL? | default  "https://provisioning.systems" | into string)
    # Refactored from try-catch to do/complete for explicit error handling
    #let parts_k = (do { $env.PROVISIONING_ARGS | split row "-k" | get 1 } | complete)
    #let infra = if $parts_k.exit_code == 0 {
    #    ($parts_k.stdout | str trim)
    #} else {
    #    let parts_space = (do { $env.PROVISIONING_ARGS | split row " " | get 1 } | complete)
    #    if $parts_space.exit_code == 0 { ($parts_space.stdout | str trim) } else { "" }
    #}
    #$env.CURR_KLOUD = if $infra == "" { (^pwd) } else { $infra }

    $env.PROVISIONING_USE_SOPS = (config-get "sops.use_sops" | default  "age" | into string)
    $env.PROVISIONING_USE_KMS = (config-get "sops.use_kms" | default "" | into string)
    $env.PROVISIONING_SECRET_PROVIDER = (config-get "sops.secret_provider" | default "sops" | into string)

    # AI Configuration
    $env.PROVISIONING_AI_ENABLED = (config-get "ai.enabled" | default false | into bool | into string)
    $env.PROVISIONING_AI_PROVIDER = (config-get "ai.provider" | default "openai" | into string)
    $env.PROVISIONING_LAST_ERROR = ""

    # CLI Daemon Configuration
    $env.PROVISIONING_DAEMON_URL = ($env.PROVISIONING_DAEMON_URL? | default "http://localhost:9091" | into string)


    # For SOPS if settings below fails -> look at: sops_env.nu  loaded when is need to set env context
    let curr_infra = (config-get "paths.infra" "" --config $config)
    if $curr_infra != "" { $env.CURRENT_INFRA_PATH = $curr_infra }

    let sops_path = (config-get "sops.config_path" | default "" | str replace "KLOUD_PATH" $env.PROVISIONING_KLOUD_PATH)
    if $sops_path != "" {
       $env.PROVISIONING_SOPS = $sops_path
    } else if $env.CURRENT_KLOUD_PATH? != null and ($env.CURRENT_INFRA_PATH | is -not-empty) {
        $env.PROVISIONING_SOPS = (get_def_sops $env.CURRENT_KLOUD_PATH)
    }

    let kage_path = (config-get "sops.key_path" | default "" | str replace "KLOUD_PATH" $env.PROVISIONING_KLOUD_PATH)
    if $kage_path != "" {
        $env.PROVISIONING_KAGE = $kage_path
    } else if $env.CURRENT_KLOUD_PATH? != null and ($env.CURRENT_INFRA_PATH | is-not-empty) {
        $env.PROVISIONING_KAGE = (get_def_age $env.CURRENT_KLOUD_PATH)
    }

    if $env.PROVISIONING_KAGE? != null and ($env.PROVISIONING_KAGE | is-not-empty) {
        $env.SOPS_AGE_KEY_FILE = $env.PROVISIONING_KAGE
        $env.SOPS_AGE_RECIPIENTS = (grep "public key:" $env.SOPS_AGE_KEY_FILE | split row ":" |
            get -o 1  | str trim |  default "")
        if $env.SOPS_AGE_RECIPIENTS == "" {
            print $"❗Error no key found in (_ansi red_bold)($env.SOPS_AGE_KEY_FILE)(_ansi reset) file for secure AGE operations "
            exit 1
        }
    }
    $env.PROVISIONING_OUT = ($env.PROVISIONING_OUT? | default "")
    if ($env.PROVISIONING_OUT | is-not-empty) {
        $env.PROVISIONING_NO_TERMINAL = true
        # if ($env.PROVISIONING_OUT | str ends-with ".yaml") or ($env.PROVISIONING_OUT | str ends-with ".yml") {
        #     $env.PROVISIONING_NO_TERMINAL = true
        # } else if ($env.PROVISIONING_OUT | str ends-with ".json") {
        #     $env.PROVISIONING_NO_TERMINAL = true
        # } else {
        #     $env.PROVISIONING_NO_TERMINAL = true
        # }
    }
    # Nickel Module Path Configuration
    # Set up NICKEL_IMPORT_PATH to help Nickel resolve modules when running from different directories
    $env.NICKEL_IMPORT_PATH = ($env.NICKEL_IMPORT_PATH? | default [] | append [
        $env.PROVISIONING
        ($env.PROVISIONING | path join "nickel")
        ($env.PROVISIONING_PROVIDERS_PATH)
        $env.PWD
    ] | uniq | str join ":")

    # Path helpers for dynamic imports
    $env.PROVISIONING_CORE_NULIB = ($env.PROVISIONING | path join "core" "nulib")
    $env.PROVISIONING_PROV_LIB = ($env.PROVISIONING_PROVIDERS_PATH | path join "prov_lib")

    # Add extensions paths to NU_LIB_DIRS for module discovery
    $env.NU_LIB_DIRS = ($env.NU_LIB_DIRS? | default [] | append [
        $env.PROVISIONING_PROVIDERS_PATH
        $env.PROVISIONING_TASKSERVS_PATH
        $env.PROVISIONING_CLUSTERS_PATH
        ($env.PROVISIONING | path join "extensions")
        $env.PROVISIONING_CORE_NULIB
    ] | uniq)

    # Extension System Configuration
    $env.PROVISIONING_EXTENSIONS_PATH = ($env.PROVISIONING_EXTENSIONS_PATH? | default
        (config-get "extensions.path" | default "") | into string)

    $env.PROVISIONING_EXTENSION_MODE = ($env.PROVISIONING_EXTENSION_MODE? | default
        (config-get "extensions.mode" | default "full") | into string)

    $env.PROVISIONING_PROFILE = ($env.PROVISIONING_PROFILE? | default
        (config-get "extensions.profile" | default "") | into string)

    $env.PROVISIONING_ALLOWED_EXTENSIONS = ($env.PROVISIONING_ALLOWED_EXTENSIONS? | default
        (config-get "extensions.allowed" | default "") | into string)

    $env.PROVISIONING_BLOCKED_EXTENSIONS = ($env.PROVISIONING_BLOCKED_EXTENSIONS? | default
        (config-get "extensions.blocked" | default "") | into string)

    # Custom paths for extensions
    $env.PROVISIONING_CUSTOM_PROVIDERS = ($env.PROVISIONING_CUSTOM_PROVIDERS? | default "" | into string)
    $env.PROVISIONING_CUSTOM_TASKSERVS = ($env.PROVISIONING_CUSTOM_TASKSERVS? | default "" | into string)

    # Project-local environment should be loaded manually if needed
    # Example: source .env.nu (from project directory)

    # Load providers environment settings...
    # use ../../providers/prov_lib/env_middleware.nu

    # Auto-load tera plugin if available for template rendering at env initialization
    # Call this in a block that runs AFTER the export-env completes
    if ( (version).installed_plugins | str contains "tera" ) {
        (plugin use tera)
    }
}

export def "show_env" [
] {
  let env_vars = {
    PROVISIONING: $env.PROVISIONING,
    PROVISIONING_CORE: $env.PROVISIONING_CORE,
    PROVISIONING_PROVIDERS_PATH: $env.PROVISIONING_PROVIDERS_PATH,
    PROVISIONING_TASKSERVS_PATH: $env.PROVISIONING_TASKSERVS_PATH,
    PROVISIONING_CLUSTERS_PATH: $env.PROVISIONING_CLUSTERS_PATH,
    PROVISIONING_RESOURCES: $env.PROVISIONING_RESOURCES,
    PROVISIONING_NOTIFY_ICON: $env.PROVISIONING_NOTIFY_ICON,

    PROVISIONING_DEBUG: $"($env.PROVISIONING_DEBUG)",
    PROVISIONING_METADATA: $"($env.PROVISIONING_METADATA)",

    PROVISIONING_DEBUG_CHECK: $"($env.PROVISIONING_DEBUG_CHECK)",
    PROVISIONING_DEBUG_REMOTE: $"($env.PROVISIONING_DEBUG_REMOTE)",
    PROVISIONING_LOG_LEVEL: $env.PROVISIONING_LOG_LEVEL,

    PROVISIONING_NO_TERMINAL: $env.PROVISIONING_NO_TERMINAL,
    PROVISIONING_ARGS: $env.PROVISIONING_ARGS,
    PROVISIONING_MODULE: $env.PROVISIONING_MODULE,
    PROVISIONING_NAME: $env.PROVISIONING_NAME,

    PROVISIONING_FILEVIEWER: $env.PROVISIONING_FILEVIEWER,

    NU_LOG_LEVEL: ($env.NU_LOG_LEVEL| default null),
    NU_LIB_DIRS: (if ($env.PROVISIONING_OUT | is-empty) { $env.NU_LIB_DIRS } else { $"($env.NU_LIB_DIRS | to json)"}),

    PROVISIONING_KLOUD_PATH: $env.PROVISIONING_KLOUD_PATH,

    PROVISIONING_DFLT_SET: $env.PROVISIONING_DFLT_SET,

    NOW: $env.NOW,
    PROVISIONING_MATCH_DATE: $env.PROVISIONING_MATCH_DATE,

    PROVISIONING_WK_FORMAT: $env.PROVISIONING_WK_FORMAT,

    PROVISIONING_REQ_VERSIONS: $env.PROVISIONING_REQ_VERSIONS,
    PROVISIONING_TOOLS_PATH: $env.PROVISIONING_TOOLS_PATH,
    PROVISIONING_TEMPLATES_PATH: $env.PROVISIONING_TEMPLATES_PATH,
    SSH_OPS: (if ($env.PROVISIONING_OUT | is-empty) { $env.SSH_OPS } else { $"($env.SSH_OPS | to json)"}),

    PROVISIONING_RUN_TASKSERVS_PATH: $env.PROVISIONING_RUN_TASKSERVS_PATH,
    PROVISIONING_RUN_CLUSTERS_PATH: $env.PROVISIONING_RUN_CLUSTERS_PATH,
    PROVISIONING_GENERATE_DIRPATH: $env.PROVISIONING_GENERATE_DIRPATH,
    PROVISIONING_GENERATE_DEFSFILE: $env.PROVISIONING_GENERATE_DEFSFILE,

    PROVISIONING_KEYS_PATH: $env.PROVISIONING_KEYS_PATH,

    PROVISIONING_USE_nickel: $"($env.PROVISIONING_USE_nickel)",
    PROVISIONING_J2_PARSER: ($env.PROVISIONING_J2_PARSER? | default ""),

    PROVISIONING_URL: $env.PROVISIONING_URL,

    PROVISIONING_USE_SOPS: $env.PROVISIONING_USE_SOPS,
    PROVISIONING_LAST_ERROR: $env.PROVISIONING_LAST_ERROR,

    CURRENT_KLOUD_PATH: ($env.CURRENT_INFRA_PATH? | default ""),

    PROVISIONING_SOPS: ($env.PROVISIONING_SOPS? | default ""),

    PROVISIONING_KAGE: ($env.PROVISIONING_KAGE? | default ""),

    PROVISIONING_OUT: $env.PROVISIONING_OUT,
  };

  if $env.PROVISIONING_KAGE? != null and ($env.PROVISIONING_KAGE | is-not-empty) {
    $env_vars | merge {
      SOPS_AGE_KEY_FILE: $env.SOPS_AGE_KEY_FILE,
      SOPS_AGE_RECIPIENTS: $env.SOPS_AGE_RECIPIENTS,
    }
  } else {
    $env_vars
  }
}

# Get CLI daemon URL for template rendering and other daemon operations
# Returns the daemon endpoint, checking environment variable first, then default
export def get-cli-daemon-url [] {
  $env.PROVISIONING_DAEMON_URL? | default "http://localhost:9091"
}
