#!/usr/bin/env bash
# Deploy explorer config (token list, networks, capabilities) to VMID 5000 for /api/config/* endpoints.
# Run from repo root. Requires Proxmox host access (pct exec) or SSH to explorer container.
#
# Usage:
#   From Proxmox host:  pct exec 5000 -- bash -c 'mkdir -p /var/www/html/config'
#   Then: ./scripts/deploy-explorer-config-to-vmid5000.sh
#
#   From a workstation that can SSH to the Proxmox host:
#   PROXMOX_HOST=192.168.11.12 ./scripts/deploy-explorer-config-to-vmid5000.sh
#
# Or run inside VMID 5000:
#   pct push 5000 /path/to/DUAL_CHAIN_TOKEN_LIST.tokenlist.json /var/www/html/config/
#   pct push 5000 /path/to/DUAL_CHAIN_NETWORKS.json /var/www/html/config/
#   pct push 5000 /path/to/CHAIN138_RPC_CAPABILITIES.json /var/www/html/config/

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
CONFIG_SRC="$REPO_ROOT/explorer-monorepo/backend/api/rest/config/metamask"
TOPOLOGY_SRC="$REPO_ROOT/explorer-monorepo/frontend/public/config/topology-graph.json"
VERIFY_EXAMPLE_SRC="$REPO_ROOT/explorer-monorepo/frontend/public/config/mission-control-verify.example.json"
TOKEN_ICONS_SRC="$REPO_ROOT/explorer-monorepo/frontend/public/token-icons"
VMID="${EXPLORER_VMID:-5000}"
PROXMOX_HOST="${PROXMOX_HOST:-192.168.11.12}"
EXEC_MODE="${EXEC_MODE:-pct}"
SSH_OPTS=(-o BatchMode=yes -o StrictHostKeyChecking=no)

