#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")/.."

echo "Stopping CapaKraken..."

# 1. Stop any legacy local dev server
if [ -f /tmp/capakraken-dev.pid ]; then
  PID=$(cat /tmp/capakraken-dev.pid)
  if kill -0 "$PID" 2>/dev/null; then
    echo "  Stopping Next.js (PID $PID)..."
    kill "$PID" 2>/dev/null || true
    sleep 1
    kill -9 "$PID" 2>/dev/null || true
  fi
  rm -f /tmp/capakraken-dev.pid
fi

# Also kill anything on port 3100
fuser -k 3100/tcp 2>/dev/null || true

# 2. Stop Docker services (keep data volumes)
echo "  Stopping app, PostgreSQL and Redis..."
docker compose --profile full stop app postgres redis 2>/dev/null || true

echo ""
echo "CapaKraken stopped."
