# ░░░░▀▀█░█▀▀░█░█░█▀▄░█▀▀
# ░░░░▄▀░░▀▀█░█▀█░█▀▄░█░░
# ░▀░░▀▀▀░▀▀▀░▀░▀░▀░▀░▀▀▀
# Refined shell configuration following durability principles
# ASCII art: TextKool (https://textkool.com/en) "Font: Pagga"

# ═══════════════════════════════════════════════════════════════════════════════
# PERFORMANCE & EARLY INITIALIZATION
# ═══════════════════════════════════════════════════════════════════════════════

# Enable Powerlevel10k instant prompt (must be near top)
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
  source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi

# ═══════════════════════════════════════════════════════════════════════════════
# CORE ENVIRONMENT SETUP
# ═══════════════════════════════════════════════════════════════════════════════

# Source modular configuration files
local config_files=(
    "$HOME/dotfiles/shell/environment.zsh"
    "$HOME/dotfiles/shell/paths.zsh"
)

for config_file in "${config_files[@]}"; do
    [[ -f "$config_file" ]] && source "$config_file"
done

# ═══════════════════════════════════════════════════════════════════════════════
# PLUGIN MANAGEMENT
# ═══════════════════════════════════════════════════════════════════════════════

# Antidote plugin manager with graceful fallback
if [[ -f "/opt/homebrew/share/antidote/antidote.zsh" ]]; then
    source /opt/homebrew/share/antidote/antidote.zsh
    
    # Load plugins from manifest
    local plugin_file="$HOME/dotfiles/shell/.zsh-plugins.txt"
    if [[ -f "$plugin_file" ]]; then
        antidote load < "$plugin_file"
    else
        echo "⚠️  Plugin manifest not found: $plugin_file"
    fi
else
    echo "⚠️  Antidote not found. Install with: brew install antidote"
fi

# ═══════════════════════════════════════════════════════════════════════════════
# PROMPT CONFIGURATION
# ═══════════════════════════════════════════════════════════════════════════════

# Powerlevel10k configuration
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh
# Run 'p10k configure' to reconfigure prompt

# ═══════════════════════════════════════════════════════════════════════════════
# SHELL BEHAVIOR & OPTIONS
# ═══════════════════════════════════════════════════════════════════════════════

# Directory navigation
setopt auto_cd              # Type directory name to cd into it
setopt auto_pushd           # Make cd push old directory onto stack
setopt pushd_ignore_dups    # Don't push duplicates onto stack
setopt pushd_minus          # Make pushd work like cd

# Command correction and expansion
setopt correct              # Spelling correction for commands
setopt correct_all          # Spelling correction for arguments
setopt interactive_comments # Allow comments in interactive shell
setopt extended_glob        # Enable extended globbing
setopt glob_dots            # Don't require leading . in filename to be matched

# History configuration (extended)
setopt extended_history        # Record timestamp and duration
setopt hist_expire_dups_first  # Expire duplicates first
setopt hist_ignore_all_dups    # Don't record duplicates
setopt hist_ignore_space       # Don't record commands starting with space
setopt hist_verify             # Show command before executing from history
setopt inc_append_history      # Add commands immediately
setopt share_history           # Share history between sessions
setopt hist_reduce_blanks      # Remove superfluous blanks

# Job control
setopt auto_resume          # Single word commands can resume jobs
setopt long_list_jobs       # List jobs in long format
setopt notify               # Report status of background jobs immediately

# ═══════════════════════════════════════════════════════════════════════════════
# COMPLETION SYSTEM
# ═══════════════════════════════════════════════════════════════════════════════

# Initialize completion system
autoload -Uz compinit
compinit

# Completion options
setopt complete_in_word     # Complete from both ends of word
setopt always_to_end        # Move cursor to end after completion
setopt auto_menu            # Show completion menu on successive tab
setopt auto_list            # Automatically list choices on ambiguous completion
setopt auto_param_slash     # Add trailing slash for directory completions

# Completion styling
zstyle ':completion:*' menu select
zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}' 'r:|=*' 'l:|=* r:|=*'

# ═══════════════════════════════════════════════════════════════════════════════
# TOOL INTEGRATIONS
# ═══════════════════════════════════════════════════════════════════════════════

# FZF integration
if [[ -f ~/.fzf.zsh ]]; then
    source ~/.fzf.zsh
elif command -v fzf >/dev/null; then
    # Manual key bindings if fzf is available but not set up
    eval "$(fzf --zsh)" 2>/dev/null || true
fi

# Zoxide integration (smart cd)
if command -v zoxide >/dev/null; then
    eval "$(zoxide init zsh)"
fi

# ═══════════════════════════════════════════════════════════════════════════════
# CUSTOM FUNCTIONS & ALIASES
# ═══════════════════════════════════════════════════════════════════════════════

# Load enhanced functions and aliases
local shell_files=(
    "$HOME/dotfiles/shell/functions.zsh"
    "$HOME/dotfiles/shell/aliases.zsh"
)

for shell_file in "${shell_files[@]}"; do
    if [[ -f "$shell_file" ]]; then
        source "$shell_file"
    else
        echo "⚠️  Shell configuration missing: $shell_file"
    fi
done

# Legacy support (if old files exist)
[[ -f "$HOME/.zsh/aliases.zsh" ]] && source "$HOME/.zsh/aliases.zsh"
[[ -f "$HOME/.zsh/functions.zsh" ]] && source "$HOME/.zsh/functions.zsh"

# ═══════════════════════════════════════════════════════════════════════════════
# CONTEXT AWARENESS
# ═══════════════════════════════════════════════════════════════════════════════

# Detect and set initial context
detect_context

# Display context-aware welcome message (interactive shells only)
if [[ $- == *i* ]] && [[ -z "$VSCODE_INJECTION" ]]; then
    case "$CURRENT_CONTEXT" in
        "work") echo "🏢 Work context activated" ;;
        "chamber") echo "🔮 Chamber context activated" ;;
        "vault") echo "📚 Vault context activated" ;;
        "dotfiles") echo "⚙️  Dotfiles context activated" ;;
    esac
fi

# ═══════════════════════════════════════════════════════════════════════════════
# PERFORMANCE OPTIMIZATION
# ═══════════════════════════════════════════════════════════════════════════════

# Fast mode for non-interactive shells
[[ $- != *i* ]] && unsetopt zle && return

# ═══════════════════════════════════════════════════════════════════════════════
# LOCAL CUSTOMIZATIONS
# ═══════════════════════════════════════════════════════════════════════════════

# Machine-specific configurations
[[ -f "$HOME/.zshrc.local" ]] && source "$HOME/.zshrc.local"

# Development environment configurations
[[ -f "$HOME/.env.local" ]] && source "$HOME/.env.local"

# ═══════════════════════════════════════════════════════════════════════════════
# LEGACY & MIGRATION SUPPORT
# ═══════════════════════════════════════════════════════════════════════════════

# Temporary: Handle pipx path duplication (remove after migration)
typeset -U path PATH

# Note: This configuration follows the μέτρον principle
# - Each section has clear purpose and boundaries
# - Graceful degradation when tools are missing  
# - Performance considerations for different use cases
# - Modular structure for maintainability