#!/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)
#
# 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"

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

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" \
  "${HOST}:/tmp/desk-staging-deploy/"

sshpass -p "${PASS}" ssh -o StrictHostKeyChecking=no "${HOST}" bash -s <<EOF
set -e
C=${CONTAINER}
docker cp /tmp/desk-staging-deploy/index.staging.html \$C:/usr/share/nginx/html/index.html
docker cp /tmp/desk-staging-deploy/topnav.js \$C:/usr/share/nginx/html/assets/topnav.js
docker cp /tmp/desk-staging-deploy/desk-modern.css \$C:/usr/share/nginx/html/assets/desk-modern.css
docker cp /tmp/desk-staging-deploy/app.staging.js \$C:/usr/share/nginx/html/assets/app.staging.js
docker cp /tmp/desk-staging-deploy/styles.css \$C:/usr/share/nginx/html/assets/styles.css
docker cp /tmp/desk-staging-deploy/servicos.js \$C:/usr/share/nginx/html/assets/servicos.js
docker cp /tmp/desk-staging-deploy/auth.js \$C:/usr/share/nginx/html/assets/auth.js
echo "=== Verificação staging ==="
docker exec \$C grep "20260626staging1" /usr/share/nginx/html/index.html | head -3
docker exec \$C grep -c "navigateScope\\|PURGE_BLOCKLIST" /usr/share/nginx/html/assets/servicos.js
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"
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."
