# Path to your Oh My Zsh installation.
export ZSH="$HOME/.oh-my-zsh"

# History configuration
HISTFILE=$HOME/.zsh_history
HISTSIZE=10000
SAVEHIST=10000
setopt APPEND_HISTORY          # Append to history file, don't overwrite
setopt SHARE_HISTORY           # Share history across all sessions
setopt HIST_IGNORE_DUPS        # Don't record duplicate consecutive commands
setopt HIST_IGNORE_ALL_DUPS    # Delete old duplicate entries
setopt HIST_FIND_NO_DUPS       # Don't show duplicates when searching
setopt HIST_IGNORE_SPACE       # Don't record commands starting with space
setopt HIST_SAVE_NO_DUPS       # Don't save duplicates to history file
setopt HIST_REDUCE_BLANKS      # Remove extra blanks from commands
setopt INC_APPEND_HISTORY      # Write to history file immediately, not on shell exit

# Homebrew Setup
if [[ "$(uname)" == "Darwin" ]]; then
    eval "$(/opt/homebrew/bin/brew shellenv)" 2>/dev/null || eval "$(/usr/local/bin/brew shellenv)" 2>/dev/null
    BREW_PREFIX=$(brew --prefix)
elif [[ "$(uname)" == "Linux" ]]; then
    [ -d "/home/linuxbrew/.linuxbrew" ] && eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
    BREW_PREFIX="/home/linuxbrew/.linuxbrew"
fi

# Zsh Completions (must be set before sourcing Oh My Zsh)
if [ -d "$BREW_PREFIX/share/zsh-completions" ]; then
    FPATH="$BREW_PREFIX/share/zsh-completions:$FPATH"
fi

# Set name of the theme to load
ZSH_THEME="" # Using Starship instead


# Plugin configuration
plugins=(
    brew
    globalias
    git
    git-commit
    direnv
    nvm
    pyenv
    zoxide
    fzf
    history-substring-search
    colored-man-pages
    sudo
    gh
    golang
    extract
    command-not-found
)

# Only use tmux if not on macOS
if [[ "$(uname)" != "Darwin" ]]; then
    plugins+=(tmux)
    ZSH_TMUX_AUTOSTART=true
    ZSH_TMUX_AUTOCONNECT=true
    ZSH_TMUX_AUTOSTART_ONCE=true
    ZSH_TMUX_ITERM2=true
fi
ZOXIDE_CMD_OVERRIDE=cd
GLOBALIAS_FILTER_VALUES=(ls cat find grep top htop diff cd regen_protos cherrypick deploy_page deploy_service setup_venv claude_local claude_devbox)
DISABLE_LS_COLORS="true"

source $ZSH/oh-my-zsh.sh

# Brew-installed plugins (source manually since they aren't in $ZSH/plugins)
if [ -f "$BREW_PREFIX/share/zsh-autosuggestions/zsh-autosuggestions.zsh" ]; then
    source "$BREW_PREFIX/share/zsh-autosuggestions/zsh-autosuggestions.zsh"
fi
if [ -f "$BREW_PREFIX/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" ]; then
    source "$BREW_PREFIX/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh"
fi

# History substring search bindings (functions provided by history-substring-search plugin)
bindkey '^[[A' history-substring-search-up
bindkey '^[[B' history-substring-search-down
bindkey '^[OA' history-substring-search-up
bindkey '^[OB' history-substring-search-down

# Starship initialization
eval "$(starship init zsh)"

# --- User Settings ---
# Preferred editor for local and remote sessions
export EDITOR='nano'

# Java Setup (Homebrew only)
if command -v brew &> /dev/null && brew --prefix openjdk &>/dev/null; then
    export PATH="$(brew --prefix openjdk)/bin:$PATH"
fi

# CUSTOM ALIASES
alias z="nano $HOME/.zshrc"
alias s="omz reload"
alias h="history"
alias c="cursor ."

# Modern replacements
alias ls='eza --icons --group-directories-first --git --git-repos --header "$@"'
alias cat='bat --style plain'
alias find='fd'
alias grep='rg'
alias top='btop'
alias htop='btop'
alias diff='difft'

# Navigation & Utilities
alias ..="cd .."
alias ...="cd ../.."
alias ll='ls -l'
alias la='ls -a'
alias lla='ls -la'
alias q="exit"
alias path='echo -e ${PATH//:/\\n}'
alias j="java -jar"

# Git aliases
alias ga="git add"
alias gco="git checkout"
alias gca="git commit --amend"
alias gcm="git commit -m"
alias gl="git log"
alias gp="git push"
alias gpl="git pull"
alias gplr="git pull --rebase"
alias gs="git status"
alias gd='git diff'
alias gds='git diff --staged'
alias glg="git log --graph --oneline --decorate --all"
alias gbn="git rev-parse --abbrev-ref HEAD"

# msw-dotfiles sync function
function update_dotfiles() {
    local repo="$HOME/work/msw-dotfiles"
    local msg="${1:-Backing up dotfile changes}"

    git -C "$repo" add -A

    # If the index is different from origin/main, we have work to do
    if ! git -C "$repo" diff-index --quiet origin/main; then
        # 1. Commit if Index != HEAD
        if ! git -C "$repo" diff-index --quiet HEAD; then
            echo "Committing local changes..."
            git -C "$repo" commit -m "$msg"
        fi

        # 2. Sync with remote
        echo "Syncing with GitHub..."
        git -C "$repo" pull --rebase origin main && git -C "$repo" push origin main
    else
        echo "Dotfiles are already up to date with origin/main."
    fi
}
alias dotup="update_dotfiles"

# Source Canva-specific configuration if present
[ -f "$HOME/.canva.zsh" ] && source "$HOME/.canva.zsh"

# Expand aliases on Enter before executing (respects globalias filter)
function expand-alias-and-accept-line() {
    local -a words
    # Use (z) to split into shell words, handle potential empty LBUFFER
    words=(${(z)LBUFFER})
    if (( ${#words} > 0 )); then
        local last_word=${words[-1]}
        # Check if the last word is in our filter list
        if [[ ! " ${GLOBALIAS_FILTER_VALUES[*]} " == *" $last_word "* ]]; then
            zle _expand_alias
        fi
    fi
    zle accept-line
}
zle -N expand-alias-and-accept-line
bindkey '^M' expand-alias-and-accept-line
bindkey '^J' expand-alias-and-accept-line

