#!/usr/bin/env bash
# Desk modern UI — deploy APENAS staging (desk-staging.ligbox.com.br)
# NÃO toca em ligbox-ops-platform_frontend_1 (produção desk.ligbox.com.br)
#
# Inclui agentic-ops.js/css (Spec 030) — mesmo pacote que promote-staging-to-prod.sh
#
# Uso: ./deploy-staging-only.sh [VM122_HOST]
# Spec / processo: docs/anais-referencia/20260626_DESK_STAGING_MODERN_PROCESS.md

set -euo pipefail

HOST="${1:-root@10.10.10.122}"
PASS="${DESK_SSH_PASS:-805353}"
CONTAINER="ligbox-ops-platform-staging_frontend-staging_1"
ROOT="$(cd "$(dirname "$0")/../../projects/ops-desk/frontend" && pwd)"
HTML="${ROOT}/assets"
REMOTE="/tmp/desk-staging-deploy"
MIN_AGENTIC_JS_LINES=1200

echo "==> Desk staging modern + Agentic Ops — fonte: ${ROOT}"
echo "==> Container: ${CONTAINER} (NÃO produção)"

sshpass -p "${PASS}" ssh -o StrictHostKeyChecking=no "${HOST}" "mkdir -p ${REMOTE}"

sshpass -p "${PASS}" scp -o StrictHostKeyChecking=no \
  "${ROOT}/index.staging.html" \
  "${HTML}/topnav.js" \
  "${HTML}/desk-modern.css" \
  "${HTML}/app.staging.js" \
  "${HTML}/styles.css" \
  "${HTML}/servicos.js" \
  "${HTML}/auth.js" \
  "${HTML}/agentic-ops.js" \
  "${HTML}/agentic-ops.css" \
  "${HOST}:${REMOTE}/"

sshpass -p "${PASS}" ssh -o StrictHostKeyChecking=no "${HOST}" bash -s <<EOF
set -e
C=${CONTAINER}
docker cp ${REMOTE}/index.staging.html \$C:/usr/share/nginx/html/index.html
docker cp ${REMOTE}/topnav.js \$C:/usr/share/nginx/html/assets/topnav.js
docker cp ${REMOTE}/desk-modern.css \$C:/usr/share/nginx/html/assets/desk-modern.css
docker cp ${REMOTE}/app.staging.js \$C:/usr/share/nginx/html/assets/app.staging.js
docker cp ${REMOTE}/styles.css \$C:/usr/share/nginx/html/assets/styles.css
docker cp ${REMOTE}/servicos.js \$C:/usr/share/nginx/html/assets/servicos.js
docker cp ${REMOTE}/auth.js \$C:/usr/share/nginx/html/assets/auth.js
docker cp ${REMOTE}/agentic-ops.js \$C:/usr/share/nginx/html/assets/agentic-ops.js
docker cp ${REMOTE}/agentic-ops.css \$C:/usr/share/nginx/html/assets/agentic-ops.css

echo "=== Verificação staging ==="
docker exec \$C test -f /usr/share/nginx/html/assets/desk-modern.css && echo "desk-modern.css OK"
docker exec \$C test -f /usr/share/nginx/html/assets/app.staging.js && echo "app.staging.js OK"
docker exec \$C test -f /usr/share/nginx/html/assets/agentic-ops.js && echo "agentic-ops.js OK"

AGENTIC_LINES=\$(docker exec \$C wc -l < /usr/share/nginx/html/assets/agentic-ops.js | tr -d ' ')
if [[ "\${AGENTIC_LINES}" -lt ${MIN_AGENTIC_JS_LINES} ]]; then
  echo "ERRO: agentic-ops.js staging tem \${AGENTIC_LINES} linhas (esperado >= ${MIN_AGENTIC_JS_LINES})" >&2
  exit 1
fi
echo "agentic-ops.js linhas: \${AGENTIC_LINES} (Spec 030 OK)"
docker exec \$C grep -q "Squad Command" /usr/share/nginx/html/assets/agentic-ops.js && echo "Squad Command presente"
docker exec \$C grep -c "navigateScope\\|PURGE_BLOCKLIST" /usr/share/nginx/html/assets/servicos.js
EOF

echo ""
echo "Deploy staging concluído."
echo "Testar: https://desk-staging.ligbox.com.br/?desk=1 (Ctrl+Shift+R)"
echo "Produção desk.ligbox.com.br NÃO foi alterada."
