// Copyright 2025 Emiliano Spinella (eminwux)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

package config

import (
	"os"

	"github.com/spf13/viper"
)

type Var struct {
	Key        string // e.g. "KUKEON_RUN_PATH"
	ViperKey   string // optional, e.g. "global.runPath"
	CobraKey   string // optional, e.g. "run-path"
	Default    string // optional
	HasDefault bool
}

func DefineKV(envName, viperKey string, defaultVal ...string) Var {
	v := Var{Key: envName, ViperKey: viperKey}
	if len(defaultVal) > 0 {
		v.Default = defaultVal[0]
		v.HasDefault = true
		if viperKey != "" {
			viper.SetDefault(viperKey, defaultVal[0])
		}
	}
	return v
}

// DefineKVNoViperDefault is like DefineKV but does not register the default
// with viper.SetDefault. Use for keys whose downstream logic relies on
// viper.IsSet to distinguish "operator pinned this explicitly" from
// "fall through to derivation" — viper.SetDefault trips IsSet (viper v1.21)
// and would silently disable the derivation. Callers must fall back to
// .Default themselves via the `viper.GetString(...); if "" { = .Default }`
// pattern. Currently used by KUKEOND_SOCKET so that
// applyRunPathImpliesKukeondSocket can derive `<runPath>/kukeond.sock`
// when no env / flag / YAML pinned the key.
func DefineKVNoViperDefault(envName, viperKey, defaultVal string) Var {
	return Var{
		Key:        envName,
		ViperKey:   viperKey,
		Default:    defaultVal,
		HasDefault: true,
	}
}

func Define(envName string, defaultVal ...string) Var {
	return DefineKV(envName, "", defaultVal...)
}

func (v *Var) EnvKey() string               { return v.Key }
func (v *Var) EnvVar() string               { return v.Key }
func (v *Var) DefaultValue() (string, bool) { return v.Default, v.HasDefault }

// ValueOrDefault defines precedence: viper (if ViperKey set and value present) → OS env → default → "".
func (v *Var) ValueOrDefault() string {
	if v.ViperKey != "" && viper.IsSet(v.ViperKey) {
		return viper.GetString(v.ViperKey)
	}
	if val, ok := os.LookupEnv(v.Key); ok {
		return val
	}
	if v.HasDefault {
		return v.Default
	}
	return ""
}

// BindEnv is safe if ViperKey is empty: does nothing.
func (v *Var) BindEnv() error {
	if v.ViperKey == "" {
		return nil
	}
	return viper.BindEnv(v.ViperKey, v.Key)
}

func (v *Var) Set(value string) error {
	return os.Setenv(v.Key, value)
}

func (v *Var) SetDefault(val string) {
	v.Default = val
	v.HasDefault = true
	if v.ViperKey != "" {
		viper.SetDefault(v.ViperKey, val)
	}
}

func KV(v Var, value string) string { return v.Key + "=" + value }

