# Powerlevel10k instant prompt. Keep near the top of ~/.zshrc.
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

# Environment
path=(~/.local/bin $path)
typeset -x XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"
typeset -g POWERLEVEL9K_INSTANT_PROMPT=quiet

# Shell options
setopt correct
setopt interactive_comments

# Load zinit and OMZ libs
load_omz_lib() {
  local i
  for i in theme-and-appearance.zsh git.zsh prompt_info_functions.zsh history.zsh; do
    zinit snippet "OMZL::${i}"
  done

  for i in completion.zsh key-bindings.zsh; do
    zinit ice lucid wait="1"
    zinit snippet "OMZL::${i}"
  done
}

load_zinit_compinit_function() {
  zpcompinit
  zpcdreplay
}

# Install zinit manually (official way) with GitHub proxy for China mainland
ZINIT_HOME="${XDG_DATA_HOME:-${HOME}/.local/share}/zinit/zinit.git"
[[ ! -d "${ZINIT_HOME}" ]] && mkdir -p "$(dirname "${ZINIT_HOME}")"
[[ ! -d "${ZINIT_HOME}/.git" ]] && git clone https://gh-proxy.org/https://github.com/zdharma-continuum/zinit.git "${ZINIT_HOME}"
source "${ZINIT_HOME}/zinit.zsh"
load_omz_lib

# Theme: Powerlevel10k
zinit ice depth=1
zinit light romkatv/powerlevel10k
skip_global_compinit=1
load_zinit_compinit_function

# Plugins (zinit official style)
# command-not-found: command missing 时提示可安装的软件包
if [[ -e /usr/lib/command-not-found ]]; then
  zinit ice lucid wait="0"
  zinit snippet OMZP::command-not-found
fi

# colored-man-pages: man 页面彩色高亮
zinit ice lucid wait="3"
zinit snippet OMZP::colored-man-pages

# fast-syntax-highlighting: 命令行语法高亮
zinit ice wait lucid atinit"ZINIT[COMPINIT_OPTS]=-C; zicompinit; zicdreplay"
zinit light zdharma-continuum/fast-syntax-highlighting

# zsh-autosuggestions: 基于历史记录的命令自动建议
zinit ice wait lucid atload"_zsh_autosuggest_start"
zinit light zsh-users/zsh-autosuggestions

# sudo: 双击 ESC 为当前命令加 sudo 前缀
zinit ice lucid wait="2"
zinit snippet OMZP::sudo

# z: 目录访问频率跳转
zinit ice lucid wait="1"
zinit snippet OMZP::z
unsetopt BG_NICE

# git: 提供 git 常用别名与函数
zinit ice lucid wait="1"
zinit snippet OMZP::git

# extract: 一条命令解压多种压缩格式，并加载其补全
zinit ice lucid wait="1"
zinit snippet OMZP::extract

# OMZ kubectl: kubectl 别名、补全与辅助函数
zinit ice lucid wait="1"
zinit snippet OMZP::kubectl

# Powerlevel10k config
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh

# Aliases
alias ...=../..
alias ....=../../..
alias .....=../../../..
alias ......=../../../../..
alias 1='cd -'
alias 2='cd -2'
alias 3='cd -3'
alias 4='cd -4'
alias 5='cd -5'
alias 6='cd -6'
alias 7='cd -7'
alias 8='cd -8'
alias 9='cd -9'
alias _='sudo '
alias afind='ack -il'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias gc1='git clone --recursive --depth=1'
alias globurl='noglob urlglobber '
alias grep='grep --color=auto'
alias md='mkdir -p'
alias rd='rmdir'

if [[ -n "$(command -v eza)" ]]; then
  DISABLE_LS_COLORS=true
  alias ls='eza --color=auto'
  alias l='eza -lbah --icons=auto'
  alias la='eza -labgh --icons=auto'
  alias ll='eza -lbg --icons=auto'
  alias lt='eza -lTbg --icons=auto'
  alias lsa='eza -lbagR --icons=auto'
  alias lst='eza -lTabgh --icons=auto'
else
  alias ls='ls --color=auto'
  alias l='ls -lah'
  alias la='ls -lAh'
  alias ll='ls -lh'
  alias lsa='ls -lah'
  alias lst='tree -pCsh'
fi

# yazi: open and jump to last cwd
function y() {
  local tmp cwd
  tmp="$(mktemp -t yazi-cwd.XXXXXX)"
  yazi "$@" --cwd-file="$tmp"
  cwd="$(cat -- "$tmp")"
  if [[ -n "$cwd" && "$cwd" != "$PWD" ]]; then
    builtin cd -- "$cwd"
  fi
  rm -f -- "$tmp"
}

# uv: Python package manager（自动补全）
if (( $+commands[uv] )); then
  eval "$(uv generate-shell-completion zsh)"
fi

# 快速开关代理（添加到 ~/.bashrc）
alias proxy-on='export http_proxy=http://127.0.0.1:7890 https_proxy=http://127.0.0.1:7890'
alias proxy-off='unset http_proxy https_proxy'

# Required packages
# - zsh
# - git
# - zinit
# - powerlevel10k
# - eza (optional, for modern ls aliases)
# - tree (optional, fallback for lst alias)
# - ack (optional, for afind alias)
# - yazi (terminal file manager)
# - uv (Python package manager)
# - kubectl (for OMZ kubectl plugin)
# - command-not-found (optional, distro package name may vary)
# - bat or batcat (optional, if you later add bat aliases)
