#!/bin/bash
set -e
HOST="aissa@10.4.7.18"
PASS='Foodlinkk#2026'
BASE="/tmp/foodlinkk-deploy"
R="~/foodlinkk-command-center"

echo "=== Herman neo + Agents souls + Permissions ==="

sshpass -p "$PASS" scp -o StrictHostKeyChecking=no \
  "$BASE/migrations/013_agent_souls_permissions.sql" "$HOST:$R/migrations/"

sshpass -p "$PASS" scp -o StrictHostKeyChecking=no \
  "$BASE/cockpit/app/services/agent_souls.py" "$HOST:$R/cockpit/app/services/"

sshpass -p "$PASS" scp -o StrictHostKeyChecking=no \
  "$BASE/cockpit/app/routes/agents_api.py" \
  "$BASE/cockpit/app/routes/settings_api.py" \
  "$BASE/cockpit/app/routes/settings.py" \
  "$HOST:$R/cockpit/app/routes/"

sshpass -p "$PASS" scp -o StrictHostKeyChecking=no \
  "$BASE/cockpit/templates/dashboard.html" \
  "$BASE/cockpit/templates/agents.html" \
  "$BASE/cockpit/templates/settings.html" \
  "$HOST:$R/cockpit/templates/"

sshpass -p "$PASS" scp -o StrictHostKeyChecking=no \
  "$BASE/cockpit/static/css/herman-dashboard.css" \
  "$BASE/cockpit/static/css/pulse-theme.css" \
  "$BASE/cockpit/static/css/hermes.css" \
  "$HOST:$R/cockpit/static/css/"

sshpass -p "$PASS" scp -o StrictHostKeyChecking=no \
  "$BASE/cockpit/static/js/briefing-charts.js" "$HOST:$R/cockpit/static/js/"

sshpass -p "$PASS" ssh -o StrictHostKeyChecking=no "$HOST" bash <<'REMOTE'
set -e
cd ~/foodlinkk-command-center

# Register agents_api router
if ! grep -q agents_api ~/foodlinkk-command-center/cockpit/app/main.py; then
  sed -i '/from app.routes.settings_api import settings_router/a from app.routes.agents_api import router as agents_api_router' cockpit/app/main.py
  sed -i '/settings_router,/a\    agents_api_router,' cockpit/app/main.py
fi

docker exec -i foodlinkk_db psql -U aissa -d foodlinkk < migrations/013_agent_souls_permissions.sql

docker compose build cockpit
docker compose up -d cockpit
sleep 6

curl -sf -o /dev/null -w "dashboard: %{http_code}\n" http://localhost:8600/
curl -sf -o /dev/null -w "agents: %{http_code}\n" http://localhost:8600/agents
curl -sf -o /dev/null -w "settings perms: %{http_code}\n" "http://localhost:8600/settings?tab=permissions"
curl -sf http://localhost:8600/api/agents/souls | head -c 150
echo ""
curl -sf http://localhost:8600/api/settings/permissions | head -c 150
echo ""
REMOTE

sshpass -p "$PASS" ssh -o StrictHostKeyChecking=no aissa@10.4.7.19 \
  'python3 ~/foodlinkk-ai/hermes/notify.py --team "Herman neo UI: KPI rings zoals Hermes, groot veld vast. Agents soul.md registry + Settings rechten Herman. Hard refresh Ctrl+Shift+R"'

echo DONE