deploy_via_pct() {
  pct exec "$VMID" -- mkdir -p /var/www/html/config /var/www/html/token-icons
  pct push "$VMID" "$CONFIG_SRC/DUAL_CHAIN_TOKEN_LIST.tokenlist.json" /var/www/html/config/DUAL_CHAIN_TOKEN_LIST.tokenlist.json
  pct push "$VMID" "$CONFIG_SRC/DUAL_CHAIN_NETWORKS.json" /var/www/html/config/DUAL_CHAIN_NETWORKS.json
  pct push "$VMID" "$CONFIG_SRC/CHAIN138_RPC_CAPABILITIES.json" /var/www/html/config/CHAIN138_RPC_CAPABILITIES.json
  if [ -f "$CONFIG_SRC/GRU_V2_PUBLIC_DEPLOYMENT_STATUS.json" ]; then
    pct push "$VMID" "$CONFIG_SRC/GRU_V2_PUBLIC_DEPLOYMENT_STATUS.json" /var/www/html/config/GRU_V2_PUBLIC_DEPLOYMENT_STATUS.json
  fi
  if [ -f "$CONFIG_SRC/GRU_V2_DEPLOYMENT_QUEUE.json" ]; then
    pct push "$VMID" "$CONFIG_SRC/GRU_V2_DEPLOYMENT_QUEUE.json" /var/www/html/config/GRU_V2_DEPLOYMENT_QUEUE.json
  fi
  if [ -f "$TOPOLOGY_SRC" ]; then
    pct push "$VMID" "$TOPOLOGY_SRC" /var/www/html/config/topology-graph.json
  fi
  if [ -f "$VERIFY_EXAMPLE_SRC" ]; then
    pct push "$VMID" "$VERIFY_EXAMPLE_SRC" /var/www/html/config/mission-control-verify.example.json
  fi
  if [ -d "$TOKEN_ICONS_SRC" ]; then
    for icon in "$TOKEN_ICONS_SRC"/*.png; do
      [ -f "$icon" ] || continue
      pct push "$VMID" "$icon" "/var/www/html/token-icons/$(basename "$icon")"
    done
  fi
}

deploy_via_remote_pct() {
  local remote_tmp
  remote_tmp="/tmp/explorer-config-${VMID}-$$"
  ssh "${SSH_OPTS[@]}" "root@$PROXMOX_HOST" "mkdir -p '$remote_tmp'"
  scp "${SSH_OPTS[@]}" "$CONFIG_SRC/DUAL_CHAIN_TOKEN_LIST.tokenlist.json" "root@$PROXMOX_HOST:$remote_tmp/DUAL_CHAIN_TOKEN_LIST.tokenlist.json"
  scp "${SSH_OPTS[@]}" "$CONFIG_SRC/DUAL_CHAIN_NETWORKS.json" "root@$PROXMOX_HOST:$remote_tmp/DUAL_CHAIN_NETWORKS.json"
  scp "${SSH_OPTS[@]}" "$CONFIG_SRC/CHAIN138_RPC_CAPABILITIES.json" "root@$PROXMOX_HOST:$remote_tmp/CHAIN138_RPC_CAPABILITIES.json"
  if [ -f "$CONFIG_SRC/GRU_V2_PUBLIC_DEPLOYMENT_STATUS.json" ]; then
    scp "${SSH_OPTS[@]}" "$CONFIG_SRC/GRU_V2_PUBLIC_DEPLOYMENT_STATUS.json" "root@$PROXMOX_HOST:$remote_tmp/GRU_V2_PUBLIC_DEPLOYMENT_STATUS.json"
  fi
  if [ -f "$CONFIG_SRC/GRU_V2_DEPLOYMENT_QUEUE.json" ]; then
    scp "${SSH_OPTS[@]}" "$CONFIG_SRC/GRU_V2_DEPLOYMENT_QUEUE.json" "root@$PROXMOX_HOST:$remote_tmp/GRU_V2_DEPLOYMENT_QUEUE.json"
  fi
  if [ -f "$TOPOLOGY_SRC" ]; then
    scp "${SSH_OPTS[@]}" "$TOPOLOGY_SRC" "root@$PROXMOX_HOST:$remote_tmp/topology-graph.json"
  fi
  if [ -f "$VERIFY_EXAMPLE_SRC" ]; then
    scp "${SSH_OPTS[@]}" "$VERIFY_EXAMPLE_SRC" "root@$PROXMOX_HOST:$remote_tmp/mission-control-verify.example.json"
  fi
  if [ -d "$TOKEN_ICONS_SRC" ]; then
    for icon in "$TOKEN_ICONS_SRC"/*.png; do
      [ -f "$icon" ] || continue
      scp "${SSH_OPTS[@]}" "$icon" "root@$PROXMOX_HOST:$remote_tmp/$(basename "$icon")"
    done
  fi
  ssh "${SSH_OPTS[@]}" "root@$PROXMOX_HOST" "\
    pct exec '$VMID' -- mkdir -p /var/www/html/config /var/www/html/token-icons && \
    pct push '$VMID' '$remote_tmp/DUAL_CHAIN_TOKEN_LIST.tokenlist.json' /var/www/html/config/DUAL_CHAIN_TOKEN_LIST.tokenlist.json && \
    pct push '$VMID' '$remote_tmp/DUAL_CHAIN_NETWORKS.json' /var/www/html/config/DUAL_CHAIN_NETWORKS.json && \
    pct push '$VMID' '$remote_tmp/CHAIN138_RPC_CAPABILITIES.json' /var/www/html/config/CHAIN138_RPC_CAPABILITIES.json \
    $(if [ -f "$CONFIG_SRC/GRU_V2_PUBLIC_DEPLOYMENT_STATUS.json" ]; then printf "%s" "&& pct push '$VMID' '$remote_tmp/GRU_V2_PUBLIC_DEPLOYMENT_STATUS.json' /var/www/html/config/GRU_V2_PUBLIC_DEPLOYMENT_STATUS.json "; fi) \
    $(if [ -f "$CONFIG_SRC/GRU_V2_DEPLOYMENT_QUEUE.json" ]; then printf "%s" "&& pct push '$VMID' '$remote_tmp/GRU_V2_DEPLOYMENT_QUEUE.json' /var/www/html/config/GRU_V2_DEPLOYMENT_QUEUE.json "; fi) \
    $(if [ -f "$TOPOLOGY_SRC" ]; then printf "%s" "&& pct push '$VMID' '$remote_tmp/topology-graph.json' /var/www/html/config/topology-graph.json "; fi) \
    $(if [ -f "$VERIFY_EXAMPLE_SRC" ]; then printf "%s" "&& pct push '$VMID' '$remote_tmp/mission-control-verify.example.json' /var/www/html/config/mission-control-verify.example.json "; fi) \
    $(if [ -d "$TOKEN_ICONS_SRC" ]; then for icon in "$TOKEN_ICONS_SRC"/*.png; do [ -f "$icon" ] || continue; printf "%s" "&& pct push '$VMID' '$remote_tmp/$(basename "$icon")' /var/www/html/token-icons/$(basename "$icon") "; done; fi) && \
    rm -rf '$remote_tmp'"
}

if [ ! -f "$CONFIG_SRC/DUAL_CHAIN_TOKEN_LIST.tokenlist.json" ]; then
  echo "Error: Token list not found at $CONFIG_SRC/DUAL_CHAIN_TOKEN_LIST.tokenlist.json" >&2
  exit 1
fi
if [ ! -f "$CONFIG_SRC/DUAL_CHAIN_NETWORKS.json" ]; then
  echo "Error: Networks config not found at $CONFIG_SRC/DUAL_CHAIN_NETWORKS.json" >&2
  exit 1
fi
if [ ! -f "$CONFIG_SRC/CHAIN138_RPC_CAPABILITIES.json" ]; then
  echo "Error: Capabilities config not found at $CONFIG_SRC/CHAIN138_RPC_CAPABILITIES.json" >&2
  exit 1
fi

echo "Deploying explorer config to VMID $VMID..."
echo "  Token list: $CONFIG_SRC/DUAL_CHAIN_TOKEN_LIST.tokenlist.json"
echo "  Networks:   $CONFIG_SRC/DUAL_CHAIN_NETWORKS.json"
echo "  Capabilities: $CONFIG_SRC/CHAIN138_RPC_CAPABILITIES.json"
if [ -f "$CONFIG_SRC/GRU_V2_PUBLIC_DEPLOYMENT_STATUS.json" ]; then
  echo "  GRU status: $CONFIG_SRC/GRU_V2_PUBLIC_DEPLOYMENT_STATUS.json"
fi
if [ -f "$CONFIG_SRC/GRU_V2_DEPLOYMENT_QUEUE.json" ]; then
  echo "  GRU queue:  $CONFIG_SRC/GRU_V2_DEPLOYMENT_QUEUE.json"
fi
if [ -d "$TOKEN_ICONS_SRC" ]; then
  echo "  Token icons: $TOKEN_ICONS_SRC/*.png"
fi
echo ""

case "$EXEC_MODE" in
  pct)
    if command -v pct &>/dev/null; then
      deploy_via_pct
      echo "Done. Verify: curl -s https://explorer.d-bis.org/api/config/capabilities | jq '.chainId'"
    elif ssh "${SSH_OPTS[@]}" "root@$PROXMOX_HOST" "command -v pct" >/dev/null 2>&1; then
      deploy_via_remote_pct
      echo "Done. Verify: curl -s https://explorer.d-bis.org/api/config/capabilities | jq '.chainId'"
    else
      echo "pct not available locally, and remote pct on $PROXMOX_HOST was not reachable."
      echo "Use EXEC_MODE=ssh or run from the Proxmox host:"
      echo "  ssh root@$PROXMOX_HOST 'pct exec $VMID -- mkdir -p /var/www/html/config'"
      exit 1
    fi
    ;;
  ssh)
    CONTAINER_IP="${EXPLORER_IP:-192.168.11.140}"
    TMP_DIR=$(mktemp -d)
    trap "rm -rf $TMP_DIR" EXIT
    cp "$CONFIG_SRC/DUAL_CHAIN_TOKEN_LIST.tokenlist.json" "$TMP_DIR/"
    cp "$CONFIG_SRC/DUAL_CHAIN_NETWORKS.json" "$TMP_DIR/"
    cp "$CONFIG_SRC/CHAIN138_RPC_CAPABILITIES.json" "$TMP_DIR/"
    if [ -f "$CONFIG_SRC/GRU_V2_PUBLIC_DEPLOYMENT_STATUS.json" ]; then
      cp "$CONFIG_SRC/GRU_V2_PUBLIC_DEPLOYMENT_STATUS.json" "$TMP_DIR/"
    fi
    if [ -f "$CONFIG_SRC/GRU_V2_DEPLOYMENT_QUEUE.json" ]; then
      cp "$CONFIG_SRC/GRU_V2_DEPLOYMENT_QUEUE.json" "$TMP_DIR/"
    fi
    if [ -d "$TOKEN_ICONS_SRC" ]; then
      cp "$TOKEN_ICONS_SRC"/*.png "$TMP_DIR/" 2>/dev/null || true
    fi
    ssh "root@$CONTAINER_IP" "mkdir -p /var/www/html/config /var/www/html/token-icons"
    scp "$TMP_DIR/DUAL_CHAIN_TOKEN_LIST.tokenlist.json" "root@$CONTAINER_IP:/var/www/html/config/" 2>/dev/null || {
      echo "SSH to $CONTAINER_IP failed. Ensure config dir exists: ssh root@$CONTAINER_IP 'mkdir -p /var/www/html/config'"
      exit 1
    }
    scp "$TMP_DIR/DUAL_CHAIN_NETWORKS.json" "root@$CONTAINER_IP:/var/www/html/config/"
    scp "$TMP_DIR/CHAIN138_RPC_CAPABILITIES.json" "root@$CONTAINER_IP:/var/www/html/config/"
    if [ -f "$TMP_DIR/GRU_V2_PUBLIC_DEPLOYMENT_STATUS.json" ]; then
      scp "$TMP_DIR/GRU_V2_PUBLIC_DEPLOYMENT_STATUS.json" "root@$CONTAINER_IP:/var/www/html/config/"
    fi
    if [ -f "$TMP_DIR/GRU_V2_DEPLOYMENT_QUEUE.json" ]; then
      scp "$TMP_DIR/GRU_V2_DEPLOYMENT_QUEUE.json" "root@$CONTAINER_IP:/var/www/html/config/"
    fi
    if [ -f "$TOPOLOGY_SRC" ]; then
      scp "$TOPOLOGY_SRC" "root@$CONTAINER_IP:/var/www/html/config/topology-graph.json"
    fi
    if [ -f "$VERIFY_EXAMPLE_SRC" ]; then
      scp "$VERIFY_EXAMPLE_SRC" "root@$CONTAINER_IP:/var/www/html/config/mission-control-verify.example.json"
    fi
    if [ -d "$TOKEN_ICONS_SRC" ]; then
      for icon in "$TMP_DIR"/*.png; do
        [ -f "$icon" ] || continue
        scp "$icon" "root@$CONTAINER_IP:/var/www/html/token-icons/"
      done
    fi
    echo "Done. Verify: curl -s https://explorer.d-bis.org/api/config/capabilities | jq '.chainId'"
    ;;
  *)
    echo "Unknown EXEC_MODE=$EXEC_MODE. Use pct or ssh." >&2
    exit 1
    ;;
esac
