#!/bin/bash
# ============================================================
# ATLAS Social Command Center — Deploy Script
# ============================================================
# Ziel:    social.atlas-os.ai (VPS3: 82.165.139.249)
# Repo:    https://git.atlas-os.ai/hiss/atlas-social-command-center
# Forgejo: VPS4 (217.154.249.52), Port 3000 (intern)
#
# Verwendung:
#   ./deploy.sh                  # Deploy auf VPS3
#   ./deploy.sh --dry-run        # Nur testen, nicht deployen
#
# Voraussetzungen (lokal):
#   - sshpass installiert
#   - SSH-Zugang zu VPS3 (root@82.165.139.249, Passwort: Atlas68!)
#   - SSH-Zugang zu VPS4 (root@217.154.249.52, Passwort: Atlas68!)
# ============================================================

set -e

VPS3="82.165.139.249"
VPS4="217.154.249.52"
VPS_PASS="Atlas68!"
FORGEJO_TOKEN="e90510e84c652430b6e07b771d0cb66b8ce6d075"
REPO_DIR="/opt/atlas-social-command-center"
WEBROOT="/var/www/social"

echo "🚀 ATLAS Social Command Center — Deploy"
echo "========================================"

# 1. Code pushen (falls lokale Änderungen vorhanden)
if [ -d ".git" ]; then
  echo "📦 Pushe Code auf Forgejo..."
  
  # SSH-Tunnel zu Forgejo aufbauen (via VPS4)
  pkill -f "ssh.*3002.*3000" 2>/dev/null || true
  sleep 1
  sshpass -p "$VPS_PASS" ssh -o StrictHostKeyChecking=no -o ExitOnForwardFailure=yes -f -N \
    -L 3002:127.0.0.1:3000 root@$VPS4 2>/dev/null
  sleep 2
  
  git remote set-url forgejo "http://hiss:${FORGEJO_TOKEN}@localhost:3002/hiss/atlas-social-command-center.git" 2>/dev/null || \
  git remote add forgejo "http://hiss:${FORGEJO_TOKEN}@localhost:3002/hiss/atlas-social-command-center.git"
  
  git push forgejo main
  echo "✅ Code gepusht"
fi

# 2. Auf VPS3 deployen
echo "🔧 Deploye auf VPS3 (social.atlas-os.ai)..."

sshpass -p "$VPS_PASS" ssh -o StrictHostKeyChecking=no root@$VPS3 bash << REMOTE
set -e

FORGEJO_TOKEN="$FORGEJO_TOKEN"
REPO_DIR="$REPO_DIR"
WEBROOT="$WEBROOT"

# SSH-Tunnel zu Forgejo (VPS4) aufbauen
pkill -f "ssh.*3002.*3000" 2>/dev/null || true
sleep 1
sshpass -p '$VPS_PASS' ssh -o StrictHostKeyChecking=no -o ExitOnForwardFailure=yes -f -N \
  -L 3002:127.0.0.1:3000 root@$VPS4 2>/dev/null
sleep 2

# Repo klonen oder updaten
if [ -d "\$REPO_DIR/.git" ]; then
  echo "📥 Update Repo..."
  cd "\$REPO_DIR"
  git remote set-url origin "http://hiss:\${FORGEJO_TOKEN}@localhost:3002/hiss/atlas-social-command-center.git"
  git fetch origin
  git reset --hard origin/main
else
  echo "📥 Klone Repo..."
  git clone "http://hiss:\${FORGEJO_TOKEN}@localhost:3002/hiss/atlas-social-command-center.git" "\$REPO_DIR"
  cd "\$REPO_DIR"
fi

cd "\$REPO_DIR"

# Dependencies & Build
echo "📦 pnpm install..."
pnpm install --frozen-lockfile 2>&1 | tail -3

echo "🔨 pnpm build..."
pnpm run build 2>&1 | tail -5

# Webroot aktualisieren
echo "🌐 Webroot aktualisieren..."
mkdir -p "\$WEBROOT"
rm -rf "\${WEBROOT:?}"/*
cp -r "\$REPO_DIR/dist/public/." "\$WEBROOT/"

echo "✅ Deploy abgeschlossen!"
echo "🌍 https://social.atlas-os.ai"
REMOTE

echo ""
echo "✅ ATLAS Social Command Center erfolgreich deployed!"
echo "🌍 https://social.atlas-os.ai"
