# =============================================================================
# 1. ZSH THEME & PLUGINS (Must be defined before Oh My Zsh loads)
# =============================================================================
export ZSH="$HOME/.oh-my-zsh"

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

ZSH_THEME_CHOICE="${ZSH_THEME_CHOICE:-}"
if [[ -z "$ZSH_THEME_CHOICE" && -f "$HOME/.zsh_theme" ]]; then
    ZSH_THEME_CHOICE="$(<"$HOME/.zsh_theme")"
fi
ZSH_THEME_CHOICE="${ZSH_THEME_CHOICE:-spaceship}"

case "$ZSH_THEME_CHOICE" in
    starship)
        ZSH_THEME=""
        ;;
    spaceship|*)
        ZSH_THEME="spaceship"

        SPACESHIP_PROMPT_ORDER=(
          time          # Time stamps section
          user          # Username section
          dir           # Current directory section
          git           # Git section (git_branch + git_status)
          node          # Node.js section
          dotnet        # .NET section
          java          # Java section
          kotlin        # Kotlin section
          ruby          # Ruby section
          xcode         # Xcode section
          swift         # Swift section
          golang        # Go section
          docker        # Docker section
          venv          # virtualenv section
          line_sep      # Line break
          char          # Prompt character
        )

        SPACESHIP_USER_SHOW=always
        SPACESHIP_PROMPT_SEPARATE_LINE=true
        SPACESHIP_PROMPT_ADD_NEWLINE=true
        SPACESHIP_CHAR_SYMBOL="❯"
        SPACESHIP_CHAR_SUFFIX=" "
        SPACESHIP_DOCKER_CONTEXT_SHOW=false

        if [[ $(uname) == "Darwin" ]]; then
            #  is the Apple logo in Nerd Fonts
            SPACESHIP_USER_SUFFIX="%F{cyan} [  ]%f "
        elif [[ $(uname) == "Linux" ]]; then
            #  is the Ubuntu logo in Nerd Fonts
            SPACESHIP_USER_SUFFIX="%F{yellow} [   ]%f "
        else
            SPACESHIP_USER_SUFFIX=" "
        fi
        ;;
esac

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

# Initialize Oh My Zsh (Crucial for themes and plugins to work)
source_if_readable "$ZSH/oh-my-zsh.sh"

# =============================================================================
# 2. THIRD-PARTY INITIALIZATION (SDKMAN, NVM, Oh My Zsh)
# =============================================================================
source_if_readable "$HOME/.sourcerc"

# =============================================================================
# 3. CUSTOM OVERRIDES (Functions must load before Path Management)
# =============================================================================
source_if_readable "$HOME/.func"

# =============================================================================
# 4. PATH MANAGEMENT (Relies on append_path from .func)
# =============================================================================
source_if_readable "$HOME/.pathrc"

# =============================================================================
# 5. ALIASES (Loaded dead last so YOUR code always wins over Oh My Zsh)
# =============================================================================
source_if_readable "$HOME/.alias"

if [[ "$ZSH_THEME_CHOICE" == "starship" ]]; then
    if command -v starship >/dev/null 2>&1; then
        eval "$(starship init zsh)"
    else
        echo "Warning: Starship selected but starship is not installed."
    fi
fi
