# Guard against re-sourcing (prevents slowdown on multiple sources)
# But allow aliases and exports to be reloaded when manually sourced
if [[ -n "$ZSHRC_LOADED" ]]; then
  # If manually sourcing (not initial shell startup), just reload configs
  [ -f ~/.aliases ] && source ~/.aliases
  [ -f ~/.exports ] && source ~/.exports
  return 0
fi
# Don't export - each shell instance should load fresh
ZSHRC_LOADED=1

# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
  source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh
# source ~/powerlevel10k/powerlevel10k.zsh-theme
# Instant prompt = Quiet
POWERLEVEL9K_INSTANT_PROMPT=quiet

if [[ -f "/opt/homebrew/bin/brew" ]]; then
  # If you're using macOS, you'll want this enabled
  eval "$(/opt/homebrew/bin/brew shellenv)"
fi

# Kill conflicting alias if it exists
unalias z 2>/dev/null
unalias zi 2>/dev/null

# Clone zcomet if necessary
if [[ ! -f ${ZDOTDIR:-${HOME}}/.zcomet/bin/zcomet.zsh ]]; then
  command git clone https://github.com/agkozak/zcomet.git ${ZDOTDIR:-${HOME}}/.zcomet/bin
fi

source ${ZDOTDIR:-${HOME}}/.zcomet/bin/zcomet.zsh

# Load a code snippet - no need to download an entire repository
zcomet snippet https://github.com/jreese/zsh-titles/blob/master/titles.plugin.zsh

# Lazy-load Prezto's archive module without downloading all of Prezto's
# submodules
zcomet trigger --no-submodules archive unarchive lsarchive \
    sorin-ionescu/prezto modules/archive

# Run compinit and compile its cache
zcomet compinit
# Load completions
# autoload -Uz compinit && compinit

# Themes and UI
zcomet load romkatv/powerlevel10k

# Completions and plugins
zcomet load zsh-users/zsh-syntax-highlighting
zcomet load zsh-users/zsh-completions
zcomet load zsh-users/zsh-autosuggestions
zcomet load Aloxaf/fzf-tab

# Oh My Zsh snippets
# zcomet load ohmyzsh/ohmyzsh lib/git.zsh
# zcomet load ohmyzsh/ohmyzsh plugins/git
# zcomet load ohmyzsh/ohmyzsh plugins/sudo
# zcomet load ohmyzsh/ohmyzsh plugins/archlinux
# zcomet load ohmyzsh/ohmyzsh plugins/aws
# zcomet load ohmyzsh/ohmyzsh plugins/kubectl
# zcomet load ohmyzsh/ohmyzsh plugins/kubectx
# zcomet load ohmyzsh/ohmyzsh plugins/command-not-found

# Keybindings
bindkey -e
bindkey '^p' history-search-backward
bindkey '^n' history-search-forward
bindkey '^[w' kill-region
bindkey "^[[1;5C" forward-word   # Ctrl+Right
bindkey "^[[1;5D" backward-word  # Ctrl+Left

# History
HISTSIZE=99999
HISTFILE=~/.zsh_history
SAVEHIST=$HISTSIZE
HISTDUP=erase
setopt appendhistory
# setopt sharehistory
setopt hist_ignore_space
setopt hist_ignore_all_dups
setopt hist_save_no_dups
setopt hist_ignore_dups
setopt hist_find_no_dups

# Completion styling
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Za-z}'
zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"
zstyle ':completion:*' menu no
# Enhanced fzf-tab completions with better previews
zstyle ':fzf-tab:complete:cd:*' fzf-preview 'eza -la --color=always $realpath 2>/dev/null || ls --color=always $realpath'
zstyle ':fzf-tab:complete:__zoxide_z:*' fzf-preview 'eza -la --color=always $realpath 2>/dev/null || ls --color=always $realpath'

# Preview for other common commands
zstyle ':fzf-tab:complete:cat:*' fzf-preview 'bat --color=always $realpath 2>/dev/null || cat $realpath'
zstyle ':fzf-tab:complete:bat:*' fzf-preview 'bat --color=always $realpath'
zstyle ':fzf-tab:complete:less:*' fzf-preview 'bat --color=always $realpath 2>/dev/null || cat $realpath'
zstyle ':fzf-tab:complete:more:*' fzf-preview 'bat --color=always $realpath 2>/dev/null || cat $realpath'

