#!/bin/bash
set -euo pipefail

GREEN="\e[32m"
YELLOW="\e[33m"
RESET="\e[0m"

log()  { echo -e "${GREEN}✔ $1${RESET}"; }
skip() { echo -e "${YELLOW}↷ $1${RESET}"; }

# Update system
log "Updating system..."
sudo pacman -Syu --noconfirm

# Install base packages
PACKAGES=(zsh neovim curl pyright bash atftp bash-language-server btop clang fastfetch fzf hyfetch lua-language-server micro nano pulsemixer yazi z qrencode distrobox dysk python python-pip glow)
for pkg in "${PACKAGES[@]}"; do
    if ! pacman -Qi "$pkg" &>/dev/null; then
        log "Installing $pkg..."
        sudo pacman -S --noconfirm --needed "$pkg"
    else
        skip "$pkg already installed."
    fi
done

# yay
if ! command -v yay &>/dev/null; then
    log "Installing yay..."
    sudo pacman -S --noconfirm --needed git base-devel
    git clone https://aur.archlinux.org/yay.git /tmp/yay
    cd /tmp/yay && makepkg -si --noconfirm
    cd ~
else
    skip "yay already installed."
fi

# Rust / Cargo
if ! command -v cargo &>/dev/null; then
    log "Installing Rust & Cargo..."
    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
    . "$HOME/.cargo/env"
else
    skip "Rust & Cargo already installed."
fi

# nvm + Node.js
if ! command -v node &>/dev/null; then
    log "Installing nvm and Node.js..."
    if [ ! -d "$HOME/.nvm" ]; then
        curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.2/install.sh | bash
    fi
    . "$HOME/.nvm/nvm.sh"
    nvm install 22
else
    skip "Node.js already installed."
fi

# git config
git config --global core.editor nvim

# Dotfiles
log "Deploying dotfiles..."
mkdir -p ~/.config ~/Pictures

ln -sf ~/Dotfiles/.bashrc ~/.bashrc
ln -sf ~/Dotfiles/.zshrc ~/.zshrc
ln -sf ~/Dotfiles/starship.toml ~/.config/starship.toml

rm -rf ~/.config/micro
cp -r ~/Dotfiles/micro ~/.config/

# nvim — lazy.nvim bootstraps itself on first launch via init.lua
# airline cyberqueer theme lives at nvim/autoload/airline/themes/cyberqueer.vim
# and is discovered automatically via the rtp symlink below
rm -rf ~/.config/nvim
ln -sf ~/Dotfiles/nvim ~/.config/nvim

rm -rf ~/.config/yazi
ln -sf ~/Dotfiles/yazi ~/.config/yazi

rm -rf ~/.config/spotify-tui
ln -sf ~/Dotfiles/spotify-tui ~/.config/spotify-tui

cp -f ~/Dotfiles/resources/fflogo.svg ~/Pictures/fflogo.svg

# Starship
if ! command -v starship &>/dev/null; then
    log "Installing Starship..."
    curl -sS https://starship.rs/install.sh | sh -s -- --yes
else
    skip "Starship already installed."
fi

# oh-my-zsh (RUNZSH=no prevents installer from spawning a new shell)
if [ ! -d "$HOME/.oh-my-zsh" ]; then
    log "Installing oh-my-zsh..."
    RUNZSH=no CHSH=no sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
else
    skip "oh-my-zsh already installed."
fi

# oh-my-zsh plugins
ZSH_CUSTOM="${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}"
if [ ! -d "$ZSH_CUSTOM/plugins/zsh-syntax-highlighting" ]; then
    git clone https://github.com/zsh-users/zsh-syntax-highlighting.git \
        "$ZSH_CUSTOM/plugins/zsh-syntax-highlighting"
else
    skip "zsh-syntax-highlighting already installed."
fi
if [ ! -d "$ZSH_CUSTOM/plugins/zsh-autosuggestions" ]; then
    git clone https://github.com/zsh-users/zsh-autosuggestions \
        "$ZSH_CUSTOM/plugins/zsh-autosuggestions"
else
    skip "zsh-autosuggestions already installed."
fi

# Default shell
if [ "$SHELL" != "/usr/bin/zsh" ]; then
    log "Setting zsh as default shell..."
    chsh -s /usr/bin/zsh
else
    skip "zsh is already the default shell."
fi

log "Shell setup complete."
