#!/usr/bin/env bash
# Foodlinkk Command Center — volledige deploy (migraties + build + health)
set -euo pipefail

PASS="${FOODLINKK_SSH_PASS:-Foodlinkk#2026}"
VM="${FOODLINKK_VM:-aissa@10.4.7.18}"
REMOTE_DIR="${FOODLINKK_REMOTE_DIR:-/home/aissa/foodlinkk-command-center}"
LOCAL_DIR="$(cd "$(dirname "$0")" && pwd)"

echo "=== Foodlinkk deploy-all ==="
echo "Local:  $LOCAL_DIR"
echo "Remote: $VM:$REMOTE_DIR"

if ! command -v sshpass >/dev/null 2>&1; then
  echo "sshpass is vereist (apt install sshpass)"
  exit 1
fi

echo "=== Rsync naar VM106 ==="
sshpass -p "$PASS" rsync -az --delete \
  --exclude '.git' \
  --exclude '__pycache__' \
  --exclude '*.pyc' \
  --exclude '.env' \
  --exclude 'node_modules' \
  -e "ssh -o StrictHostKeyChecking=no" \
  "$LOCAL_DIR/" "$VM:$REMOTE_DIR/"

echo "=== Migraties + Docker rebuild op VM106 ==="
sshpass -p "$PASS" ssh -o StrictHostKeyChecking=no "$VM" bash -s <<REMOTE
set -euo pipefail
cd "$REMOTE_DIR"

for mig in migrations/*.sql; do
  echo "Applying \$(basename "\$mig")..."
  docker exec -i foodlinkk_db psql -U aissa -d foodlinkk < "\$mig" || true
done

docker compose build tools-api cockpit email-agent browser-agent
docker compose up -d

echo "Waiting for services..."
sleep 15

curl -sf http://127.0.0.1:8700/health && echo " tools-api OK" || echo " tools-api FAIL"
curl -sf -o /dev/null -w "cockpit:%{http_code}\n" http://127.0.0.1:8600/
curl -sf -o /dev/null -w "marketing:%{http_code}\n" http://127.0.0.1:8600/marketing
curl -sf -o /dev/null -w "ops:%{http_code}\n" http://127.0.0.1:8600/ops
curl -sf -o /dev/null -w "packaging:%{http_code}\n" http://127.0.0.1:8600/packaging
curl -sf -o /dev/null -w "agents:%{http_code}\n" http://127.0.0.1:8600/agents

echo "=== NAS client folders root ==="
mkdir -p /home/aissa/nas-clients

echo "=== Gitea SysOps user ==="
chmod +x scripts/setup_gitea_sysops.sh 2>/dev/null || true
bash scripts/setup_gitea_sysops.sh || true

echo "=== SysOps + Herman + Agent scheduler cron ==="
(crontab -l 2>/dev/null || true | grep -v sysops_daily.py | grep -v herman_daily_digest.py | grep -v herman_platform_update.py | grep -v agent_scheduler.py | grep -v research_refresh.py | grep -v marketing_publish_due.py; \
  echo "0 6 * * * cd $REMOTE_DIR && /usr/bin/python3 scripts/research_refresh.py >> /tmp/research-daily.log 2>&1"; \
  echo "5 6 * * * curl -sf -X POST http://127.0.0.1:8700/retail/rss/refresh >> /tmp/rss-daily.log 2>&1 || true"; \
  echo "10 6 * * * cd $REMOTE_DIR && /usr/bin/python3 scripts/agent_scheduler.py >> /tmp/agent-scheduler.log 2>&1"; \
  echo "0 7 * * * cd $REMOTE_DIR && /usr/bin/python3 scripts/sysops_daily.py >> /tmp/sysops-daily.log 2>&1"; \
  echo "30 7 * * * cd $REMOTE_DIR && /usr/bin/python3 scripts/herman_daily_digest.py >> /tmp/herman-daily.log 2>&1"; \
  echo "0 8 * * * cd $REMOTE_DIR && /usr/bin/python3 scripts/marketing_publish_due.py >> /tmp/marketing-publish.log 2>&1") | sort -u | crontab -
echo "Cron: research 06:00, RSS 06:05, agent_scheduler 06:10, SysOps 07:00, Herman 07:30, marketing publish 08:00"

echo "=== Run SysOps daily now (test) ==="
cd "$REMOTE_DIR" && python3 scripts/sysops_daily.py || true

echo "=== Schedule Herman platform update over 2 uur ==="
if command -v at >/dev/null 2>&1; then
  echo "cd $REMOTE_DIR && /usr/bin/python3 scripts/herman_platform_update.py >> /tmp/herman-update.log 2>&1" | at now + 2 hours 2>&1 || true
  echo "Gepland via 'at now + 2 hours'"
else
  UPD_H=$(date -d '+2 hours' +%H 2>/dev/null || date -v+2H +%H 2>/dev/null || echo "09")
  UPD_M=$(date -d '+2 hours' +%M 2>/dev/null || date -v+2H +%M 2>/dev/null || echo "00")
  (crontab -l 2>/dev/null || true; echo "\$UPD_M \$UPD_H * * * cd $REMOTE_DIR && /usr/bin/python3 scripts/herman_platform_update.py >> /tmp/herman-update.log 2>&1 # once") | sort -u | crontab -
  echo "Gepland via cron om \$UPD_H:\$UPD_M"
fi

echo "=== SysOps Gitea sync (volledige repo) ==="
docker exec foodlinkk_tools_api python3 -c "
from app.connectors.gitea_repo_sync import run_gitea_repo_sync
import json
print(json.dumps(run_gitea_repo_sync('deploy-all'), indent=2))
" || echo "Gitea sync warning — check tools-api logs"

echo "=== Herman briefing nu genereren ==="
cd "$REMOTE_DIR" && python3 scripts/herman_daily_digest.py || true
REMOTE

echo "=== Deploy voltooid ==="
echo "Cockpit:  http://10.4.7.18:8600"
echo "Tools API: http://10.4.7.18:8700"
echo "Gitea:    http://10.4.7.18:3001"
