#!/usr/bin/env bash
# Install cron jobs in VMID 5000 to keep explorer API and nginx healthy and prevent disk full.
# Run from repo root on Proxmox host that has VMID 5000, or with EXPLORER_VM_HOST=root@<node>.
#
# Cron installed inside the VM:
#   - Every 5 min: health check + restart Blockscout/nginx if needed
#   - Daily 03:15: safe disk prune (images + build cache only, no container prune)

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
[ -f "$REPO_ROOT/../.env" ] && source "$REPO_ROOT/../.env" 2>/dev/null || true
VMID="${EXPLORER_VMID:-5000}"
EXPLORER_NODE="${EXPLORER_VM_HOST:-${PROXMOX_R630_02:-192.168.11.12}}"
if [[ "$EXPLORER_NODE" == *"@"* ]]; then SSH_TARGET="$EXPLORER_NODE"; else SSH_TARGET="root@$EXPLORER_NODE"; fi

# Remote mode
if ! command -v pct &>/dev/null || ! pct list 2>/dev/null | grep -q "^$VMID "; then
    if [ -n "${EXPLORER_VM_HOST:-}" ] || [ -n "${PROXMOX_R630_02:-}" ]; then
        echo "Installing cron via SSH on $SSH_TARGET..."
        scp -o StrictHostKeyChecking=no -o ConnectTimeout=10 "$SCRIPT_DIR/explorer-maintain.sh" "$SSH_TARGET:/tmp/explorer-maintain.sh" 2>/dev/null || true
        scp -o StrictHostKeyChecking=no -o ConnectTimeout=10 "$SCRIPT_DIR/install-explorer-cron.sh" "$SSH_TARGET:/tmp/install-explorer-cron.sh" 2>/dev/null || true
        ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 "$SSH_TARGET" "EXPLORER_VM_HOST= bash /tmp/install-explorer-cron.sh"
        exit $?
    else
        echo "Run on Proxmox host that has VMID $VMID, or set EXPLORER_VM_HOST=root@<node-ip>"
        exit 1
    fi
fi

EXEC_PREFIX="pct exec $VMID --"
MAINTAIN_SCRIPT="/usr/local/bin/explorer-maintain.sh"

echo "=============================================="
echo "Install explorer maintenance cron (VMID $VMID)"
echo "=============================================="

# Copy script into VM
pct push $VMID "$SCRIPT_DIR/explorer-maintain.sh" "$MAINTAIN_SCRIPT"
$EXEC_PREFIX chmod +x "$MAINTAIN_SCRIPT"
echo "✅ Installed $MAINTAIN_SCRIPT"

# Install crontab (append to existing)
$EXEC_PREFIX bash -c '(crontab -l 2>/dev/null | grep -v explorer-maintain | grep -v /usr/local/bin/explorer-maintain.sh || true; echo "# explorer-maintain"; echo "*/5 * * * * /usr/local/bin/explorer-maintain.sh >> /var/log/explorer-maintain.log 2>&1"; echo "15 3 * * * RUN_PRUNE=1 /usr/local/bin/explorer-maintain.sh >> /var/log/explorer-maintain.log 2>&1") | crontab -'
echo "✅ Cron installed:"
$EXEC_PREFIX crontab -l 2>/dev/null | grep -E "explorer-maintain|explorer-maintain.sh" || true

# Ensure log file exists and is writable
$EXEC_PREFIX touch /var/log/explorer-maintain.log 2>/dev/null || true
$EXEC_PREFIX chmod 644 /var/log/explorer-maintain.log 2>/dev/null || true

echo ""
echo "=============================================="
echo "Done. Schedule:"
echo "  - Every 5 min: health check + recover Blockscout/nginx"
echo "  - Daily 03:15: safe disk prune (if usage >= 90%)"
echo "  Log: pct exec $VMID -- tail -f /var/log/explorer-maintain.log"
echo "=============================================="