# Shell integrations
# eval "$(fzf --zsh)"
[ -f ~/.fzf.zsh ] && source ~/.fzf.zsh
eval "$(zoxide init zsh)"

# Atuin setup - smart history search
eval "$(atuin init zsh)"

# Sourcing
source $HOME/.cargo/env
[ -f ~/.aliases ] && source ~/.aliases
[ -f ~/.exports ] && source ~/.exports
# source /usr/share/doc/pkgfile/command-not-found.zsh
# source /usr/lib/command-not-found

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
# __conda_setup="$('/home/e/miniconda3/bin/conda' 'shell.zsh' 'hook' 2> /dev/null)"
# if [ $? -eq 0 ]; then
#     eval "$__conda_setup"
# else
#     if [ -f "/home/e/miniconda3/etc/profile.d/conda.sh" ]; then
#         . "/home/e/miniconda3/etc/profile.d/conda.sh"
#     else
#         export PATH="/home/e/miniconda3/bin:$PATH"
#     fi
# fi
# unset __conda_setup
# <<< conda initialize <<<


# NVM Lazy Loading (saves ~400ms on shell startup)
# Instead of loading NVM immediately, we create placeholder functions
# that load NVM only when first used
export NVM_DIR="$HOME/.nvm"

# Add NVM's default Node to PATH manually (fast)
if [ -d "$NVM_DIR/versions/node" ]; then
  # Get the default/current Node version
  NVM_DEFAULT_NODE=$(find "$NVM_DIR/versions/node" -maxdepth 1 -type d | sort -V | tail -1)
  if [ -n "$NVM_DEFAULT_NODE" ]; then
    export PATH="$NVM_DEFAULT_NODE/bin:$PATH"
  fi
fi

# Lazy load function - loads NVM on first use
lazy_load_nvm() {
  unset -f nvm node npm npx
  [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
  [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
}

# Create placeholder functions that trigger lazy loading
nvm() {
  lazy_load_nvm
  nvm "$@"
}

node() {
  # If node is in PATH (from our manual add above), use it directly
  if command -v node >/dev/null 2>&1; then
    command node "$@"
  else
    lazy_load_nvm
    node "$@"
  fi
}

npm() {
  if command -v npm >/dev/null 2>&1; then
    command npm "$@"
  else
    lazy_load_nvm
    npm "$@"
  fi
}

npx() {
  if command -v npx >/dev/null 2>&1; then
    command npx "$@"
  else
    lazy_load_nvm
    npx "$@"
  fi
}


# Generated for pdtm. Do not edit.
export PATH=$PATH:/home/e/.pdtm/go/bin


# opencode
export PATH=/home/e/.opencode/bin:$PATH

# bun completions
[ -s "/home/e/.bun/_bun" ] && source "/home/e/.bun/_bun"

# bun
export BUN_INSTALL="$HOME/.bun"
export PATH="$BUN_INSTALL/bin:$PATH"
# Removed redundant .local/bin addition (already in ~/.exports)

# SSH Agent for persistent tunnel (Baserow, etc.)
export SSH_AUTH_SOCK=/run/user/1000/ssh-agent.socket

# SSH keys - auto-add if agent is running but keys not loaded
# Load main key (check by fingerprint, not email - OPSEC)
ssh-add -l 2>/dev/null | grep -q "Tq/46I0GfFmme7/tyb/BFhj" || {
  ssh-add ~/.ssh/id_ed25519 2>/dev/null || echo "🔑 Run: ssh-add ~/.ssh/id_ed25519"
}
# Load additional keys from local config (not committed - contains infra-specific paths)
[[ -f ~/.ssh/autoload ]] && source ~/.ssh/autoload

# Deduplicate PATH at the end (after all PATH modifications)
export PATH=$(echo "$PATH" | tr ':' '\n' | awk '!seen[$0]++' | paste -sd:)

# Make PATH available to GUI applications (Ulauncher, etc.)
# This ensures apps launched outside terminal sessions can find bun, custom bins, etc.
systemctl --user import-environment PATH

[ -f ~/.env ] && source ~/.env

# Port reference quick access
alias ports='cat ~/.claude/context/infrastructure/ports-reference.md | less'
