#!/bin/bash
set -euo pipefail
REMOTE="aissa@10.4.7.18"
PASS='Foodlinkk#2026'
SRC="/tmp/foodlinkk-deploy"
DEST="~/foodlinkk-command-center"

sshpass -p "$PASS" rsync -avz \
  "$SRC/cockpit/templates/base.html" \
  "$SRC/cockpit/templates/dashboard.html" \
  "$SRC/cockpit/templates/cto-dashboard.html" \
  "$REMOTE:$DEST/cockpit/templates/"

sshpass -p "$PASS" rsync -avz \
  "$SRC/cockpit/app/routes/dashboard.py" \
  "$REMOTE:$DEST/cockpit/app/routes/"

sshpass -p "$PASS" rsync -avz \
  "$SRC/cockpit/static/css/vertical-tabs.css" \
  "$REMOTE:$DEST/cockpit/static/css/"

sshpass -p "$PASS" scp -o StrictHostKeyChecking=no \
  "$SRC/cockpit/static/i18n/nl.json" \
  "$SRC/cockpit/static/i18n/en.json" \
  "$SRC/cockpit/static/manifest.json" \
  "$REMOTE:$DEST/cockpit/static/i18n/" 2>/dev/null || true

sshpass -p "$PASS" scp -o StrictHostKeyChecking=no \
  "$SRC/cockpit/static/manifest.json" \
  "$REMOTE:$DEST/cockpit/static/manifest.json"

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

echo "CEO/CTO split deployed."
