#!/bin/bash
# Daily auto-update: OpenClaw, Claude Code, OS packages
# Runs at 9:00 AM ET via systemd timer
# Logs results to memory/updates/ for morning briefing

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
WORKSPACE="$(dirname "$SCRIPT_DIR")"
LOG_DIR="$WORKSPACE/memory/updates"
DATE=$(date +%Y-%m-%d)
LOG="$LOG_DIR/$DATE.json"

mkdir -p "$LOG_DIR"

# Initialize log
cat > "$LOG" <<'EOF'
{
  "date": "DATE_PLACEHOLDER",
  "timestamp": "TS_PLACEHOLDER",
  "openclaw": {},
  "claude_code": {},
  "os": {},
  "gateway_restarted": false
}
EOF
sed -i "s/DATE_PLACEHOLDER/$DATE/" "$LOG"
sed -i "s/TS_PLACEHOLDER/$(date -Iseconds)/" "$LOG"

update_json() {
    local key="$1" value="$2"
    python3 << PYEOF
import json
with open("$LOG") as f: d = json.load(f)
keys = "$key".split(".")
obj = d
for k in keys[:-1]: obj = obj[k]
raw = '''$value'''
# Try parsing as JSON first (handles strings, arrays, numbers, booleans)
try:
    obj[keys[-1]] = json.loads(raw)
except (json.JSONDecodeError, ValueError):
    obj[keys[-1]] = raw
with open("$LOG", "w") as f: json.dump(d, f, indent=2)
PYEOF
}

echo "=== Daily Update Check: $DATE ==="

# --- OpenClaw ---
echo ""
echo "--- OpenClaw ---"
OC_BEFORE=$(openclaw --version 2>/dev/null | head -1 || echo "unknown")
OC_LATEST=$(npm show openclaw version 2>/dev/null || echo "unknown")
echo "Current: $OC_BEFORE | Latest: $OC_LATEST"

update_json "openclaw.before" "\"$OC_BEFORE\""
update_json "openclaw.latest" "\"$OC_LATEST\""

if [ "$OC_BEFORE" != "$OC_LATEST" ] && [ "$OC_LATEST" != "unknown" ]; then
    echo "Updating OpenClaw..."
    if npm update -g openclaw 2>&1; then
        OC_AFTER=$(openclaw --version 2>/dev/null | head -1 || echo "unknown")
        update_json "openclaw.after" "\"$OC_AFTER\""
        update_json "openclaw.updated" "true"
        echo "Updated: $OC_BEFORE → $OC_AFTER"
    else
        update_json "openclaw.updated" "false"
        update_json "openclaw.error" "\"npm update failed\""
        echo "Update failed"
    fi
else
    update_json "openclaw.updated" "false"
    echo "Up to date"
fi

# --- Claude Code ---
echo ""
echo "--- Claude Code ---"
CC_BEFORE=$(claude --version 2>/dev/null | sed 's/ (Claude Code)//' || echo "unknown")
CC_LATEST=$(npm show @anthropic-ai/claude-code version 2>/dev/null || echo "unknown")
echo "Current: $CC_BEFORE | Latest: $CC_LATEST"

update_json "claude_code.before" "\"$CC_BEFORE\""
update_json "claude_code.latest" "\"$CC_LATEST\""

if [ "$CC_BEFORE" != "$CC_LATEST" ] && [ "$CC_LATEST" != "unknown" ]; then
    echo "Updating Claude Code..."
    if npm update -g @anthropic-ai/claude-code 2>&1; then
        CC_AFTER=$(claude --version 2>/dev/null | sed 's/ (Claude Code)//' || echo "unknown")
        update_json "claude_code.after" "\"$CC_AFTER\""
        update_json "claude_code.updated" "true"
        echo "Updated: $CC_BEFORE → $CC_AFTER"
    else
        update_json "claude_code.updated" "false"
        update_json "claude_code.error" "\"npm update failed\""
        echo "Update failed"
    fi
else
    update_json "claude_code.updated" "false"
    echo "Up to date"
fi

# --- OS Packages ---
echo ""
echo "--- OS Packages ---"
# Capture upgradable list before updating
sudo apt-get update -qq 2>/dev/null

UPGRADABLE=$(apt list --upgradable 2>/dev/null | grep -v "^Listing" || true)
PKG_COUNT=$(echo "$UPGRADABLE" | grep -c . || echo "0")

update_json "os.available" "$PKG_COUNT"

if [ "$PKG_COUNT" -gt 0 ]; then
    echo "$PKG_COUNT packages upgradable"
    # Capture package names and versions
    PKG_LIST=$(echo "$UPGRADABLE" | head -50 | python3 -c "
import sys, json
pkgs = []
for line in sys.stdin:
    line = line.strip()
    if not line: continue
    parts = line.split('/')
    if len(parts) >= 2:
        name = parts[0]
        rest = '/'.join(parts[1:])
        # Extract version info
        ver_parts = rest.split(' ')
        new_ver = ver_parts[1] if len(ver_parts) > 1 else 'unknown'
        old_ver = ver_parts[-1].strip('[]') if '[' in rest else 'unknown'
        pkgs.append({'name': name, 'from': old_ver, 'to': new_ver})
print(json.dumps(pkgs))
" 2>/dev/null || echo "[]")
    update_json "os.packages" "$PKG_LIST"

    echo "Upgrading..."
    if sudo DEBIAN_FRONTEND=noninteractive apt-get upgrade -y -qq 2>&1 | tail -5; then
        update_json "os.updated" "true"
        echo "OS packages updated"
        
        # Check if reboot required
        if [ -f /var/run/reboot-required ]; then
            update_json "os.reboot_required" "true"
            echo "⚠️  Reboot required!"
        else
            update_json "os.reboot_required" "false"
        fi
    else
        update_json "os.updated" "false"
        update_json "os.error" "\"apt upgrade failed\""
    fi
else
    update_json "os.updated" "false"
    update_json "os.packages" "[]"
    echo "All packages up to date"
fi

# --- Gateway Restart (only if OpenClaw updated) ---
echo ""
OC_UPDATED=$(python3 -c "import json; print(json.load(open('$LOG'))['openclaw'].get('updated', False))")
if [ "$OC_UPDATED" = "True" ]; then
    echo "OpenClaw was updated — restarting gateway..."
    systemctl --user restart openclaw-gateway
    update_json "gateway_restarted" "true"
    echo "Gateway restarted"
else
    echo "No gateway restart needed"
fi

echo ""
echo "=== Update complete. Log: $LOG ==="
