#!/usr/bin/env bash
# Deploy Chain 138 Snap companion site to VMID 5000 at /var/www/html/snap/
# Build first (from repo root): cd metamask-integration/chain138-snap && bash scripts/build-snap-site-for-explorer.sh
# Or set BUILD_FIRST=1 to build from this script (requires pnpm in metamask-integration/chain138-snap).
set -euo pipefail

VMID="${EXPLORER_VMID:-5000}"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Repo root (parent of explorer-monorepo)
REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
SNAP_ROOT="${SNAP_ROOT:-$REPO_ROOT/metamask-integration/chain138-snap}"
SNAP_PUBLIC="${SNAP_PUBLIC:-$SNAP_ROOT/packages/site/public}"
[ -f "$REPO_ROOT/.env" ] && source "$REPO_ROOT/.env" 2>/dev/null || true
[ -f "$REPO_ROOT/config/ip-addresses.conf" ] && source "$REPO_ROOT/config/ip-addresses.conf" 2>/dev/null || true
PROXMOX_R630_02="${EXPLORER_VM_HOST:-root@${PROXMOX_R630_02:-${PROXMOX_HOST_R630_02:-192.168.11.12}}}"
if [[ "$PROXMOX_R630_02" != *"@"* ]]; then PROXMOX_R630_02="root@$PROXMOX_R630_02"; fi

echo "=========================================="
echo "Deploy Snap site to VMID $VMID (/var/www/html/snap/)"
echo "=========================================="

# Optional: build first
if [ "${BUILD_FIRST:-0}" = "1" ]; then
    if [ -d "$SNAP_ROOT" ]; then
        echo "Building Snap site..."
        (cd "$SNAP_ROOT" && bash scripts/build-snap-site-for-explorer.sh)
    else
        echo "❌ SNAP_ROOT not found: $SNAP_ROOT (set BUILD_FIRST=0 and build manually)"
        exit 1
    fi
fi

if [ ! -d "$SNAP_PUBLIC" ] || [ -z "$(ls -A "$SNAP_PUBLIC" 2>/dev/null)" ]; then
    echo "❌ Snap site build not found at $SNAP_PUBLIC"
    echo "   Build first: cd $SNAP_ROOT && bash scripts/build-snap-site-for-explorer.sh"
    echo "   Or run with BUILD_FIRST=1"
    exit 1
fi

# Detect execution context (run from Proxmox host or remote; not from inside VM)
if [ -f "/proc/1/cgroup" ] && grep -q "lxc" /proc/1/cgroup 2>/dev/null; then
    echo "❌ Run this script from the Proxmox host or from your dev machine with EXPLORER_VM_HOST=root@<node-ip>"
    exit 1
fi
if command -v pct &>/dev/null && pct list 2>/dev/null | grep -q "^$VMID "; then
    DEPLOY_METHOD="pct"
    run_in_vm() { pct exec $VMID -- "$@"; }
else
    DEPLOY_METHOD="remote"
    run_in_vm() { ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no "$PROXMOX_R630_02" "pct exec $VMID -- $*"; }
fi

echo "Deploy method: $DEPLOY_METHOD"
run_in_vm "mkdir -p /var/www/html/snap"
run_in_vm "chown -R www-data:www-data /var/www/html/snap" 2>/dev/null || true

TMP_TAR="/tmp/snap-site-$$.tar"
cleanup() { rm -f "$TMP_TAR"; }
trap cleanup EXIT

( cd "$SNAP_PUBLIC" && tar cf "$TMP_TAR" . )

if [ "$DEPLOY_METHOD" = "remote" ]; then
    scp -o ConnectTimeout=10 -o StrictHostKeyChecking=no "$TMP_TAR" "$PROXMOX_R630_02:/tmp/snap-site.tar"
    ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no "$PROXMOX_R630_02" "pct push $VMID /tmp/snap-site.tar /tmp/snap-site.tar --perms 0644 && pct exec $VMID -- bash -c 'cd /var/www/html/snap && tar xf /tmp/snap-site.tar && rm /tmp/snap-site.tar && chown -R www-data:www-data /var/www/html/snap'"
    ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no "$PROXMOX_R630_02" "rm -f /tmp/snap-site.tar"
    echo "✅ Snap site deployed via $PROXMOX_R630_02"
else
    pct push $VMID "$TMP_TAR" /tmp/snap-site.tar
    run_in_vm "bash -c 'cd /var/www/html/snap && tar xf /tmp/snap-site.tar && rm /tmp/snap-site.tar && chown -R www-data:www-data /var/www/html/snap'"
    echo "✅ Snap site deployed"
fi

echo ""
echo "Snap site should be at: https://explorer.d-bis.org/snap/"
echo "Ensure nginx serves /snap/ from /var/www/html/snap/ (see fix-nginx-serve-custom-frontend.sh)."
