# =============================================================================
# 1. HELPER FUNCTIONS & PATH
# =============================================================================
source_if_readable() {
    local file="$1"
    if [[ -f "$file" && -r "$file" ]]; then
        source "$file"
    fi
}

# Ensure local bin is in PATH early (catches manual Starship installations)
export PATH="$HOME/.local/bin:$PATH"

# =============================================================================
# 2. OH MY ZSH FRAMEWORK
# =============================================================================
export ZSH="${ZSH:-$HOME/.oh-my-zsh}"
ZSH_THEME=""

plugins=(
    git
    zsh-autosuggestions
    zsh-syntax-highlighting
)

source_if_readable "$ZSH/oh-my-zsh.sh"

# =============================================================================
# 3. THIRD-PARTY INITIALIZATION & CUSTOM CONFIGS
# =============================================================================
# Load external initializers (SDKMAN, NVM, etc.)
source_if_readable "$HOME/.sourcerc"

# Custom Functions (must load before Path Management)
source_if_readable "$HOME/.func"

# Path Management (Relies on append_path from .func)
source_if_readable "$HOME/.pathrc"

# Aliases (Loaded late so they override framework/system defaults)
source_if_readable "$HOME/.alias"

# =============================================================================
# 4. STARSHIP PROMPT
# =============================================================================
if [[ -z "${STARSHIP_CONFIG:-}" && -f "$HOME/.config/starship.toml" ]]; then
    export STARSHIP_CONFIG="$HOME/.config/starship.toml"
fi

# Starship owns the prompt. It must initialize after Oh My Zsh.
if command -v starship >/dev/null 2>&1; then
    eval "$(starship init zsh)"
elif [[ -x "$HOME/.local/bin/starship" ]]; then
    eval "$("$HOME/.local/bin/starship" init zsh)"
fi
