# 1. P10k Instant Prompt (MUSS GANZ OBEN STEHEN)
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

# 2. OS Info
if [ -f /etc/os-release ]; then
    . /etc/os-release
    OS_ID=$ID
fi

# Environment
export EDITOR='nvim'
export VISUAL='nvim'
export TERM='xterm-256color'

# Entferne das '/' aus der Definition eines Wortes
WORDCHARS='*?_-.[]~=&;!#$%^(){}<>'

# 3. Zinit Setup
ZINIT_HOME="${XDG_DATA_HOME:-${HOME}/.local/share}/zinit/zinit.git"
if [ ! -d "$ZINIT_HOME" ]; then
    mkdir -p "$(dirname $ZINIT_HOME)"
    git clone https://github.com/zdharma-continuum/zinit.git "$ZINIT_HOME"
fi
source "${ZINIT_HOME}/zinit.zsh"

# 4. Plugins
zinit ice depth=1; zinit light romkatv/powerlevel10k
zinit light zsh-users/zsh-syntax-highlighting
zinit light zsh-users/zsh-completions
zinit light zsh-users/zsh-autosuggestions
zinit snippet OMZ::plugins/git/git.plugin.zsh
zinit load 'zsh-users/zsh-history-substring-search'

autoload -Uz compinit && compinit
zinit cdreplay -q

# 5. Theme & Fastfetch
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh
# Fastfetch erst nach dem Prompt-Setup aufrufen
(( $+commands[fastfetch] )) && fastfetch --config ~/.config/fastfetch/config.jsonc

# --- History & Options ---
HISTSIZE=5000
HISTFILE=~/.zsh_history
SAVEHIST=$HISTSIZE
setopt appendhistory sharehistory hist_ignore_space hist_ignore_all_dups 
setopt hist_save_no_dups hist_ignore_dups hist_find_no_dups
setopt autocd auto_param_slash globdots

# --- Aliases & Update Script ---
alias c='clear'
alias x='exit'

# Check ob eza installiert ist, sonst ls fallback
if (( $+commands[eza] )); then
    alias l='eza -lagh --group-directories-first'
else
    alias l='ls -lah --color=always --group-directories-first'
fi
alias yay='yay --noconfirm'

# --- Update Script ---

update_system() {
    # Hilfsfunktion für Flatpak (nur ausführen wenn installiert)
    update_flatpak() {
        if (( $+commands[flatpak] )); then
            echo "--- Flatpak Update ---"
            flatpak update -y && flatpak uninstall --unused -y
        fi
    }

    local s=""; (( $+commands[sudo] )) && s="sudo"

    case "$OS_ID" in
        arch|cachyos)
            echo "--- Arch/CachyOS erkannt ---"
            if (( $+commands[yay] )); then
                yay -Syu --needed --noconfirm
            else
                $s pacman -Syu --noconfirm
            fi
            update_flatpak

            # Verwaiste Pakete entfernen
            local orphans=$(pacman -Qdtq)
            if [[ -n "$orphans" ]]; then
                echo "--- Entferne verwaiste Pakete ---"
                $s pacman -Rns ${=orphans} --noconfirm
            else
                echo "--- System ist bereits sauber ---"
            fi
            ;;

        alpine)
            echo "--- Alpine Linux erkannt ---"
            $s apk update && $s apk upgrade && update_flatpak
            ;;

        debian|ubuntu|kali)
            echo "--- Debian/Ubuntu erkannt ---"
            $s apt update && $s apt upgrade -y && update_flatpak
            $s apt autoremove -y
            ;;

        fedora)
            echo "--- Fedora erkannt ---"
            $s dnf upgrade --refresh -y && update_flatpak
            $s dnf autoremove -y
            ;;

        termux)
            echo "--- Termux erkannt ---"
            pkg upgrade -y && update_flatpak
            ;;

        freebsd)
            echo "--- FreeBSD erkannt ---"
            $s pkg update && $s pkg upgrade -y && update_flatpak
            ;;

        *)
            # Fallback für Derivate
            if [[ "$OS_LIKE" == *"arch"* ]]; then
                $s pacman -Syu --noconfirm && update_flatpak
            elif [[ "$OS_LIKE" == *"debian"* ]]; then
                $s apt update && $s apt upgrade -y && update_flatpak
            else
                echo "Unbekanntes System: $OS_ID. Update bitte manuell."
            fi
            ;;
    esac
}
alias u='update_system'

# Keybindings
bindkey -e
bindkey "^[[1;5C" forward-word
bindkey "^[[1;5D" backward-word
bindkey "^[[3~" delete-char
bindkey '^[[A' history-substring-search-up
bindkey '^[[B' history-substring-search-down