#!/bin/bash
set -euo pipefail
HOST106="aissa@10.4.7.18"
HOST19="aissa@10.4.7.19"
PASS='Foodlinkk#2026'
BASE="/tmp/foodlinkk-deploy"

echo "==> Migration 033 on VM106"
sshpass -p "$PASS" scp -o StrictHostKeyChecking=no \
  "$BASE/migrations/033_revenue_cockpit.sql" "$HOST106:~/foodlinkk-command-center/migrations/"
sshpass -p "$PASS" ssh -o StrictHostKeyChecking=no "$HOST106" \
  'docker exec -i foodlinkk_db psql -U aissa -d foodlinkk < ~/foodlinkk-command-center/migrations/033_revenue_cockpit.sql'

echo "==> Deploy cockpit Revenue Cockpit"
sshpass -p "$PASS" scp -o StrictHostKeyChecking=no \
  "$BASE/cockpit/app/main.py" \
  "$HOST106:~/foodlinkk-command-center/cockpit/app/main.py"

sshpass -p "$PASS" scp -o StrictHostKeyChecking=no \
  "$BASE/cockpit/app/services/revenue_cockpit.py" \
  "$HOST106:~/foodlinkk-command-center/cockpit/app/services/revenue_cockpit.py"

sshpass -p "$PASS" scp -o StrictHostKeyChecking=no \
  "$BASE/cockpit/app/routes/revenue_cockpit.py" \
  "$HOST106:~/foodlinkk-command-center/cockpit/app/routes/revenue_cockpit.py"

sshpass -p "$PASS" scp -o StrictHostKeyChecking=no \
  "$BASE/cockpit/templates/revenue_cockpit.html" \
  "$BASE/cockpit/templates/base.html" \
  "$HOST106:~/foodlinkk-command-center/cockpit/templates/"

sshpass -p "$PASS" scp -o StrictHostKeyChecking=no \
  "$BASE/cockpit/static/css/revenue-cockpit.css" \
  "$HOST106:~/foodlinkk-command-center/cockpit/static/css/revenue-cockpit.css"

sshpass -p "$PASS" scp -o StrictHostKeyChecking=no \
  "$BASE/cockpit/static/js/revenue-cockpit.js" \
  "$HOST106:~/foodlinkk-command-center/cockpit/static/js/revenue-cockpit.js"

sshpass -p "$PASS" scp -o StrictHostKeyChecking=no \
  "$BASE/cockpit/static/i18n/nl.json" \
  "$BASE/cockpit/static/i18n/en.json" \
  "$HOST106:~/foodlinkk-command-center/cockpit/static/i18n/"

sshpass -p "$PASS" ssh -o StrictHostKeyChecking=no "$HOST106" 'bash -s' <<'REMOTE'
set -e
cd ~/foodlinkk-command-center
docker compose build cockpit
docker compose up -d cockpit
sleep 6
curl -sf -o /dev/null -w "revenue:%{http_code}\n" http://localhost:8600/revenue-cockpit
curl -sf -o /dev/null -w "api:%{http_code}\n" http://localhost:8600/api/revenue-cockpit/dashboard
REMOTE

echo "==> Deploy excel parser to doc-ingest VM19"
sshpass -p "$PASS" ssh -o StrictHostKeyChecking=no "$HOST19" 'mkdir -p ~/foodlinkk-ai/doc-ingest/app'
sshpass -p "$PASS" scp -o StrictHostKeyChecking=no \
  "$BASE/doc-ingest/app/excel_service.py" \
  "$BASE/doc-ingest/app/excel_routes.py" \
  "$HOST19:~/foodlinkk-ai/doc-ingest/app/"

sshpass -p "$PASS" ssh -o StrictHostKeyChecking=no "$HOST19" 'bash -s' <<'REMOTE19'
set -e
cd ~/foodlinkk-ai/doc-ingest
grep -q openpyxl requirements.txt 2>/dev/null || echo 'openpyxl>=3.1.0' >> requirements.txt
python3 <<'PY'
from pathlib import Path
p = Path("app/main.py")
text = p.read_text()
if "excel_routes" not in text:
    if "from app.docling_routes import" in text:
        text = text.replace(
            "from app.docling_routes import router as docling_router",
            "from app.docling_routes import router as docling_router\nfrom app.excel_routes import router as excel_router",
        )
    else:
        text = "from app.excel_routes import router as excel_router\n" + text
    if "excel_router" not in text:
        text = text.replace(
            "app.include_router(docling_router)",
            "app.include_router(docling_router)\napp.include_router(excel_router)",
        )
    p.write_text(text)
    print("patched main.py for excel_router")
PY
docker compose up -d --build 2>&1 | tail -15
sleep 4
curl -sf "http://localhost:8750/excel/revenue-sheet/preview" | head -c 200
echo
REMOTE19

echo "==> Bootstrap import"
curl -sf -X POST http://10.4.7.18:8600/api/revenue-cockpit/import-from-excel \
  -H 'Content-Type: application/json' \
  -d '{"replace":true}' | head -c 300
echo
echo "DONE — http://10.4.7.18:8600/revenue-cockpit"
