# .zshrc

# Zsh Configuration
export ZSH="$HOME/.oh-my-zsh"
export ZSH_CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/oh-my-zsh"
export ZSH_COMPDUMP="${ZSH_CACHE_DIR}/.zcompdump-${HOST}-${ZSH_VERSION}"

mkdir -p "$ZSH_CACHE_DIR"
mkdir -p "$ZSH_CACHE_DIR/completions"

export DOTZSH_GIT_PLUGIN="$ZSH/plugins/git/git.plugin.zsh"
export DOTZSH_SUDO_PLUGIN="$ZSH/plugins/sudo/sudo.plugin.zsh"
export DOTZSH_RCLONE_PLUGIN="$ZSH/plugins/rclone/rclone.plugin.zsh"
export DOTZSH_AUTOSUGGESTIONS_PLUGIN="$ZSH/custom/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh"
export DOTZSH_SYNTAX_HIGHLIGHTING_PLUGIN="$ZSH/custom/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.plugin.zsh"
export DOTZSH_HISTORY_SUBSTRING_SEARCH_PLUGIN="$ZSH/plugins/history-substring-search/history-substring-search.plugin.zsh"

# Programming Languages Root
export PROG_DIR="$HOME/.programming"

# History
export HISTFILE="$HOME/.zsh_history"
export HISTSIZE=10000
export SAVEHIST=10000

setopt APPEND_HISTORY
setopt SHARE_HISTORY
setopt EXTENDED_HISTORY
setopt HIST_EXPIRE_DUPS_FIRST
setopt HIST_IGNORE_ALL_DUPS
setopt HIST_IGNORE_SPACE
setopt HIST_FIND_NO_DUPS
setopt HIST_REDUCE_BLANKS
setopt HIST_SAVE_NO_DUPS

typeset -gA DOTZSH_MISSING_TOOL_WARNED

dotzsh_warn_missing_tool_once() {
    local tool_key="$1"
    local tool_label="$2"
    local setup_hint="$3"

    if [[ -n "${DOTZSH_MISSING_TOOL_WARNED[$tool_key]:-}" ]]; then
        return 0
    fi

    DOTZSH_MISSING_TOOL_WARNED[$tool_key]=1

    print -u2 -- "$tool_label is not installed on this system."
    print -u2 -- "Run: $setup_hint"
    print -u2 -- "Or:  just setup all"
}

# Go and GVM (Black Box)
export GOPATH="$PROG_DIR/go"
export GVM_ROOT="$GOPATH"

gvm_load() {
    local requested_command="$1"

    unset -f gvm go gofmt

    if [[ ! -s "$GVM_ROOT/scripts/gvm" ]]; then
        dotzsh_warn_missing_tool_once "go" "Go / GVM" "just lang go"
        return 127
    fi

    source "$GVM_ROOT/scripts/gvm"

    if ! command -v "$requested_command" >/dev/null 2>&1; then
        dotzsh_warn_missing_tool_once "go" "Go / GVM" "just lang go"
        return 127
    fi

    "$@"
}

gvm() { gvm_load gvm "$@"; }
go() { gvm_load go "$@"; }
gofmt() { gvm_load gofmt "$@"; }

# Node and NVM (Lazy Load)
export NVM_DIR="$PROG_DIR/node"

nvm_load() {
    local requested_command="$1"

    unset -f nvm node npm npx

    if [[ ! -s "$NVM_DIR/nvm.sh" ]]; then
        dotzsh_warn_missing_tool_once "node" "Node / NVM" "just lang node"
        return 127
    fi

    . "$NVM_DIR/nvm.sh"

    if ! command -v "$requested_command" >/dev/null 2>&1; then
        dotzsh_warn_missing_tool_once "node" "Node / NVM" "just lang node"
        return 127
    fi

    "$@"
}

nvm() { nvm_load "nvm" "$@"; }
node() { nvm_load "node" "$@"; }
npm() { nvm_load "npm" "$@"; }
npx() { nvm_load "npx" "$@"; }

# Rust and Cargo
export RUSTUP_HOME="$PROG_DIR/rust/multirust"
export CARGO_HOME="$PROG_DIR/rust/cargo"

# Python (Pyenv + Miniconda)
export PYENV_ROOT="$PROG_DIR/python/pyenv"
export PATH="$PYENV_ROOT/bin:$PYENV_ROOT/shims:$PATH"
export CONDA_ROOT="$PYENV_ROOT/versions/miniforge3-latest"

