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

echo "=== Deploying 360 upgrade to VM 106 ==="

# Tools API
sshpass -p "$PASS" scp -o StrictHostKeyChecking=no \
  "$BASE/tools-api/app/retail_360.py" \
  "$BASE/tools-api/app/retail_360_routes.py" \
  "$BASE/tools-api/app/wholesaler_scrapers.py" \
  "$BASE/tools-api/app/connectors/rss_feeds.py" \
  "$HOST:$REMOTE/tools-api/app/"

sshpass -p "$PASS" scp -o StrictHostKeyChecking=no \
  "$BASE/tools-api/app/connectors/rss_feeds.py" \
  "$HOST:$REMOTE/tools-api/app/connectors/"

# Migration
sshpass -p "$PASS" scp -o StrictHostKeyChecking=no \
  "$BASE/migrations/011_retail_360.sql" \
  "$HOST:$REMOTE/migrations/"

# Cockpit
sshpass -p "$PASS" scp -o StrictHostKeyChecking=no \
  "$BASE/cockpit/templates/retail.html" \
  "$BASE/cockpit/templates/marketing.html" \
  "$HOST:$REMOTE/cockpit/templates/"

sshpass -p "$PASS" scp -o StrictHostKeyChecking=no \
  "$BASE/cockpit/static/retail.css" \
  "$HOST:$REMOTE/cockpit/static/"

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

sshpass -p "$PASS" scp -o StrictHostKeyChecking=no \
  "$BASE/cockpit/app/routes/retail.py" \
  "$HOST:$REMOTE/cockpit/app/routes/"

echo "=== Patching main.py and base.html ==="
sshpass -p "$PASS" ssh -o StrictHostKeyChecking=no "$HOST" bash <<'REMOTE'
set -e
cd ~/foodlinkk-command-center

# Add retail_360 router to tools-api main.py
if ! grep -q retail_360_routes ~/foodlinkk-command-center/tools-api/app/main.py; then
  sed -i '/from app.retail import router as retail_router/a from app.retail_360_routes import router as retail_360_router' tools-api/app/main.py
  sed -i '/app.include_router(retail_router)/a app.include_router(retail_360_router)' tools-api/app/main.py
fi

# Add pulse-theme.css to base.html
if ! grep -q pulse-theme.css ~/foodlinkk-command-center/cockpit/templates/base.html; then
  sed -i 's|palantir-theme.css|palantir-theme.css" />\n  <link rel="stylesheet" href="/static/css/pulse-theme.css|' cockpit/templates/base.html
fi

echo "=== Running migration 011 ==="
docker exec -i foodlinkk_db psql -U aissa -d foodlinkk < migrations/011_retail_360.sql

echo "=== Rebuilding containers ==="
docker compose build tools-api cockpit
docker compose up -d tools-api cockpit

echo "=== Waiting for services ==="
sleep 8

echo "=== Health checks ==="
curl -sf http://localhost:8700/retail/stats | head -c 200
echo ""
curl -sf -o /dev/null -w "retail page: %{http_code}\n" http://localhost:8600/retail
curl -sf -o /dev/null -w "marketing page: %{http_code}\n" http://localhost:8600/marketing
curl -sf -o /dev/null -w "360 endpoint: %{http_code}\n" http://localhost:8700/retail/360/1
curl -sf -o /dev/null -w "live-dashboard: %{http_code}\n" http://localhost:8700/retail/live-dashboard

echo "=== RSS refresh ==="
curl -sf -X POST http://localhost:8700/retail/rss/refresh | head -c 300
echo ""

echo "=== Wholesaler import (background) ==="
nohup curl -sf -X POST http://localhost:8700/retail/wholesalers/import > /tmp/wholesale-import.log 2>&1 &
echo "Started wholesaler import in background"

echo "=== City demographics sync ==="
curl -sf -X POST "http://localhost:8700/retail/cities/sync?limit=30" | head -c 200
echo ""

echo "=== DONE ==="
REMOTE

echo "=== Notify team ==="
sshpass -p "$PASS" ssh -o StrictHostKeyChecking=no aissa@10.4.7.19 \
  'python3 ~/foodlinkk-ai/hermes/notify.py --team "Retail 360 upgrade live: pulse UI, notes/media/milestones/ownership, RSS feeds, groothandels, stad demografie, marketing hub uitgebreid. Bekijk http://10.4.7.18:8600/retail"'
