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

# mosaic-release-upgrade — Upgrade installed Mosaic framework release.
#
# This re-runs the remote installer with explicit install mode controls.
# Default behavior is safe/idempotent (keep SOUL.md + memory).
#
# Usage:
#   mosaic-release-upgrade
#   mosaic-release-upgrade --ref main --keep
#   mosaic-release-upgrade --ref v0.2.0 --overwrite --yes

MOSAIC_HOME="${MOSAIC_HOME:-$HOME/.config/mosaic}"
REMOTE_SCRIPT_URL="${MOSAIC_REMOTE_INSTALL_URL:-https://git.mosaicstack.dev/mosaic/mosaic-stack/raw/branch/main/tools/install.sh}"
BOOTSTRAP_REF="${MOSAIC_BOOTSTRAP_REF:-main}"
INSTALL_MODE="${MOSAIC_INSTALL_MODE:-keep}" # keep|overwrite
YES=false
DRY_RUN=false

usage() {
  cat <<USAGE
Usage: $(basename "$0") [options]

Upgrade the installed Mosaic framework release.

Options:
  --ref <name>         Bootstrap archive ref (branch/tag/commit). Default: main
  --keep               Keep local files (SOUL.md, memory/) during upgrade (default)
  --overwrite          Overwrite target install directory contents
  -y, --yes            Skip confirmation prompt
  --dry-run            Show actions without executing
  -h, --help           Show this help
USAGE
}

while [[ $# -gt 0 ]]; do
  case "$1" in
    --ref)
      [[ $# -lt 2 ]] && { echo "Missing value for --ref" >&2; exit 1; }
      BOOTSTRAP_REF="$2"
      shift 2
      ;;
    --keep)
      INSTALL_MODE="keep"
      shift
      ;;
    --overwrite)
      INSTALL_MODE="overwrite"
      shift
      ;;
    -y|--yes)
      YES=true
      shift
      ;;
    --dry-run)
      DRY_RUN=true
      shift
      ;;
    -h|--help)
      usage
      exit 0
      ;;
    *)
      echo "Unknown argument: $1" >&2
      usage >&2
      exit 1
      ;;
  esac
done

case "$INSTALL_MODE" in
  keep|overwrite) ;;
  *)
    echo "[mosaic-release-upgrade] Invalid install mode: $INSTALL_MODE" >&2
    exit 1
    ;;
esac

current_version="unknown"
if [[ -x "$MOSAIC_HOME/bin/mosaic" ]]; then
  current_version="$("$MOSAIC_HOME/bin/mosaic" --version 2>/dev/null | awk '{print $2}' || true)"
  [[ -n "$current_version" ]] || current_version="unknown"
fi

echo "[mosaic-release-upgrade] Current version: $current_version"
echo "[mosaic-release-upgrade] Target ref: $BOOTSTRAP_REF"
echo "[mosaic-release-upgrade] Install mode: $INSTALL_MODE"
echo "[mosaic-release-upgrade] Installer URL: $REMOTE_SCRIPT_URL"

if [[ "$DRY_RUN" == "true" ]]; then
  echo "[mosaic-release-upgrade] Dry run: no changes applied."
  exit 0
fi

if [[ "$YES" != "true" && -t 0 ]]; then
  printf "Proceed with Mosaic release upgrade? [y/N]: "
  read -r confirm
  case "${confirm:-n}" in
    y|Y|yes|YES) ;;
    *)
      echo "[mosaic-release-upgrade] Aborted."
      exit 1
      ;;
  esac
fi

if command -v curl >/dev/null 2>&1; then
  curl -sL "$REMOTE_SCRIPT_URL" | \
    MOSAIC_BOOTSTRAP_REF="$BOOTSTRAP_REF" \
    MOSAIC_INSTALL_MODE="$INSTALL_MODE" \
    MOSAIC_HOME="$MOSAIC_HOME" \
    sh
elif command -v wget >/dev/null 2>&1; then
  wget -qO- "$REMOTE_SCRIPT_URL" | \
    MOSAIC_BOOTSTRAP_REF="$BOOTSTRAP_REF" \
    MOSAIC_INSTALL_MODE="$INSTALL_MODE" \
    MOSAIC_HOME="$MOSAIC_HOME" \
    sh
else
  echo "[mosaic-release-upgrade] ERROR: curl or wget required." >&2
  exit 1
fi

