# PATH and Environment
export PATH="$HOME/.local/bin:$PATH"
export NVM_DIR="$HOME/.nvm"
export BUN_INSTALL="$HOME/.bun"
export PATH="$BUN_INSTALL/bin:$PATH"

# Oh My Zsh configuration
export ZSH="$HOME/.oh-my-zsh"
ZSH_THEME=""

# Plugins
plugins=(
    git
    zsh-autosuggestions
    zsh-syntax-highlighting
    zsh-history-substring-search
    colored-man-pages
    command-not-found
)

source $ZSH/oh-my-zsh.sh

# ============================================
# SYNTAX HIGHLIGHTING — Adaptive for light/dark backgrounds
# ============================================
# Uses ANSI colors; fg=8 (bright black) is adaptive gray

ZSH_HIGHLIGHT_STYLES[command]='bold'
ZSH_HIGHLIGHT_STYLES[unknown-token]='fg=red,bold'
ZSH_HIGHLIGHT_STYLES[builtin]='bold'
ZSH_HIGHLIGHT_STYLES[alias]='bold,underline'
ZSH_HIGHLIGHT_STYLES[path]='underline'
ZSH_HIGHLIGHT_STYLES[globbing]='fg=yellow'
ZSH_HIGHLIGHT_STYLES[precommand]='bold,underline'
ZSH_HIGHLIGHT_STYLES[single-quoted-argument]='fg=yellow'
ZSH_HIGHLIGHT_STYLES[double-quoted-argument]='fg=yellow'
ZSH_HIGHLIGHT_STYLES[redirection]='bold'
ZSH_HIGHLIGHT_STYLES[comment]='fg=8'

# ============================================
# AUTOSUGGESTIONS — Adaptive
# ============================================
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=8'

# ============================================
# HISTORY SUBSTRING SEARCH
# ============================================
HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND='bold'
HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND='fg=red'

# ============================================
# FZF integration
# ============================================
if [ -f /usr/share/fzf/shell/key-bindings.zsh ]; then
    source /usr/share/fzf/shell/key-bindings.zsh
    export FZF_DEFAULT_OPTS='--height 40% --layout=reverse --border'
    export FZF_CTRL_T_OPTS="--preview 'bat --color=always --line-range :500 {} 2>/dev/null || cat {}'"
fi

# ============================================
# ALIASES — Modern tools
# ============================================
if command -v eza &> /dev/null; then
    alias ls='eza --icons --group-directories-first'
    alias ll='eza -l --icons --group-directories-first'
    alias la='eza -la --icons --group-directories-first'
    alias lt='eza --tree --icons --group-directories-first --git-ignore --ignore-glob="venv|.venv|env|.env|node_modules|.git"'
    alias lta='eza --tree --icons --group-directories-first'
    alias lsga='eza --tree --icons --group-directories-first --git'
    alias lsg3='eza --tree --level 3 --icons --group-directories-first --git'
    alias lsgm='git status -s'
fi

if command -v bat &> /dev/null; then
    alias cat='bat --style=auto'
fi

if command -v rg &> /dev/null; then
    alias grep='rg'
fi

if command -v fd &> /dev/null; then
    alias find='fd'
fi

# ============================================
# STARSHIP PROMPT
# ============================================
if command -v starship &> /dev/null; then
    eval "$(starship init zsh)"
fi

# ============================================
# NVM & Bun
# ============================================
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
[ -s "$HOME/.bun/_bun" ] && source "$HOME/.bun/_bun"

# ============================================
# DOTFILES (Bare Git Repository)
# ============================================
alias config='/usr/bin/git --git-dir=$HOME/.cfg/ --work-tree=$HOME'

# ============================================
# TELEMETRY DISABLE
# ============================================
export CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1
export DISABLE_TELEMETRY=1
export DISABLE_ERROR_REPORTING=1

# ============================================
# PATH EXTRAS
# ============================================
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv zsh)"
export PATH=/home/dawid/.opencode/bin:$PATH
export PATH="$HOME/.local/bin:$PATH"

# ============================================
# TMUX ALIASES
# ============================================
alias ta='tmux attach -t'
alias tl='tmux ls'
alias tn='tmux new -s'
alias tk='tmux kill-session -t'
alias tw='tmux new-window'
alias tsp='tmux split-window -v'
alias tsh='tmux split-window -h'
alias ts='~/.local/bin/tmux-session 2>/dev/null || tmux-session'

# Apply theme to current tmux session
ttheme() {
    local session
    session=$(tmux display-message -p '#S' 2>/dev/null)
    if [ -z "$session" ]; then
        echo "No active tmux session"
        return 1
    fi
    if [ -z "$1" ]; then
        echo "Usage: ttheme <theme>"
        echo "Themes: black, graphite, paper, cobalt, green, blue, purple, orange, red"
        echo "        nord, everforest, gruvbox (dark)"
        echo "        lcobalt, lgreen, lblue, lpurple, lorange, lred, lnord, leverforest, lgruvbox (light)"
        return 1
    fi
    bash -c "source ~/.tmux/themes.sh && apply_theme '$1' '$session'"
}

# Tmux help
th() {
    echo ""
    echo "  TMUX ALIASES"
    echo "  ────────────────────────────────────"
    echo "  ta <name>    attach to session"
    echo "  tl           list sessions"
    echo "  tn <name>    new session"
    echo "  tk <name>    kill session"
    echo "  tw           new window"
    echo "  tsp          split vertical (↕)"
    echo "  tsh          split horizontal (↔)"
    echo "  ttheme <n>   apply theme to session"
    echo ""
    echo "  DARK THEMES (accent colors)"
    echo "  ────────────────────────────────────"
    echo "  black (#000000)     graphite (neutral)"
    echo "  cobalt (#5f87a8)    green/code (#98c379)"
    echo "  blue (#0000ff)      purple (#cd00cd)"
    echo "  red (#cd0000)       orange (#d7a65f)"
    echo "  nord (#88c0d0)"
    echo "  everforest (#a7c080) gruvbox (#fabd2f)"
    echo ""
    echo "  LIGHT THEMES (same accents)"
    echo "  ────────────────────────────────────"
    echo "  paper (neutral)     lcobalt"
    echo "  lgreen              lblue"
    echo "  lpurple             lorange"
    echo "  lred                lnord"
    echo "  leverforest         lgruvbox"
    echo ""
    echo "  AUTO-THEME (from session name)"
    echo "  ────────────────────────────────────"
    echo "  *black* *trueblack*         → black (#000000)"
    echo "  *dev* *code* *claude* *ai*  → green/code (#98c379)"
    echo "  *research* *doc* *read*     → blue"
    echo "  *test* *debug*              → orange"
    echo "  *prod* *urgent* *live*      → red"
    echo "  *creative* *write* *note*   → purple"
    echo ""
}

# ============================================
# ZOXIDE (must be last)
# ============================================
if command -v zoxide &> /dev/null; then
    eval "$(zoxide init zsh --cmd cd)"
fi