# ABOUTME: Moonarch default zsh configuration with Catppuccin-themed prompt.
# ABOUTME: Sources user overrides from ~/.zshrc.d/ and ~/.zshrc.local

# --- History ---
HISTFILE=~/.histfile
HISTSIZE=1000
SAVEHIST=1000
setopt autocd nomatch notify appendhistory sharehistory hist_ignore_space hist_ignore_all_dups hist_save_no_dups hist_find_no_dups
unsetopt beep extendedglob
bindkey -e

# --- Completion ---
zstyle :compinstall filename "$HOME/.zshrc"
autoload -Uz compinit
compinit

# --- Window title ---
autoload -Uz add-zsh-hook

_precmd_title() {
    print -Pn "\e]0;%~\a"
}

_preexec_title() {
    print -Pn "\e]0;$2\a"
}

add-zsh-hook precmd _precmd_title
add-zsh-hook preexec _preexec_title

# --- Prompt (Catppuccin Mocha) ---
parse_git_branch() {
    local branch=""
    branch=$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
    local git_status=$(git status --porcelain 2>/dev/null)
    local color=green
    if echo "$git_status" | grep -q "^ M"; then
        color=yellow
        branch="${branch}*"
    fi
    if echo "$git_status" | grep -qE "^ A|^\?\?"; then
        color=yellow
        branch="${branch}+"
    fi
    if echo "$git_status" | grep -q "^ D"; then
        color=yellow
        branch="${branch}-"
    fi
    if [[ -n "$branch" ]]; then
        branch=[%F{${color}}${branch}%F{reset}]
    fi
    echo " $branch"
}

precmd() {
    print ""
    print -rP "%F{#b4befe}%B%n@%M%b %2~%F{#f5e0dc}$(parse_git_branch)%f"
}

PROMPT="%B%F{#b4befe}$%f%b "
RPROMPT="%F{241}%B%T%b%f"

# --- PATH ---
if [ -d "$HOME/.local/bin" ]; then
    PATH="$HOME/.local/bin:$PATH"
fi

# --- Key bindings ---
bindkey "^[[3~"   delete-char
bindkey "^[[H"    beginning-of-line
bindkey "^[[F"    end-of-line
bindkey "^[[1;5C" forward-word
bindkey "^[[1;5D" backward-word

# --- Aliases ---
alias ssh="TERM=xterm-256color ssh"
alias orphans='[[ -n $(pacman -Qdt) ]] && sudo pacman -Rs $(pacman -Qdtq) || echo "no orphans to remove"'
alias ls='eza --icons --color=always --group-directories-first'
alias ll='eza -lF --icons --color=always --group-directories-first'
alias la='eza -a --icons --color=always --group-directories-first'
alias l='eza -F --icons --color=always --group-directories-first'
alias vim='nvim'
alias uninstall='sudo pacman -Rsn'

# --- Extract archives ---
function extract() {
    if [ -f "$1" ]; then
        case "$1" in
            *.tar.bz2)   tar xjvf "$1"   ;;
            *.tar.gz)    tar xzvf "$1"   ;;
            *.tar.xz)    tar xvf "$1"    ;;
            *.bz2)       bzip2 -d "$1"   ;;
            *.rar)       unrar2dir "$1"  ;;
            *.gz)        gunzip "$1"     ;;
            *.tar)       tar xf "$1"     ;;
            *.tbz2)      tar xjf "$1"    ;;
            *.tgz)       tar xzf "$1"    ;;
            *.zip)       unzip2dir "$1"  ;;
            *.Z)         uncompress "$1" ;;
            *.7z)        7z x "$1"       ;;
            *.ace)       unace x "$1"    ;;
            *)           echo "'$1' cannot be extracted via extract()" ;;
        esac
    else
        echo "'$1' is not a valid file"
    fi
}

# --- FZF ---
if command -v fzf &>/dev/null; then
    eval "$(fzf --zsh)"
    export FZF_DEFAULT_COMMAND="fd --hidden --strip-cwd-prefix --exclude .git"
    export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"
    export FZF_ALT_C_COMMAND="fd --type=d --hidden --strip-cwd-prefix --exclude .git"
    export FZF_DEFAULT_OPTS="--height 50% --layout=default --border --color=hl:#2dd4bf"
    export FZF_CTRL_T_OPTS="--preview 'bat --color=always -n --line-range :500 {}'"
    export FZF_ALT_C_OPTS="--preview 'eza --tree --color=always {} | head -200'"
fi

# --- Plugins (system packages) ---
if [[ -f /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh ]]; then
    source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
fi
if [[ -f /usr/share/zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh ]]; then
    source /usr/share/zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh
    ZSH_AUTOSUGGEST_STRATEGY=(history completion)
fi

# --- Wayland environment ---
export TERMINAL="footclient"
export GDK_BACKEND="wayland,x11,*"
export QT_QPA_PLATFORM="wayland;xcb"
export QT_QPA_PLATFORMTHEME="qt6ct"
export QT_AUTO_SCREEN_SCALE_FACTOR="1"
export QT_WAYLAND_DISABLE_WINDOWDECORATION="1"
export SDL_VIDEODRIVER="wayland"
export CLUTTER_BACKEND="wayland"
export XDG_CURRENT_DESKTOP="niri"
export XDG_SESSION_DESKTOP="niri"
export XDG_SESSION_TYPE="wayland"
export EDITOR="nvim"
export SUDO_EDITOR="nvim"
export MOZ_ENABLE_WAYLAND="1"

# --- User override scripts ---
# Drop custom config snippets into ~/.zshrc.d/*.zsh
if [[ -d "$HOME/.zshrc.d" ]]; then
    for f in "$HOME/.zshrc.d"/*.zsh(N); do
        source "$f"
    done
fi

# Single-file user override (for simple additions)
[[ -f "$HOME/.zshrc.local" ]] && source "$HOME/.zshrc.local"
