#!/usr/bin/env bash
# Promove Desk modern UI (staging validado) → produção desk.ligbox.com.br
#
# IMPORTANTE: Agentic Ops (Spec 030) via agentic-ops.js + agentic-ops.css DEVE ir
# no mesmo deploy que index.html / desk-modern.css. Deploy só do "casco" Desk sem
# Agentic deixa a página híbrida (incidente 2026-07-02).
#
# Rollback shell: ./rollback-prod-to-legacy.sh
# Processo: docs/anais-referencia/20260626_DESK_STAGING_MODERN_PROCESS.md
# Agentic: specs/030-agentic-ops-ui/spec.md

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"
REMOTE="/tmp/desk-prod-deploy"
MIN_AGENTIC_JS_LINES=1200

echo "==> Desk modern + Agentic Ops (Spec 030) → PRODUÇÃO"
echo "==> Container: ${CONTAINER}"

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

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" \
  "${HTML}/modules.js" \
  "${HTML}/tickets-sla.js" \
  "${HTML}/tickets-workspace.js" \
  "${HTML}/tickets-workspace.css" \
  "${HTML}/tickets-detail-panel.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.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.js \$C:/usr/share/nginx/html/assets/app.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}/modules.js \$C:/usr/share/nginx/html/assets/modules.js
docker cp ${REMOTE}/tickets-sla.js \$C:/usr/share/nginx/html/assets/tickets-sla.js
docker cp ${REMOTE}/tickets-workspace.js \$C:/usr/share/nginx/html/assets/tickets-workspace.js
docker cp ${REMOTE}/tickets-workspace.css \$C:/usr/share/nginx/html/assets/tickets-workspace.css
docker cp ${REMOTE}/tickets-detail-panel.js \$C:/usr/share/nginx/html/assets/tickets-detail-panel.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 "=== Produção verificada ==="
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/agentic-ops.js && echo "agentic-ops.js OK"
docker exec \$C test -f /usr/share/nginx/html/assets/agentic-ops.css && echo "agentic-ops.css 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 tem \${AGENTIC_LINES} linhas (esperado >= ${MIN_AGENTIC_JS_LINES} — Spec 030)" >&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 "agentic-ops.js contém Squad Command (Spec 030)"
docker exec \$C grep "agentic-ops" /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/index.html 2>/dev/null | tail -1
EOF

echo ""
echo "Produção actualizada: https://desk.ligbox.com.br/?desk=1"
echo "Agentic Ops: view agentic-ops — hard refresh (Ctrl+Shift+R)"
echo "Rollback shell: ./rollback-prod-to-legacy.sh"
