#!/usr/bin/env bash

FLAG_FILE="$HOME/.config/nomarchy/.first-run-done"

if [ -f "$FLAG_FILE" ]; then
    exit 0
fi

# Ensure we have a terminal for the wizard
if [ -z "$TERMINAL_WIZARD" ]; then
    export TERMINAL_WIZARD=1
    alacritty -e "$0"
    exit 0
fi

gum style \
	--foreground 212 --border-foreground 212 --border double \
	--align center --width 50 --margin "1 2" --padding "2 4" \
	"Nomarchy" "The NixOS Distribution with Nomarchy Flavour"

echo "Welcome! Let's personalize your new system."
echo ""

# 1. Select initial theme
echo "Step 1: Choose your starting theme"
nomarchy-theme-set "$(nomarchy-theme-list | gum filter --placeholder 'Select a theme...')"

# 2. Select initial font
echo "Step 2: Choose your preferred font"
nomarchy-font-set "$(nomarchy-font-list | gum filter --placeholder 'Select a font...')"

# 3. Setup Local Repo (Crucial for nomarchy-env-update to work)
echo ""
echo "Nomarchy relies on a local git repository for declarative updates."
if [ ! -d "/etc/nixos/.git" ]; then
    echo "Warning: /etc/nixos is not a git repository. Declarative updates might fail."
    if gum confirm "Would you like to initialize /etc/nixos as a git repo?"; then
        sudo git -C /etc/nixos init
        sudo git -C /etc/nixos add .
        sudo git -C /etc/nixos commit -m "Initial Nomarchy System Commit"
    fi
fi

# 4. Success
mkdir -p "$(dirname "$FLAG_FILE")"
touch "$FLAG_FILE"

gum style --foreground 82 "Setup complete! Enjoy your Nomarchy experience."
sleep 3
