#!/usr/bin/env bash
# Promove Desk modern UI (staging validado) → produção desk.ligbox.com.br
# Rollback: index.legacy.html + app.legacy.js no repo

set -euo pipefail

HOST="${1:-root@10.10.10.122}"
PASS="${DESK_SSH_PASS:-805353}"
CONTAINER="ligbox-ops-platform_frontend_1"
ROOT="$(cd "$(dirname "$0")/../../projects/ops-desk/frontend" && pwd)"
HTML="${ROOT}/assets"

echo "==> Desk modern → PRODUÇÃO"
echo "==> Container: ${CONTAINER}"

sshpass -p "${PASS}" ssh -o StrictHostKeyChecking=no "${HOST}" "mkdir -p /tmp/desk-prod-deploy"

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

sshpass -p "${PASS}" ssh -o StrictHostKeyChecking=no "${HOST}" bash -s <<EOF
set -e
C=${CONTAINER}
docker cp /tmp/desk-prod-deploy/index.html \$C:/usr/share/nginx/html/index.html
docker cp /tmp/desk-prod-deploy/topnav.js \$C:/usr/share/nginx/html/assets/topnav.js
docker cp /tmp/desk-prod-deploy/desk-modern.css \$C:/usr/share/nginx/html/assets/desk-modern.css
docker cp /tmp/desk-prod-deploy/app.js \$C:/usr/share/nginx/html/assets/app.js
docker cp /tmp/desk-prod-deploy/styles.css \$C:/usr/share/nginx/html/assets/styles.css
docker cp /tmp/desk-prod-deploy/servicos.js \$C:/usr/share/nginx/html/assets/servicos.js
docker cp /tmp/desk-prod-deploy/auth.js \$C:/usr/share/nginx/html/assets/auth.js
echo "=== Produção verificada ==="
docker exec \$C grep "20260626prod1" /usr/share/nginx/html/index.html | head -3
docker exec \$C grep -c "navigateScope\\|shell--v2\\|DeskTopnav" /usr/share/nginx/html/assets/app.js /usr/share/nginx/html/assets/servicos.js /usr/share/nginx/html/index.html 2>/dev/null | tail -1
docker exec \$C test -f /usr/share/nginx/html/assets/desk-modern.css && echo "desk-modern.css OK"
EOF

echo ""
echo "Produção actualizada: https://desk.ligbox.com.br/?desk=1"
echo "Rollback: ./rollback-prod-to-legacy.sh"
