# This is a Zsh config. If you see this in bash, switch to Zsh: exec zsh
if [ -n "$BASH_VERSION" ]; then
  echo "This is a Zsh config. Run: exec zsh"
  return 0 2>/dev/null || exit 0
fi

# ---------- powerlevel10k instant prompt (must stay near the top) ----------
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

# ---------- safety & history ----------
export ZSH_DISABLE_COMPFIX=true
setopt autocd autopushd pushdignoredups
setopt hist_ignore_all_dups hist_reduce_blanks inc_append_history share_history
setopt no_beep interactive_comments
export HISTSIZE=20000
export SAVEHIST=20000
export HISTFILE=$HOME/.zsh_history
bindkey -e

# ---------- helpers ----------
_has() { command -v "$1" >/dev/null 2>&1; }

# cache compinit's security check instead of paying for it on every launch
_compinit_cached() {
  autoload -Uz compinit
  local dump="${ZDOTDIR:-$HOME}/.zcompdump"
  if [[ -n ${dump}(#qN.mh+24) ]]; then
    compinit -d "$dump"
  else
    compinit -C -d "$dump"
  fi
}

# ---------- nvm ----------
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion"

# ---------- bun ----------
export BUN_INSTALL="$HOME/.bun"
[ -s "$BUN_INSTALL/_bun" ] && source "$BUN_INSTALL/_bun"

# ---------- PATH ----------
export PNPM_HOME="${PNPM_HOME:-$HOME/.local/share/pnpm}"
export PATH="$HOME/.local/bin:$HOME/bin:/opt/homebrew/bin:$PATH"
case ":$PATH:" in
  *":$PNPM_HOME/bin:"*) ;;
  *) export PATH="$PNPM_HOME/bin:$PATH" ;;
esac
case ":$PATH:" in
  *":$BUN_INSTALL/bin:"*) ;;
  *) export PATH="$BUN_INSTALL/bin:$PATH" ;;
esac

# ---------- zinit (plugin manager, auto-install) ----------
export ZINIT_HOME=${XDG_DATA_HOME:-$HOME/.local/share}/zinit/zinit.git
if [[ ! -d $ZINIT_HOME ]]; then
  mkdir -p ${ZINIT_HOME:h}
  git clone https://github.com/zdharma-continuum/zinit.git "$ZINIT_HOME"
fi
source "$ZINIT_HOME/zinit.zsh"

# ---------- prompt: powerlevel10k ----------
zinit ice depth=1
zinit light romkatv/powerlevel10k
POWERLEVEL9K_DISABLE_CONFIGURATION_WIZARD=true
POWERLEVEL9K_INSTANT_PROMPT=quiet
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(os_icon dir vcs)
POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(status command_execution_time time)
POWERLEVEL9K_PROMPT_ON_NEWLINE=true
POWERLEVEL9K_MULTILINE_FIRST_PROMPT_PREFIX="%242F╭─"
POWERLEVEL9K_MULTILINE_LAST_PROMPT_PREFIX="%242F╰▶ "
POWERLEVEL9K_TIME_FORMAT='%D{%H:%M}'
POWERLEVEL9K_DIR_SHOW_WRITABLE=v2
POWERLEVEL9K_VCS_BRANCH_ICON=' '
POWERLEVEL9K_MODE=nerdfont-complete

# ---------- completions & UI polish ----------
zstyle ':completion:*' menu select
zstyle ':completion:*' matcher-list '' 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=*' 'l:|=* r:|=*'
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}

# ---------- plugins ----------
# fzf-tab needs compinit to have already run, and needs to load before
# anything that wraps completion widgets (autosuggestions, syntax-highlighting).
zinit ice atinit'_compinit_cached'
zinit light Aloxaf/fzf-tab

zinit light zsh-users/zsh-autosuggestions

zinit light ajeetdsouza/zoxide
eval "$(zoxide init zsh)"

# fzf (uses ripgrep or fd if present, else find)
if _has rg; then
  export FZF_DEFAULT_COMMAND='rg --files --hidden --glob "!.git"'
elif _has fd; then
  export FZF_DEFAULT_COMMAND='fd --hidden --exclude .git'
else
  export FZF_DEFAULT_COMMAND='find . -path "*/.git" -prune -o -type f -print'
fi
export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"
export FZF_ALT_C_COMMAND="$FZF_DEFAULT_COMMAND"
zinit light junegunn/fzf

# syntax-highlighting must be loaded last, after any plugin that wraps widgets
zinit light zsh-users/zsh-syntax-highlighting

# ---------- aliases with smart fallbacks ----------
if _has eza; then alias ls='eza --icons=auto --group-directories-first'; else alias ls='ls --color=auto'; fi
alias ll='ls -la'
if _has bat; then
  alias cat='bat -pp'
elif _has batcat; then
  alias cat='batcat -pp'
fi
if _has rg; then alias grep='rg'; fi
if _has duf; then alias df='duf'; fi
if _has btm; then alias top='btm'; fi
alias gs='git status'
alias gl='git log --oneline --graph --decorate'
alias gp='git pull --rebase'
alias gc='git commit'
alias gco='git checkout'
alias c='clear'
phok() {
	cd "$HOME/Coding/jwtf/apps/phokus" || return
	if [ "$#" -gt 0 ]; then
		"$@"
	fi
}

jwtf() {
	cd "$HOME/Coding/jwtf" || return
	if [ "$#" -gt 0  ]; then
		"$@"
	fi
}
# This laptop's Intel iHD VA-API driver corrupts hardware-decoded video
# frames (dmabuf allocator size-mismatch bug), and WebKitGTK's DMA-BUF
# compositing path is separately broken on this GPU/driver combo too — force
# software H.264/H.265 decode and disable DMA-BUF rendering for Phokus dev
# builds so video playback is smooth.
alias phok-dev='WEBKIT_DISABLE_DMABUF_RENDERER=1 GST_PLUGIN_FEATURE_RANK="vah264dec:0,vah265dec:0,vaapih264dec:0,vaapih265dec:0" pnpm --dir "$HOME/Coding/jwtf/apps/phokus" dev:app:cpu'

# ---------- misc helpers ----------
mkcd(){ mkdir -p "$1" && cd "$1"; }
up(){
  local n="${1:-1}"
  [[ "$n" =~ ^[0-9]+$ ]] || { echo "up: expects a number"; return 1; }
  local target=""
  for ((i=0; i<n; i++)); do target="../$target"; done
  cd "$target" || return
}
extract(){
  case "$1" in
    *.tar.gz|*.tgz) tar xzf "$1";;
    *.tar.bz2|*.tbz2) tar xjf "$1";;
    *.tar.xz|*.txz) tar xJf "$1";;
    *.tar) tar xf "$1";;
    *.gz) gunzip "$1";;
    *.zip) unzip "$1";;
    *.7z) 7z x "$1";;
    *.rar) unrar x "$1";;
    *) echo "unknown format";;
  esac
}

