# Set XDG_CONFIG_HOME if not already set
if [[ -z "$XDG_CONFIG_HOME" ]]; then
  export XDG_CONFIG_HOME="$HOME/.config"
fi

export PAGER="less"

# Set editor based on SSH connection
if [[ -n $SSH_CONNECTION ]]; then
  export EDITOR='vim'
else
  export EDITOR='nvim'
fi

# Set PYENV_ROOT and add pyenv to PATH
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"

# Add ~/.local/bin to PATH if it exists
if [ -d "$HOME/.local/bin" ]; then
  PATH="$HOME/.local/bin:$PATH"
fi
chmod -R ug+rwx "$HOME/.local/bin/"

# Source fzf theme if it exists
if [ -f fzf_theme ]; then
  source fzf_theme > /dev/null 2>&1
fi

# Add RVM to PATH
export PATH="$PATH:$HOME/.rvm/bin"

# Add Golang to PATH
export GOPATH="$HOME/Documents/projects:$HOME/go"
export PATH="$PATH:$(go env GOPATH)/bin"

# Function to activate Python virtual environment if found
activate_python_env() {
    local env_dir
    # Use fd (if available) to find activate script
    if command -v fd &> /dev/null; then
        env_dir=$(fd -H -I -t f -d 3 'activate$' | xargs dirname)
    else
        # Fallback to find command
        env_dir=$(find . -type f -name activate -exec dirname {} \;)
    fi
    if [ -n "$env_dir" ]; then
        source "$env_dir/activate"
    else
        echo "No Python virtual environment found in the current directory or its subdirectories."
    fi
}
alias act='activate_python_env'

# Aliases
alias python="python3"
alias vim="nvim"
alias tn="tmux new -s $(basename "$(pwd)")"
alias cat="bat --paging=never"
alias grep="grep --color=auto"
alias ll="ls -alF"

alias cp='cp -i'

# Basic `ls` replacement with `eza`, grouping directories first
alias ls='eza --group-directories-first --color=auto'

# List all files, including hidden ones, in long format with grouped directories
alias la='eza -la --group-directories-first --color=auto'

# List files in long format with grouped directories
alias ll='eza -l --group-directories-first --color=auto'

# List files sorted by modification time, newest first, in long format with grouped directories
alias lt='eza -lt --sort newest'

# List files sorted by size, largest first, in long format with grouped directories
alias lS='eza -lS --group-directories-first --color=auto'

# Display directory tree with a depth limit of 3 levels
alias tree='eza --tree --level=3'

# Display git status alongside files
alias lg='eza --git'

# Set bat to highlight --help messages
alias bathelp='bat --plain --language=help'
help() {
    "$@" --help 2>&1 | bathelp
}

# Overwrite -h and --help with bat highlights
alias -g -- -h='-h 2>&1 | bat --language=help --style=plain'
alias -g -- --help='--help 2>&1 | bat --language=help --style=plain'

# Initialize zoxide
if command -v zoxide &> /dev/null; then
    eval "$(zoxide init bash)"
fi