// ---- Declare statically (Viper key optional per var) ----.
var (
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKEON_ROOT_VERBOSE = DefineKV("KUKEON_VERBOSE", "kukeon/verbose")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKEON_ROOT_RUN_PATH = DefineKV("KUKEON_RUN_PATH", "kukeon/runPath")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKEON_ROOT_LOG_LEVEL = DefineKV("KUKEON_LOG_LEVEL", "kukeon/logLevel", "info")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKEON_ROOT_CONTAINERD_SOCKET = DefineKV("KUKEON_CONTAINERD_SOCKET", "kukeon/containerd.socket")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKEON_ROOT_NAMESPACE_SUFFIX = DefineKV(
		"KUKEON_NAMESPACE_SUFFIX", "kukeon/namespaceSuffix", "kukeon.io",
	)
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKEON_ROOT_CGROUP_ROOT = DefineKV("KUKEON_CGROUP_ROOT", "kukeon/cgroupRoot", "/kukeon")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKEON_ROOT_POD_SUBNET_CIDR = DefineKV("KUKEON_POD_SUBNET_CIDR", "kukeon/podSubnetCIDR", "10.88.0.0/16")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKEON_ROOT_HOST = DefineKV("KUKEON_HOST", "kukeon/host", "unix:///run/kukeon/kukeond.sock")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKEON_ROOT_NO_DAEMON = DefineKV("KUKEON_NO_DAEMON", "kukeon/noDaemon", "false")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_CONFIGURATION = DefineKV("KUKE_CONFIGURATION", "kuke/configuration")

	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	// KUKEOND_SOCKET intentionally bypasses viper.SetDefault — see
	// DefineKVNoViperDefault and applyRunPathImpliesKukeondSocket.
	KUKEOND_SOCKET = DefineKVNoViperDefault(
		"KUKEOND_SOCKET", "kukeond/socket", "/run/kukeon/kukeond.sock",
	)
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKEOND_SOCKET_GID = DefineKV("KUKEOND_SOCKET_GID", "kukeond/socketGID", "0")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKEOND_CONFIGURATION = DefineKV(
		"KUKEOND_CONFIGURATION", "kukeond/configuration", "/etc/kukeon/kukeond.yaml",
	)
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKEOND_RECONCILE_INTERVAL = DefineKV(
		"KUKEOND_RECONCILE_INTERVAL", "kukeond/reconcileInterval", "30s",
	)
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKEOND_DEFAULT_MEMORY_LIMIT_BYTES = DefineKV(
		"KUKEOND_DEFAULT_MEMORY_LIMIT_BYTES", "kukeond/defaultMemoryLimitBytes", "0",
	)
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	// KUKEOND_KUKETTY_LOG_LEVEL is the daemon-wide default verbosity of the
	// kuketty wrapper's own slog output, applied when a cell omits the
	// per-container Tty.LogLevel knob. Issue #599.
	KUKEOND_KUKETTY_LOG_LEVEL = DefineKV(
		"KUKEOND_KUKETTY_LOG_LEVEL", "kukeond/kukettyLogLevel", "info",
	)
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	// KUKEOND_DISK_PRESSURE_WARN_PCT is the data-volume usage percentage above
	// which kukeond's reconcile loop emits a rate-limited WARN naming the realm
	// and current usage. Nothing is deleted. 0 (or negative) disables the
	// warning. Issue #1035.
	KUKEOND_DISK_PRESSURE_WARN_PCT = DefineKV(
		"KUKEOND_DISK_PRESSURE_WARN_PCT", "kukeond/diskPressureWarnPct", "85",
	)
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	// KUKEOND_DISK_PRESSURE_BLOCK_PCT is the data-volume usage percentage at or
	// above which kukeond refuses to provision a new cell (CreateCell fails
	// fast) so a snapshot does not push the volume to 100%. `kuke create cell`
	// / `kuke run --ignore-disk-pressure` bypasses the guard. 0 (or negative)
	// disables the guard. Issue #1035.
	KUKEOND_DISK_PRESSURE_BLOCK_PCT = DefineKV(
		"KUKEOND_DISK_PRESSURE_BLOCK_PCT", "kukeond/diskPressureBlockPct", "95",
	)

	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_INIT_REALM = DefineKV("KUKE_INIT_REALM", "kuke/init/realm")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_INIT_SPACE = DefineKV("KUKE_INIT_SPACE", "kuke/init/space")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_INIT_KUKEOND_IMAGE = DefineKV("KUKE_INIT_KUKEOND_IMAGE", "kuke/init/kukeondImage")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_INIT_NO_WAIT = DefineKV("KUKE_INIT_NO_WAIT", "kuke/init/noWait", "false")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_INIT_FORCE_REGENERATE_CNI = DefineKV(
		"KUKE_INIT_FORCE_REGENERATE_CNI", "kuke/init/forceRegenerateCNI", "false",
	)

	// Create command variables
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_CREATE_REALM_NAME = DefineKV("KUKE_CREATE_REALM_NAME", "kuke/create/realm/name")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_CREATE_SPACE_NAME = DefineKV("KUKE_CREATE_SPACE_NAME", "kuke/create/space/name")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_CREATE_SPACE_REALM = DefineKV("KUKE_CREATE_SPACE_REALM", "kuke/create/space/realm", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_CREATE_STACK_NAME = DefineKV("KUKE_CREATE_STACK_NAME", "kuke/create/stack/name")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_CREATE_STACK_REALM = DefineKV("KUKE_CREATE_STACK_REALM", "kuke/create/stack/realm", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_CREATE_STACK_SPACE = DefineKV("KUKE_CREATE_STACK_SPACE", "kuke/create/stack/space", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_CREATE_CELL_NAME = DefineKV("KUKE_CREATE_CELL_NAME", "kuke/create/cell/name")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_CREATE_CELL_REALM = DefineKV("KUKE_CREATE_CELL_REALM", "kuke/create/cell/realm", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_CREATE_CELL_SPACE = DefineKV("KUKE_CREATE_CELL_SPACE", "kuke/create/cell/space", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_CREATE_CELL_STACK = DefineKV("KUKE_CREATE_CELL_STACK", "kuke/create/cell/stack", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_CREATE_CELL_FROM_BLUEPRINT = DefineKV("KUKE_CREATE_CELL_FROM_BLUEPRINT", "kuke/create/cell/from-blueprint")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_CREATE_CELL_FROM_CONFIG = DefineKV("KUKE_CREATE_CELL_FROM_CONFIG", "kuke/create/cell/from-config")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	// KUKE_CREATE_CELL_CLONE is the env-var twin of `kuke create cell --clone
	// <src>` (epic:cell-identity #1073): the third source kind alongside
	// --from-blueprint / --from-config. Forks the named source cell's recipe
	// (its Spec.Provenance, with any per-cell --env/--param overrides) into a
	// new cell.
	KUKE_CREATE_CELL_CLONE = DefineKV("KUKE_CREATE_CELL_CLONE", "kuke/create/cell/clone")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_CREATE_CELL_PARAM_FILE = DefineKV("KUKE_CREATE_CELL_PARAM_FILE", "kuke/create/cell/param-file")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	// KUKE_CREATE_CELL_IGNORE_DISK_PRESSURE is the env-var twin of
	// `kuke create cell --ignore-disk-pressure` (issue #1035): bypasses
	// kukeond's data-volume disk-pressure creation guard for this invocation.
	KUKE_CREATE_CELL_IGNORE_DISK_PRESSURE = DefineKV(
		"KUKE_CREATE_CELL_IGNORE_DISK_PRESSURE", "kuke/create/cell/ignore-disk-pressure", "false",
	)
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_CREATE_CONFIG_NAME = DefineKV("KUKE_CREATE_CONFIG_NAME", "kuke/create/config/name")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_CREATE_CONFIG_REALM = DefineKV("KUKE_CREATE_CONFIG_REALM", "kuke/create/config/realm", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_CREATE_CONFIG_SPACE = DefineKV("KUKE_CREATE_CONFIG_SPACE", "kuke/create/config/space", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_CREATE_CONFIG_STACK = DefineKV("KUKE_CREATE_CONFIG_STACK", "kuke/create/config/stack", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_CREATE_CONFIG_BLUEPRINT = DefineKV("KUKE_CREATE_CONFIG_BLUEPRINT", "kuke/create/config/blueprint")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_CREATE_BLUEPRINT_NAME = DefineKV("KUKE_CREATE_BLUEPRINT_NAME", "kuke/create/blueprint/name")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_CREATE_BLUEPRINT_REALM = DefineKV("KUKE_CREATE_BLUEPRINT_REALM", "kuke/create/blueprint/realm", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_CREATE_BLUEPRINT_SPACE = DefineKV("KUKE_CREATE_BLUEPRINT_SPACE", "kuke/create/blueprint/space", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_CREATE_BLUEPRINT_STACK = DefineKV("KUKE_CREATE_BLUEPRINT_STACK", "kuke/create/blueprint/stack", "default")

	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_CREATE_SECRET_NAME = DefineKV("KUKE_CREATE_SECRET_NAME", "kuke/create/secret/name")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_CREATE_SECRET_REALM = DefineKV("KUKE_CREATE_SECRET_REALM", "kuke/create/secret/realm", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_CREATE_SECRET_SPACE = DefineKV("KUKE_CREATE_SECRET_SPACE", "kuke/create/secret/space")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_CREATE_SECRET_STACK = DefineKV("KUKE_CREATE_SECRET_STACK", "kuke/create/secret/stack")

	// Get command variables
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_GET_REALM_NAME = DefineKV("KUKE_GET_REALM_NAME", "kuke/get/realm/name")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_GET_SPACE_NAME = DefineKV("KUKE_GET_SPACE_NAME", "kuke/get/space/name")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_GET_SPACE_REALM = DefineKV("KUKE_GET_SPACE_REALM", "kuke/get/space/realm", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_GET_STACK_NAME = DefineKV("KUKE_GET_STACK_NAME", "kuke/get/stack/name")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_GET_STACK_REALM = DefineKV("KUKE_GET_STACK_REALM", "kuke/get/stack/realm", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_GET_STACK_SPACE = DefineKV("KUKE_GET_STACK_SPACE", "kuke/get/stack/space", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_GET_CELL_NAME = DefineKV("KUKE_GET_CELL_NAME", "kuke/get/cell/name")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_GET_CELL_REALM = DefineKV("KUKE_GET_CELL_REALM", "kuke/get/cell/realm", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_GET_CELL_SPACE = DefineKV("KUKE_GET_CELL_SPACE", "kuke/get/cell/space", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_GET_CELL_STACK = DefineKV("KUKE_GET_CELL_STACK", "kuke/get/cell/stack", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_GET_CONTAINER_NAME = DefineKV("KUKE_GET_CONTAINER_NAME", "kuke/get/container/name")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_GET_CONTAINER_REALM = DefineKV("KUKE_GET_CONTAINER_REALM", "kuke/get/container/realm", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_GET_CONTAINER_SPACE = DefineKV("KUKE_GET_CONTAINER_SPACE", "kuke/get/container/space", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_GET_CONTAINER_STACK = DefineKV("KUKE_GET_CONTAINER_STACK", "kuke/get/container/stack", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_GET_CONTAINER_CELL = DefineKV("KUKE_GET_CONTAINER_CELL", "kuke/get/container/cell")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_GET_SECRET_NAME = DefineKV("KUKE_GET_SECRET_NAME", "kuke/get/secret/name")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_GET_SECRET_REALM = DefineKV("KUKE_GET_SECRET_REALM", "kuke/get/secret/realm", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_GET_SECRET_SPACE = DefineKV("KUKE_GET_SECRET_SPACE", "kuke/get/secret/space")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_GET_SECRET_STACK = DefineKV("KUKE_GET_SECRET_STACK", "kuke/get/secret/stack")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_GET_SECRET_CELL = DefineKV("KUKE_GET_SECRET_CELL", "kuke/get/secret/cell")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_GET_BLUEPRINT_NAME = DefineKV("KUKE_GET_BLUEPRINT_NAME", "kuke/get/blueprint/name")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_GET_BLUEPRINT_REALM = DefineKV("KUKE_GET_BLUEPRINT_REALM", "kuke/get/blueprint/realm", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_GET_BLUEPRINT_SPACE = DefineKV("KUKE_GET_BLUEPRINT_SPACE", "kuke/get/blueprint/space", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_GET_BLUEPRINT_STACK = DefineKV("KUKE_GET_BLUEPRINT_STACK", "kuke/get/blueprint/stack", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_GET_CONFIG_NAME = DefineKV("KUKE_GET_CONFIG_NAME", "kuke/get/config/name")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_GET_CONFIG_REALM = DefineKV("KUKE_GET_CONFIG_REALM", "kuke/get/config/realm", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_GET_CONFIG_SPACE = DefineKV("KUKE_GET_CONFIG_SPACE", "kuke/get/config/space", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_GET_CONFIG_STACK = DefineKV("KUKE_GET_CONFIG_STACK", "kuke/get/config/stack", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_GET_OUTPUT = DefineKV("KUKE_GET_OUTPUT", "kuke/get/output")

	// Delete command variables
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_DELETE_REALM_NAME = DefineKV("KUKE_DELETE_REALM_NAME", "kuke/delete/realm/name")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_DELETE_SPACE_NAME = DefineKV("KUKE_DELETE_SPACE_NAME", "kuke/delete/space/name")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_DELETE_SPACE_REALM = DefineKV("KUKE_DELETE_SPACE_REALM", "kuke/delete/space/realm", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_DELETE_STACK_NAME = DefineKV("KUKE_DELETE_STACK_NAME", "kuke/delete/stack/name")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_DELETE_STACK_REALM = DefineKV("KUKE_DELETE_STACK_REALM", "kuke/delete/stack/realm", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_DELETE_STACK_SPACE = DefineKV("KUKE_DELETE_STACK_SPACE", "kuke/delete/stack/space", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_DELETE_CELL_NAME = DefineKV("KUKE_DELETE_CELL_NAME", "kuke/delete/cell/name")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_DELETE_CELL_REALM = DefineKV("KUKE_DELETE_CELL_REALM", "kuke/delete/cell/realm", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_DELETE_CELL_SPACE = DefineKV("KUKE_DELETE_CELL_SPACE", "kuke/delete/cell/space", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_DELETE_CELL_STACK = DefineKV("KUKE_DELETE_CELL_STACK", "kuke/delete/cell/stack", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_DELETE_SECRET_NAME = DefineKV("KUKE_DELETE_SECRET_NAME", "kuke/delete/secret/name")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_DELETE_SECRET_REALM = DefineKV("KUKE_DELETE_SECRET_REALM", "kuke/delete/secret/realm", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_DELETE_SECRET_SPACE = DefineKV("KUKE_DELETE_SECRET_SPACE", "kuke/delete/secret/space")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_DELETE_SECRET_STACK = DefineKV("KUKE_DELETE_SECRET_STACK", "kuke/delete/secret/stack")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_DELETE_SECRET_CELL = DefineKV("KUKE_DELETE_SECRET_CELL", "kuke/delete/secret/cell")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_DELETE_BLUEPRINT_NAME = DefineKV("KUKE_DELETE_BLUEPRINT_NAME", "kuke/delete/blueprint/name")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_DELETE_BLUEPRINT_REALM = DefineKV("KUKE_DELETE_BLUEPRINT_REALM", "kuke/delete/blueprint/realm", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_DELETE_BLUEPRINT_SPACE = DefineKV("KUKE_DELETE_BLUEPRINT_SPACE", "kuke/delete/blueprint/space")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_DELETE_BLUEPRINT_STACK = DefineKV("KUKE_DELETE_BLUEPRINT_STACK", "kuke/delete/blueprint/stack")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_DELETE_CONFIG_NAME = DefineKV("KUKE_DELETE_CONFIG_NAME", "kuke/delete/config/name")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_DELETE_CONFIG_REALM = DefineKV("KUKE_DELETE_CONFIG_REALM", "kuke/delete/config/realm", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_DELETE_CONFIG_SPACE = DefineKV("KUKE_DELETE_CONFIG_SPACE", "kuke/delete/config/space")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_DELETE_CONFIG_STACK = DefineKV("KUKE_DELETE_CONFIG_STACK", "kuke/delete/config/stack")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_DELETE_FORCE = DefineKV("KUKE_DELETE_FORCE", "kuke/delete/force")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_DELETE_CASCADE = DefineKV("KUKE_DELETE_CASCADE", "kuke/delete/cascade")

	// Purge command variables
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_PURGE_SPACE_REALM = DefineKV("KUKE_PURGE_SPACE_REALM", "kuke/purge/space/realm", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_PURGE_STACK_REALM = DefineKV("KUKE_PURGE_STACK_REALM", "kuke/purge/stack/realm", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_PURGE_STACK_SPACE = DefineKV("KUKE_PURGE_STACK_SPACE", "kuke/purge/stack/space", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_PURGE_CELL_REALM = DefineKV("KUKE_PURGE_CELL_REALM", "kuke/purge/cell/realm", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_PURGE_CELL_SPACE = DefineKV("KUKE_PURGE_CELL_SPACE", "kuke/purge/cell/space", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_PURGE_CELL_STACK = DefineKV("KUKE_PURGE_CELL_STACK", "kuke/purge/cell/stack", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_PURGE_FORCE = DefineKV("KUKE_PURGE_FORCE", "kuke/purge/force")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_PURGE_CASCADE = DefineKV("KUKE_PURGE_CASCADE", "kuke/purge/cascade")

	// Start command variables
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_START_CELL_REALM = DefineKV("KUKE_START_CELL_REALM", "kuke/start/cell/realm", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_START_CELL_SPACE = DefineKV("KUKE_START_CELL_SPACE", "kuke/start/cell/space", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_START_CELL_STACK = DefineKV("KUKE_START_CELL_STACK", "kuke/start/cell/stack", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_START_CONTAINER_REALM = DefineKV("KUKE_START_CONTAINER_REALM", "kuke/start/container/realm", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_START_CONTAINER_SPACE = DefineKV("KUKE_START_CONTAINER_SPACE", "kuke/start/container/space", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_START_CONTAINER_STACK = DefineKV("KUKE_START_CONTAINER_STACK", "kuke/start/container/stack", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_START_CONTAINER_CELL = DefineKV("KUKE_START_CONTAINER_CELL", "kuke/start/container/cell")

	// Run command variables.
	//nolint:revive,gochecknoglobals,staticcheck,godoclint // ignore linter warning about this variable
	KUKE_RUN_FILE = DefineKV("KUKE_RUN_FILE", "kuke/run/file")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_RUN_OUTPUT = DefineKV("KUKE_RUN_OUTPUT", "kuke/run/output")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_RUN_REALM = DefineKV("KUKE_RUN_REALM", "kuke/run/realm", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_RUN_SPACE = DefineKV("KUKE_RUN_SPACE", "kuke/run/space", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_RUN_STACK = DefineKV("KUKE_RUN_STACK", "kuke/run/stack", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_RUN_DETACH = DefineKV("KUKE_RUN_DETACH", "kuke/run/detach")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_RUN_CONTAINER = DefineKV("KUKE_RUN_CONTAINER", "kuke/run/container")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_RUN_RM = DefineKV("KUKE_RUN_RM", "kuke/run/rm")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_RUN_NAME = DefineKV("KUKE_RUN_NAME", "kuke/run/name")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	// KUKE_RUN_PARAM_FILE is the env-var twin of `kuke run --param-file`. The
	// flag is registered via cell.RegisterSourceFlags (shared with `kuke create
	// cell`) and read off cmd.Flags() rather than viper, so this Var is not bound;
	// it stays declared for env-driven defaults parity with the other keys.
	KUKE_RUN_PARAM_FILE = DefineKV("KUKE_RUN_PARAM_FILE", "kuke/run/param-file")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	// KUKE_RUN_ENV is the env-var twin of `kuke run --env KEY=VALUE` (issue
	// #834). The CLI reads --env via cobra's StringArray so multiple
	// occurrences merge; this Var exists for env-driven defaults parity with
	// the other KUKE_RUN_* keys but the flag itself is the operator-facing
	// surface.
	KUKE_RUN_ENV = DefineKV("KUKE_RUN_ENV", "kuke/run/env")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_RUN_REQUIRE_SYNCED = DefineKV("KUKE_RUN_REQUIRE_SYNCED", "kuke/run/require-synced")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	// KUKE_RUN_IGNORE_DISK_PRESSURE is the env-var twin of
	// `kuke run --ignore-disk-pressure` (issue #1035): bypasses kukeond's
	// data-volume disk-pressure creation guard for this invocation.
	KUKE_RUN_IGNORE_DISK_PRESSURE = DefineKV(
		"KUKE_RUN_IGNORE_DISK_PRESSURE", "kuke/run/ignore-disk-pressure", "false",
	)

	// Attach command variables
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_ATTACH_REALM = DefineKV("KUKE_ATTACH_REALM", "kuke/attach/realm", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_ATTACH_SPACE = DefineKV("KUKE_ATTACH_SPACE", "kuke/attach/space", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_ATTACH_STACK = DefineKV("KUKE_ATTACH_STACK", "kuke/attach/stack", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_ATTACH_CONTAINER = DefineKV("KUKE_ATTACH_CONTAINER", "kuke/attach/container")

	// Log command variables.

	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_LOG_REALM = DefineKV("KUKE_LOG_REALM", "kuke/log/realm", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_LOG_SPACE = DefineKV("KUKE_LOG_SPACE", "kuke/log/space", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_LOG_STACK = DefineKV("KUKE_LOG_STACK", "kuke/log/stack", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_LOG_CONTAINER = DefineKV("KUKE_LOG_CONTAINER", "kuke/log/container")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_LOG_FOLLOW = DefineKV("KUKE_LOG_FOLLOW", "kuke/log/follow", "false")

	// Restart command variables
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_RESTART_CELL_REALM = DefineKV("KUKE_RESTART_CELL_REALM", "kuke/restart/cell/realm", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_RESTART_CELL_SPACE = DefineKV("KUKE_RESTART_CELL_SPACE", "kuke/restart/cell/space", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_RESTART_CELL_STACK = DefineKV("KUKE_RESTART_CELL_STACK", "kuke/restart/cell/stack", "default")

	// Stop command variables
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_STOP_CELL_REALM = DefineKV("KUKE_STOP_CELL_REALM", "kuke/stop/cell/realm", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_STOP_CELL_SPACE = DefineKV("KUKE_STOP_CELL_SPACE", "kuke/stop/cell/space", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_STOP_CELL_STACK = DefineKV("KUKE_STOP_CELL_STACK", "kuke/stop/cell/stack", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_STOP_CONTAINER_REALM = DefineKV("KUKE_STOP_CONTAINER_REALM", "kuke/stop/container/realm", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_STOP_CONTAINER_SPACE = DefineKV("KUKE_STOP_CONTAINER_SPACE", "kuke/stop/container/space", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_STOP_CONTAINER_STACK = DefineKV("KUKE_STOP_CONTAINER_STACK", "kuke/stop/container/stack", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_STOP_CONTAINER_CELL = DefineKV("KUKE_STOP_CONTAINER_CELL", "kuke/stop/container/cell")

	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_UNINSTALL_YES = DefineKV("KUKE_UNINSTALL_YES", "kuke/uninstall/yes", "false")

	// Kill command variables
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_KILL_CELL_REALM = DefineKV("KUKE_KILL_CELL_REALM", "kuke/kill/cell/realm", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_KILL_CELL_SPACE = DefineKV("KUKE_KILL_CELL_SPACE", "kuke/kill/cell/space", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_KILL_CELL_STACK = DefineKV("KUKE_KILL_CELL_STACK", "kuke/kill/cell/stack", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_KILL_CONTAINER_REALM = DefineKV("KUKE_KILL_CONTAINER_REALM", "kuke/kill/container/realm", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_KILL_CONTAINER_SPACE = DefineKV("KUKE_KILL_CONTAINER_SPACE", "kuke/kill/container/space", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_KILL_CONTAINER_STACK = DefineKV("KUKE_KILL_CONTAINER_STACK", "kuke/kill/container/stack", "default")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_KILL_CONTAINER_CELL = DefineKV("KUKE_KILL_CONTAINER_CELL", "kuke/kill/container/cell")

	//nolint:revive,gochecknoglobals,staticcheck // daemon command variables
	KUKE_DAEMON_STOP_TIMEOUT = DefineKV("KUKE_DAEMON_STOP_TIMEOUT", "kuke/daemon/stop/timeout", "10s")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_DAEMON_RESTART_TIMEOUT = DefineKV("KUKE_DAEMON_RESTART_TIMEOUT", "kuke/daemon/restart/timeout", "10s")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_DAEMON_RESET_TIMEOUT = DefineKV("KUKE_DAEMON_RESET_TIMEOUT", "kuke/daemon/reset/timeout", "10s")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_DAEMON_RESET_PURGE_SYSTEM = DefineKV(
		"KUKE_DAEMON_RESET_PURGE_SYSTEM", "kuke/daemon/reset/purgeSystem", "false",
	)
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_DAEMON_LOGS_FOLLOW = DefineKV("KUKE_DAEMON_LOGS_FOLLOW", "kuke/daemon/logs/follow", "false")
	//nolint:revive,gochecknoglobals,staticcheck // daemon recreate command variables
	KUKE_DAEMON_RECREATE_TIMEOUT = DefineKV("KUKE_DAEMON_RECREATE_TIMEOUT", "kuke/daemon/recreate/timeout", "10s")
	//nolint:revive,gochecknoglobals,staticcheck // ignore linter warning about this variable
	KUKE_DAEMON_RECREATE_KUKEOND_IMAGE = DefineKV("KUKE_DAEMON_RECREATE_KUKEOND_IMAGE", "kuke/daemon/recreate/kukeondImage", "")
)
