# ========================= Omarchy ZSH ========================= #
# If not running interactively, don't do anything
[[ $- != *i* ]] && return

# Load zsh options, keybindings, and completion
[[ -f /usr/share/omarchy-zsh/shell/zoptions ]] && source /usr/share/omarchy-zsh/shell/zoptions

# Load shared shell configuration (aliases, functions, environment, tool init)
[[ -f /usr/share/omarchy-zsh/shell/all ]] && source /usr/share/omarchy-zsh/shell/all

# ========================= Environment Variables ========================== #
export EDITOR=nvim
export VISUAL=nvim
export PAGER='bat'
export BAT_THEME="Catppuccin Mocha"

# =============================== Custom Plugins =============================== #
# Autosuggestions
source ~/.local/share/zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh
# Colored Man Pages
source ~/.local/share/zsh/plugins/colored-man-pages.plugin.zsh

# =============================== Custom Completions =============================== #
source ~/.local/share/zsh/extra/zellij-completions.zsh
source ~/.local/share/zsh/extra/atuin.zsh

# ======================== ZSH Options ========================= #
setopt autocd
setopt notify
setopt numericglobsort
setopt promptsubst

# ======================== ZSH History ========================= #
unset HISTFILE
unsetopt share_history
unsetopt append_history
unsetopt inc_append_history
unsetopt extended_history

# =============================== Aliases =============================== #

# --------------- Git Shortcuts --------------- #
alias gst='git status'                                # Show git status
alias bgr='git branch'                                # List branches
alias gcm='git commit -m'                             # Commit with message
alias gammend='git commit --amend'                    # Amend last commit
alias gamendnm='git commit --amend --no-edit'         # Amend last commit without changing message
alias glog='git log'                                  # Show git log
alias gdff='git diff'                                 # Show git diff
alias gpull='git pull'                                # Pull latest changes
alias gfetch='git fetch'                              # Fetch latest changes
alias gpush='git push'                                # Push changes
alias gadd='git add'                                  # Add files to staging
alias gcl='git clone'                                 # Clone a repository
alias guncommit='git reset --soft HEAD~1'             # Undo last commit but keep changes
alias gcheckout='git checkout -b'                     # Git Checkout branch
alias github="cd $HOME/Documents/Github"              # Go to Github Directory

# --------------- Tig Shortcuts ---------------
alias t='tig'                                        # Start tig
alias tb='tig blame'                                 # Start tig blame
alias tst='tig status'                               # Start tig status
alias tlog='tig log'                                 # Start tig log
alias tdf='tig diff'                                 # Start tig diff

# --------------- File & Directory Navigation ---------------
alias ..='cd ..'                    # Go up one directory
alias ...='cd ../..'                # Go up two directories
alias ....='cd ../../..'            # # Go up three directories
mkcd() { mkdir -p "$1" && cd "$1"}  # Create a directory and navigate into it
alias edit-here='code .'            # Open current directory in VS Code
alias here='xdg-open .'             # Open current directory in file explorer

# --------------- File Operations ---------------
alias l='eza -lh --icons=auto --group-directories-first'                        # List files with human-readable sizes and directories first
alias la='eza -lah --icons=auto --group-directories-first'                      # List all files with details and color
alias ll='eza -lh --icons=always --group-directories-first'                     # Long list with exa showing directory sizes
alias lf='eza -lh --icons=always --only-files'                                  # List only files using exa
alias lh='eza -lha --icons=always | grep "^\."'                                 # List only hidden files
alias back='cd -'                                                               # Go back to previous directory
alias lt='eza --tree --level=2 --long --icons --git'                            # List Tree view
alias lta='eza --tree --level=2 --long --icons --git -a'                        # List Tree view including hidden files
alias tree='eza --tree'                                                         # List files in a tree structure
alias lgrep='ls -lah | grep'                                                    # Search files in current directory with ls and grep

# --------------- Process & System Control ---------------
alias cls='clear'                       # Clear the console
alias edit-profile='code ~/.zshrc'      # Edit Profile
alias zshrc='nvim ~/.zshrc'             # Edit Profile
edit() { code "$1" }                    # Open files in VS Code

# --------------- Hashing ---------------
alias md5="md5sum"                   # Alias md5 to md5sum for consistency across systems
alias sha1="sha1sum"                 # Alias sha1 to sha1sum for consistency across systems
alias sha256="sha256sum"             # Alias sha256 to sha256sum for consistency

# --------------- External Tools ---------------
alias ji='zi'                       # Alias ji to zoxide's ji function for fuzzy directory navigation
alias cat='bat'                     # Use bat instead of cat for better file viewing with syntax highlighting and paging
alias help='tldr --color always'    # Use tldr with color support
alias lg='lazygit'                  # Launch LazyGit
alias z='zellij'                    # Launch Zellij
alias lazyzellij='lzj'              # Zellij Session Manager
alias top='btop'                    # Launch btop
alias htop='btop'                   # Launch btop
alias neofetch='fastfetch'          # Launch FastFetch
alias gitfetch='onefetch'           # Use onefetch for git repository information

# --------------- Miscellaneous ---------------
alias grep='grep --color=auto'         # Highlight matching text in grep
alias rm='rm -i'                       # Prompt before deleting files
alias mkdir='mkdir -p'                 # Create parent directories as needed
alias ports='ss -tulwn'                # Show listening ports
alias dfh='df -h'                      # Human-readable disk usage
alias dus='du -sh *'                   # Summarize folder sizes
alias mem='free -h'                    # Memory usage
alias cpuinfo='lscpu'                  # CPU info
alias nano='nvim'                      # Use Neovim instead of nano
alias vim='nvim'                       # Use Neovim instead of vim
alias vi='nvim'                        # Use Neovim instead of vi
alias emacs='nvim'                     # Use Neovim instead of emacs
alias myip='curl ipinfo.io/ip'         # Get public IP address
alias hb='hastebin'                    # Upload file to hastebin

# Print stsyem path variables
path(){ local i=1 p; for p in "${(@)path}"; do printf "\e[38;5;75m%2d\e[0m  \e[38;5;114m%s\e[0m\n" "$i" "$p"; ((i++)); done; }

# =============================== fnm =============================== #
autoload -U add-zsh-hook

function load_node_version() {
    local dir="$PWD"
    local version_file=""

    while [[ "$dir" != "/" ]]; do
        if [[ -f "$dir/.nvmrc" ]]; then
            version_file="$dir/.nvmrc"
            break
        elif [[ -f "$dir/.node-version" ]]; then
            version_file="$dir/.node-version"
            break
        fi

        dir="${dir:h}"
    done

    if [[ -n "$version_file" ]]; then
        eval "$(fnm env --shell zsh)"
        fnm use --install-if-missing
    else
        export PATH="${PATH//\/run\/user\/1000\/fnm_multishells[^:]##:/}"
    fi
}

add-zsh-hook chpwd load_node_version
load_node_version

# =============================== keybindings =============================== #

# Open My Zellij Session Manager
zij-widget() {
    echo
    zij
    echo
    echo
    zle reset-prompt
}

zle -N zij-widget
bindkey -M emacs '^[z' zij-widget

# Open My Tmux Session Manager
tij-widget() {
    echo 
    tij
    echo
    echo
    zle reset-prompt
}

zle -N tij-widget
bindkey -M emacs '^[t' tij-widget

# Open Hastebin
hb-widget() {
    zle -I
    hb
    zle reset-prompt
}

zle -N hb-widget
bindkey -M emacs '^[b' hb-widget
