# File Analysis Container - ZSH Configuration with Oh My Zsh

# Skip interactive first-time setup
ZSH_DISABLE_COMPFIX=true

# If running as root or system user without home, create minimal config
if [[ ! -d "$HOME" ]] || [[ ! -w "$HOME" ]]; then
    # Minimal non-OMZ setup for system users
    HISTFILE=/tmp/.zsh_history_$$
    HISTSIZE=10000
    SAVEHIST=10000

    autoload -Uz compinit && compinit -d /tmp/.zcompdump_$$
    autoload -U colors && colors

    PROMPT='%F{red}[>]%f %F{cyan}%~%f $ '

    # Load plugins if available
    [[ -f /usr/share/zsh-autosuggestions/zsh-autosuggestions.zsh ]] && \
        source /usr/share/zsh-autosuggestions/zsh-autosuggestions.zsh
    [[ -f /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh ]] && \
        source /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
else
    # Oh My Zsh setup for regular users
    export ZSH="$HOME/.oh-my-zsh"

    # Install Oh My Zsh if not present
    if [[ ! -d "$ZSH" ]]; then
        echo "Installing Oh My Zsh..."
        RUNZSH=no CHSH=no sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" || {
            # Fallback if curl fails
            RUNZSH=no CHSH=no sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" 2>/dev/null
        }
    fi

    # Oh My Zsh theme
    ZSH_THEME="robbyrussell"

    if [[ -d "$ZSH" ]]; then
        plugins=(git docker command-not-found colored-man-pages)
        source $ZSH/oh-my-zsh.sh 2>/dev/null || true

        # Custom prompt with analysis indicator
        PROMPT='%F{red}>%f %F{cyan}%~%f $(git_prompt_info)%# '
        RPROMPT='%F{yellow}%*%f'
    else
        # Fallback if OMZ installation failed
        autoload -U colors && colors
        PROMPT='%F{red}[>]%f %F{cyan}%~%f $ '
    fi
fi

# History configuration
HISTFILE=${HISTFILE:-~/.zsh_history}
HISTSIZE=10000
SAVEHIST=10000
setopt SHARE_HISTORY
setopt HIST_IGNORE_ALL_DUPS
setopt HIST_FIND_NO_DUPS
setopt HIST_REDUCE_BLANKS
setopt HIST_VERIFY

# Navigation
setopt AUTO_CD
setopt AUTO_PUSHD
setopt PUSHD_IGNORE_DUPS
setopt PUSHD_SILENT

# Completion
autoload -Uz compinit
if [[ -w "$HOME" ]]; then
    compinit
else
    compinit -d /tmp/.zcompdump_$$
fi

zstyle ':completion:*' menu select
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}'
setopt COMPLETE_IN_WORD
setopt AUTO_MENU

# Load system plugins if available
[[ -f /usr/share/zsh-autosuggestions/zsh-autosuggestions.zsh ]] && \
    source /usr/share/zsh-autosuggestions/zsh-autosuggestions.zsh
[[ -f /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh ]] && \
    source /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

# Standard aliases
alias ls='ls --color=auto'
alias ll='ls -lah'
alias la='ls -A'
alias l='ls -CF'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'

# Tool aliases
alias fd='fdfind'
alias bat='batcat'
alias rg='rg --color=auto'

# Help system aliases (note: 'help' intentionally NOT aliased — preserves bash builtin)
alias analyse='fhelp'
alias h='fhelp'
if [[ -n "$ZSH_VERSION" ]]; then
    alias \?='fhelp'
else
    alias '?'='fhelp'
fi

# Quick navigation
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'

# Analysis shortcuts
alias pdfinfo='pdfid.py'
alias pdfparse='pdf-parser.py'
alias scanpdf='pdf-parser.py'

# Git aliases (if git plugin not loaded)
if ! type git &>/dev/null || ! alias gs &>/dev/null; then
    alias gs='git status'
    alias ga='git add'
    alias gc='git commit'
    alias gp='git push'
    alias gl='git log --oneline'
fi

# Environment
export EDITOR=vim
export VISUAL=vim

# ============================================================
# Navi interactive cheatsheet widget (Ctrl+G)
# ============================================================
if command -v navi &>/dev/null; then
    eval "$(navi widget zsh)" 2>/dev/null
fi

# ============================================================
# F1 / Ctrl+H: Show help for the command being typed
# ============================================================
_fhelp_inline_widget() {
    local cmd="${BUFFER%% *}"
    if [[ -n "$cmd" ]]; then
        zle -I
        echo ""
        fhelp cheat "$cmd" 2>/dev/null || fhelp tools "$cmd" 2>/dev/null || echo "No help for: $cmd"
        echo ""
        zle reset-prompt
        zle redisplay
    fi
}
zle -N _fhelp_inline_widget
bindkey '\eOP' _fhelp_inline_widget     # F1
bindkey '\e[11~' _fhelp_inline_widget   # F1 (alternate escape)
bindkey '^_' _fhelp_inline_widget       # Ctrl+/ (universal fallback)

# ============================================================
# Welcome message (login shells only)
# ============================================================
if [[ -o interactive && -o login ]] || [[ -o interactive && -z "$_WELCOME_SHOWN" ]]; then
    [[ -f /usr/local/bin/welcome.sh ]] && source /usr/local/bin/welcome.sh
fi