pyenv_load() {
    unset -f pyenv

    if ! command -v pyenv >/dev/null 2>&1; then
        dotzsh_warn_missing_tool_once "python" "Pyenv / Python" "just lang python"
        return 127
    fi

    eval "$(command pyenv init -)"

    if ! command -v pyenv >/dev/null 2>&1; then
        dotzsh_warn_missing_tool_once "python" "Pyenv / Python" "just lang python"
        return 127
    fi

    pyenv "$@"
}

pyenv() { pyenv_load "$@"; }

conda_load() {
    local requested_command="$1"

    unset -f conda mamba

    if [[ ! -f "$CONDA_ROOT/etc/profile.d/conda.sh" ]]; then
        dotzsh_warn_missing_tool_once "python" "Conda / Mamba" "just lang python"
        return 127
    fi

    source "$CONDA_ROOT/etc/profile.d/conda.sh"

    if ! command -v "$requested_command" >/dev/null 2>&1; then
        dotzsh_warn_missing_tool_once "python" "Conda / Mamba" "just lang python"
        return 127
    fi

    "$@"
}

conda() { conda_load conda "$@"; }
mamba() { conda_load mamba "$@"; }

# R and Rig
export RIG_HOME="$PROG_DIR/r"
if [ -d "$RIG_HOME/bin" ]; then
    export PATH="$RIG_HOME/bin:$PATH"
fi

if [ -n "${WSL_DISTRO_NAME:-}" ] || [ -n "${WSL_INTEROP:-}" ]; then
    typeset -gr DOTZSH_IS_WSL=1
elif [ -r /proc/sys/kernel/osrelease ] && grep -qiE '(microsoft|wsl)' /proc/sys/kernel/osrelease 2>/dev/null; then
    typeset -gr DOTZSH_IS_WSL=1
else
    typeset -gr DOTZSH_IS_WSL=0
fi

# Zoxide
if command -v zoxide >/dev/null 2>&1; then
    eval "$(zoxide init --cmd cd zsh)"
fi

# Source Aliases & Secrets
[ -f ~/.zsh_aliases ] && source ~/.zsh_aliases
[ -f ~/.zsh_secrets ] && source ~/.zsh_secrets

# Add to PATH
export PATH="$HOME/.local/bin:$CARGO_HOME/bin:$GOPATH/bin:$PATH"

# opencode
if [ "$DOTZSH_IS_WSL" -eq 1 ] && command -v openchamber >/dev/null 2>&1 && ! pgrep -f "openchamber.*7891" >/dev/null 2>&1; then
    openchamber --port 7891 >/dev/null 2>&1 &!
fi

# direnv
if command -v direnv >/dev/null 2>&1; then
    eval "$(direnv hook zsh)"
fi

# Completion Styling
[ -f ~/.zsh_completion ] && source ~/.zsh_completion

# Plugin Scripts (lighter than loading full Oh My Zsh)
[ -f "$DOTZSH_GIT_PLUGIN" ] && source "$DOTZSH_GIT_PLUGIN"
[ -f "$DOTZSH_SUDO_PLUGIN" ] && source "$DOTZSH_SUDO_PLUGIN"
[ -f "$DOTZSH_RCLONE_PLUGIN" ] && source "$DOTZSH_RCLONE_PLUGIN"

# Prompt Styling
[ -f ~/.zsh_prompt ] && source ~/.zsh_prompt

# Interactive Enhancements
[ -f "$DOTZSH_HISTORY_SUBSTRING_SEARCH_PLUGIN" ] && source "$DOTZSH_HISTORY_SUBSTRING_SEARCH_PLUGIN"
[ -f "$DOTZSH_AUTOSUGGESTIONS_PLUGIN" ] && source "$DOTZSH_AUTOSUGGESTIONS_PLUGIN"
[ -f "$DOTZSH_SYNTAX_HIGHLIGHTING_PLUGIN" ] && source "$DOTZSH_SYNTAX_HIGHLIGHTING_PLUGIN"

export HISTORY_SUBSTRING_SEARCH_PREFIXED=1

# History keybindings
bindkey '^[[A' history-substring-search-up
bindkey '^[OA' history-substring-search-up
bindkey '^[[B' history-substring-search-down
bindkey '^[OB' history-substring-search-down

# bun
export BUN_INSTALL="$PROG_DIR/node/bun"
case ":$PATH:" in
  *":$BUN_INSTALL/bin:"*) ;;
  *) export PATH="$BUN_INSTALL/bin:$PATH" ;;
esac

# pnpm
export PNPM_HOME="$PROG_DIR/node/pnpm"
case ":$PATH:" in
  *":$PNPM_HOME:"*) ;;
  *) export PATH="$PNPM_HOME:$PATH" ;;
esac
# pnpm end
