#!/usr/bin/env bash

FLAG_FILE="$HOME/.config/nnomarchy/.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" \
	"Nnomarchy" "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"
nnomarchy-theme-selector

# 2. Select initial font
echo "Step 2: Choose your preferred font"
nnomarchy-font-selector

# 3. Setup Local Repo (Crucial for env-update to work)
echo ""
echo "Nnomarchy 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 Nnomarchy System Commit"
    fi
fi

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

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