package config

import (
	"os"
	"strings"

	"gitlab.com/gitlab-org/cli/internal/dbg"
)

func ConfigKeyEquivalence(key string) string {
	return resolveAlias(key)
}

// When CI auto-login is on (GLAB_ENABLE_CI_AUTOLOGIN=true and
// GITLAB_CI=true), these vars REPLACE the schema's EnvVars for the key.
var ciAutologinEnvOverrides = map[string][]string{
	"api_host":     {"CI_SERVER_FQDN"},
	"subfolder":    {"GITLAB_SUBFOLDER", "CI_SERVER_URL"},
	"ssh_host":     {"GITLAB_SSH_HOST", "CI_SERVER_SHELL_SSH_HOST"},
	"api_protocol": {"CI_SERVER_PROTOCOL"},
	"host":         {"CI_SERVER_FQDN"},
	"job_token":    {"CI_JOB_TOKEN"},
	"ca_cert":      {"CI_SERVER_TLS_CA_FILE"},
	"client_cert":  {"CI_SERVER_TLS_CERT_FILE"},
	"client_key":   {"CI_SERVER_TLS_KEY_FILE"},
}

func EnvKeyEquivalence(key string) []string {
	canonical := resolveAlias(key)

	if os.Getenv("GLAB_ENABLE_CI_AUTOLOGIN") == "true" && os.Getenv("GITLAB_CI") == "true" {
		dbg.Debug("CI auto-login is enabled because GLAB_ENABLE_CI_AUTOLOGIN and GITLAB_CI are both true. This enables auto-login using GitLab's predefined CI/CD variables and potentially authenticates with the CI_JOB_TOKEN")
		if overrides, ok := ciAutologinEnvOverrides[canonical]; ok {
			return overrides
		}
	}

	if kd := findKeyDef(canonical); kd != nil && len(kd.EnvVars) > 0 {
		return kd.EnvVars
	}
	return []string{strings.ToUpper(canonical)}
}

// Only KeyDefs marked Fallback contribute a value here; the rest return
// "" so an absent key surfaces as "not set" rather than the seed default.
func defaultFor(key string) string {
	canonical := resolveAlias(key)
	if kd := findKeyDef(canonical); kd != nil && kd.Fallback {
		return kd.Default
	}
	return ""
}
