#!/usr/bin/env bash
set -euo pipefail

script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
source_root="$(cd -- "$script_dir/.." && pwd)"
if [[ -f "$source_root/app/shell.qml" ]]; then
  default_root="$source_root"
else
  default_root="$(cd -- "$script_dir/../share/learn-omarchy" && pwd)"
fi
app_root="${LEARN_OMARCHY_ROOT:-$default_root}"
course="${LEARN_OMARCHY_COURSE:-$app_root/courses/omarchy-basics.json}"
character="${LEARN_OMARCHY_CHARACTER:-}"
uninstall=false

while (($# > 0)); do
  case "$1" in
    --course)
      [[ $# -ge 2 ]] || { echo "learn-omarchy: --course requires a path" >&2; exit 2; }
      course="$2"
      shift 2
      ;;
    --character)
      [[ $# -ge 2 ]] || { echo "learn-omarchy: --character requires a name" >&2; exit 2; }
      character="$2"
      shift 2
      ;;
    --uninstall)
      uninstall=true
      shift
      ;;
    -h|--help)
      cat <<'USAGE'
Usage: learn-omarchy [--course PATH] [--character NAME]
       learn-omarchy --uninstall

Run an interactive, theme-aware Omarchy course. Character packs are discovered
from the bundled assets and your user data directory. Invalid or missing
selections recover to an available pack with a visible diagnostic.
--uninstall removes the Arch package, preserving progress. Run it from a
terminal as your regular user.
USAGE
      exit 0
      ;;
    *)
      echo "learn-omarchy: unknown argument '$1'" >&2
      exit 2
      ;;
  esac
done

required_commands=(node)
if [[ "$uninstall" == false ]]; then required_commands+=(qs mpv); fi
for command in "${required_commands[@]}"; do
  command -v "$command" >/dev/null 2>&1 || {
    echo "learn-omarchy: required command not found: $command" >&2
    exit 1
  }
done

if [[ "$uninstall" == true ]]; then
  [[ $EUID -ne 0 && -t 0 && -t 1 ]] || {
    echo "learn-omarchy: run --uninstall from a terminal as your regular user; it will request package-removal permission." >&2
    exit 1
  }
  command -v pacman >/dev/null && command -v sudo >/dev/null || {
    echo "learn-omarchy: Arch package removal requires pacman and sudo." >&2
    exit 1
  }
  pacman -Q learn-omarchy >/dev/null 2>&1 || {
    echo "learn-omarchy: no installed Arch package was found; use the removal method for your source installation." >&2
    exit 1
  }
  cleanup_dir="$(mktemp -d "${TMPDIR:-/tmp}/learn-omarchy-uninstall.XXXXXXXX")"
  cleanup_helper="$cleanup_dir/remove-legacy-integration.mjs"
  trap 'rm -f -- "$cleanup_helper"; rmdir -- "$cleanup_dir"' EXIT
  cp -- "$app_root/tools/remove-legacy-integration.mjs" "$cleanup_helper"
  # Keep pacman's confirmation. Cancelling leaves everything untouched.
  sudo pacman -R learn-omarchy
  # Earlier releases installed a desktop integration; remove it if it's still there.
  node "$cleanup_helper" --quiet || echo "learn-omarchy: the package was removed, but a retired integration was left in place." >&2
  exit 0
fi

course="$(realpath -- "$course")"
course_dir="$(dirname -- "$course")"
node --experimental-strip-types "$app_root/tools/validate-course.ts" "$course"
mkdir -p "${XDG_STATE_HOME:-$HOME/.local/state}/learn-omarchy"


# Only an explicit choice becomes an override; otherwise the app shows the
# coach picker on first run and remembers the selection.
if [[ -n "$character" ]]; then
  export LEARN_OMARCHY_CHARACTER="$character"
fi
export LEARN_OMARCHY_ROOT="$app_root"
export LEARN_OMARCHY_COURSE="$course"
export LEARN_OMARCHY_COURSE_DIR="$course_dir"
exec qs --no-duplicate --path "$app_root/app"
