#!/usr/bin/env bash
# opencode-kit update-version — force install latest, update global + local, clear cache
# Usage: bash src/update-version.sh [--dry-run]
set -euo pipefail

DRY_RUN=false
[[ "${1:-}" == "--dry-run" ]] && DRY_RUN=true

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
. "$SCRIPT_DIR/platform.sh"

# --- Determine package root (where package.json lives) ---
PKG_ROOT="$SCRIPT_DIR/.."
if [[ -f "$PKG_ROOT/package.json" ]]; then
  CURRENT_VER=$(json_get "$PKG_ROOT/package.json" "version")
else
  CURRENT_VER="unknown"
fi

echo "opencode-kit update-version"
echo "  Current version: v${CURRENT_VER}"
echo ""

# --- Step 1: Fetch latest version from npm registry ---
echo "Fetching latest version from npm registry..."
LATEST_VER=$(npm view @ikieaneh/opencode-kit version 2>/dev/null) || {
  echo "Error: Cannot reach npm registry. Check your internet connection." >&2
  exit 1
}
echo "  Latest version:  v${LATEST_VER}"
echo ""

if [[ "$CURRENT_VER" == "$LATEST_VER" ]]; then
  echo "Already on latest version (v${LATEST_VER}). Nothing to do."
  exit 0
fi

if $DRY_RUN; then
  echo "[dry-run] Would update v${CURRENT_VER} → v${LATEST_VER}"
  echo "[dry-run] Steps: clear cache → npm install -g → npm install --save → re-scaffold"
  exit 0
fi

# --- Step 2: Clear npm cache for this package ---
echo "Clearing npm cache for @ikieaneh/opencode-kit..."
npm cache clean --force 2>/dev/null || true
echo "  Cache cleared."

# --- Step 2.5: Clear OpenCode plugin cache ---
echo "Clearing OpenCode plugin cache..."
OPENDODE_CACHE="$HOME/.cache/opencode/packages/@ikieaneh"
if [[ -d "$OPENDODE_CACHE" ]]; then
  rm -rf "$OPENDODE_CACHE"
  echo "  Cleared: $OPENDODE_CACHE"
else
  echo "  No OpenCode cache found"
fi
# Also clear bun.lock if it exists
BUN_LOCK="$HOME/.cache/opencode/bun.lock"
if [[ -f "$BUN_LOCK" ]]; then
  rm -f "$BUN_LOCK"
  echo "  Cleared: $BUN_LOCK"
fi

# --- Step 3: Update global installation ---
echo "Updating global installation..."
if command -v npx >/dev/null 2>&1; then
  npm install -g @ikieaneh/opencode-kit@latest --prefer-online 2>&1 | tail -1
  echo "  Global updated."
else
  echo "  Skipped (npx not found — install globally with: npm install -g @ikieaneh/opencode-kit@latest)"
fi

# --- Step 4: Update local project installation ---
PROJECT_ROOT="$(pwd)"
if [[ -f "$PROJECT_ROOT/package.json" ]]; then
  echo "Updating local project installation..."
  npm install @ikieaneh/opencode-kit@latest --save 2>&1 | tail -1
  echo "  Local updated."
else
  echo "  Skipped local (no package.json in current directory)"
fi

# --- Step 5: Re-scaffold if .opencode exists ---
if [[ -d "$PROJECT_ROOT/.opencode" ]]; then
  echo "Re-scaffolding project..."
  # Find the installed script — try local node_modules first, then global
  INIT_SCRIPT=""
  if [[ -f "$PROJECT_ROOT/node_modules/@ikieaneh/opencode-kit/src/init.sh" ]]; then
    INIT_SCRIPT="$PROJECT_ROOT/node_modules/@ikieaneh/opencode-kit/src/init.sh"
  elif command -v npx >/dev/null 2>&1; then
    GLOBAL_PKG=$(npm root -g 2>/dev/null)/@ikieaneh/opencode-kit
    if [[ -f "$GLOBAL_PKG/src/init.sh" ]]; then
      INIT_SCRIPT="$GLOBAL_PKG/src/init.sh"
    fi
  fi

  if [[ -n "$INIT_SCRIPT" ]]; then
    bash "$INIT_SCRIPT" 2>&1 | sed 's/^/  /'
    echo "  Scaffold updated."
  else
    echo "  Skipped (init.sh not found — run manually: npx opencode-kit init)"
  fi
else
  echo "  Skipped scaffold (no .opencode/ directory)"
fi

# --- Step 6: Show result ---
echo ""
echo "Update complete: v${CURRENT_VER} → v${LATEST_VER}"
echo "Restart opencode to use the new version."
