#!/bin/bash
#
# Upgrade ClaudeTools Hooks to Offline-Capable Version
# Migrates from v1 hooks to v2 hooks with local storage fallback
#
# Usage: bash scripts/upgrade-to-offline-mode.sh
#

set -e

echo "=========================================="
echo "ClaudeTools Offline Mode Upgrade"
echo "=========================================="
echo ""
echo "This script will upgrade your hooks to support offline operation."
echo ""

# Detect project root
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
HOOKS_DIR="$PROJECT_ROOT/.claude/hooks"

echo "Project root: $PROJECT_ROOT"
echo ""

# Check if hooks directory exists
if [ ! -d "$HOOKS_DIR" ]; then
    echo "[ERROR] ERROR: Hooks directory not found at $HOOKS_DIR"
    exit 1
fi

# Step 1: Backup existing hooks
echo "[1/5] Backing up existing hooks..."
BACKUP_DIR="$HOOKS_DIR/backup_$(date +%Y%m%d_%H%M%S)"
mkdir -p "$BACKUP_DIR"

if [ -f "$HOOKS_DIR/user-prompt-submit" ]; then
    cp "$HOOKS_DIR/user-prompt-submit" "$BACKUP_DIR/"
    echo "  [OK] Backed up user-prompt-submit"
fi

if [ -f "$HOOKS_DIR/task-complete" ]; then
    cp "$HOOKS_DIR/task-complete" "$BACKUP_DIR/"
    echo "  [OK] Backed up task-complete"
fi

echo "  Backup location: $BACKUP_DIR"
echo ""

# Step 2: Install new hooks
echo "[2/5] Installing offline-capable hooks..."

if [ -f "$HOOKS_DIR/user-prompt-submit-v2" ]; then
    cp "$HOOKS_DIR/user-prompt-submit-v2" "$HOOKS_DIR/user-prompt-submit"
    chmod +x "$HOOKS_DIR/user-prompt-submit"
    echo "  [OK] Installed user-prompt-submit (v2)"
else
    echo "  [WARNING] Warning: user-prompt-submit-v2 not found"
fi

if [ -f "$HOOKS_DIR/task-complete-v2" ]; then
    cp "$HOOKS_DIR/task-complete-v2" "$HOOKS_DIR/task-complete"
    chmod +x "$HOOKS_DIR/task-complete"
    echo "  [OK] Installed task-complete (v2)"
else
    echo "  [WARNING] Warning: task-complete-v2 not found"
fi

if [ -f "$HOOKS_DIR/sync-contexts" ]; then
    chmod +x "$HOOKS_DIR/sync-contexts"
    echo "  [OK] Made sync-contexts executable"
else
    echo "  [WARNING] Warning: sync-contexts not found"
fi

echo ""

# Step 3: Create storage directories
echo "[3/5] Creating local storage directories..."
mkdir -p "$PROJECT_ROOT/.claude/context-cache"
mkdir -p "$PROJECT_ROOT/.claude/context-queue/pending"
mkdir -p "$PROJECT_ROOT/.claude/context-queue/uploaded"
mkdir -p "$PROJECT_ROOT/.claude/context-queue/failed"
echo "  [OK] Created .claude/context-cache/"
echo "  [OK] Created .claude/context-queue/{pending,uploaded,failed}/"
echo ""

# Step 4: Update .gitignore
echo "[4/5] Updating .gitignore..."
GITIGNORE="$PROJECT_ROOT/.gitignore"

if [ -f "$GITIGNORE" ]; then
    # Check if entries already exist
    if ! grep -q "\.claude/context-cache/" "$GITIGNORE" 2>/dev/null; then
        echo "" >> "$GITIGNORE"
        echo "# Context recall local storage (offline mode)" >> "$GITIGNORE"
        echo ".claude/context-cache/" >> "$GITIGNORE"
        echo ".claude/context-queue/" >> "$GITIGNORE"
        echo "  [OK] Added entries to .gitignore"
    else
        echo "  ℹ .gitignore already updated"
    fi
else
    echo "  [WARNING] Warning: .gitignore not found"
fi

echo ""

# Step 5: Verification
echo "[5/5] Verifying installation..."

VERIFICATION_PASSED=true

# Check hooks are executable
if [ ! -x "$HOOKS_DIR/user-prompt-submit" ]; then
    echo "  [ERROR] user-prompt-submit is not executable"
    VERIFICATION_PASSED=false
fi

if [ ! -x "$HOOKS_DIR/task-complete" ]; then
    echo "  [ERROR] task-complete is not executable"
    VERIFICATION_PASSED=false
fi

if [ ! -x "$HOOKS_DIR/sync-contexts" ]; then
    echo "  [ERROR] sync-contexts is not executable"
    VERIFICATION_PASSED=false
fi

# Check directories exist
if [ ! -d "$PROJECT_ROOT/.claude/context-cache" ]; then
    echo "  [ERROR] context-cache directory missing"
    VERIFICATION_PASSED=false
fi

if [ ! -d "$PROJECT_ROOT/.claude/context-queue/pending" ]; then
    echo "  [ERROR] context-queue/pending directory missing"
    VERIFICATION_PASSED=false
fi

if [ "$VERIFICATION_PASSED" = "true" ]; then
    echo "  [OK] All checks passed"
else
    echo "  [WARNING] Some checks failed - please review"
fi

echo ""
echo "=========================================="
echo "Upgrade Complete!"
echo "=========================================="
echo ""
echo "[SUCCESS] Offline mode is now active!"
echo ""
echo "Features enabled:"
echo "  • Context caching for offline reading"
echo "  • Context queuing when API unavailable"
echo "  • Automatic sync when API restored"
echo ""
echo "Next steps:"
echo "  1. Use Claude Code normally - offline support is automatic"
echo "  2. Review documentation: .claude/OFFLINE_MODE.md"
echo "  3. Test offline mode by stopping the API temporarily"
echo ""
echo "Manual sync command:"
echo "  bash .claude/hooks/sync-contexts"
echo ""
echo "Rollback (if needed):"
echo "  cp $BACKUP_DIR/* .claude/hooks/"
echo ""
