#!/bin/bash

# noHuman Team installer — Mac/Linux
# Usage: curl -sL https://www.nohuman.team/install.sh | bash

IMAGE="ghcr.io/nohuman-team/dashboard:latest"
CONTAINER_NAME="nohuman-dashboard"
NETWORK_NAME="nohuman-net"
VOLUME_NAME="nohuman-data"
PORT=4005

echo "=============================="
echo "  noHuman Team Installer"
echo "=============================="
echo ""

# --- Check Docker installed ---
if ! command -v docker &> /dev/null; then
  echo "❌ Docker is not installed."
  echo ""
  if [ "$(uname)" = "Darwin" ]; then
    echo "   Install Docker Desktop for Mac:"
    echo "   https://www.docker.com/products/docker-desktop/"
    echo ""
    echo "   Or install via Homebrew:"
    echo "   brew install --cask docker"
  else
    echo "   Install Docker Desktop for Linux:"
    echo "   https://www.docker.com/products/docker-desktop/"
  fi
  echo ""
  echo "After installing, run this command again:"
  echo "   curl -sL https://www.nohuman.team/install.sh | bash"
  exit 1
fi

# --- Check Docker running ---
if ! docker info &> /dev/null 2>&1; then
  echo "❌ Docker is installed but not running."
  echo ""
  if [ "$(uname)" = "Darwin" ]; then
    echo "   Open Docker Desktop from your Applications folder,"
    echo "   or run: open -a Docker"
  else
    echo "   Start the Docker service:"
    echo "   sudo systemctl start docker"
  fi
  echo ""
  echo "   Wait for Docker to finish starting, then run this command again:"
  echo "   curl -sL https://www.nohuman.team/install.sh | bash"
  exit 1
fi

echo "✅ Docker is running"

# --- Check if already running ---
if docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
  echo ""
  echo "⚠  Dashboard is already running!"
  echo "   http://localhost:${PORT}"
  echo ""
  read -p "Restart with latest version? [y/N] " answer
  if [ "$answer" != "y" ] && [ "$answer" != "Y" ]; then
    echo "Aborted."
    exit 0
  fi
  echo "🔄 Stopping existing dashboard..."
  docker stop "$CONTAINER_NAME" > /dev/null 2>&1 || true
  docker rm "$CONTAINER_NAME" > /dev/null 2>&1 || true
fi

# --- Pull latest image ---
echo "⬇️  Pulling latest noHuman Team..."
if ! docker pull "$IMAGE"; then
  echo ""
  echo "❌ Failed to pull image. Check your internet connection and try again."
  exit 1
fi
echo "✅ Image ready"

# --- Create network if needed ---
docker network create "$NETWORK_NAME" 2>/dev/null || true

# --- Start dashboard ---
echo ""
echo "🚀 Starting noHuman Team..."
if ! docker run -d \
  --name "$CONTAINER_NAME" \
  --network "$NETWORK_NAME" \
  --restart unless-stopped \
  -p "${PORT}:3000" \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -v "${VOLUME_NAME}:/data" \
  -e NODE_ENV=production \
  "$IMAGE" > /dev/null; then
  echo ""
  echo "❌ Failed to start container. Check if port ${PORT} is already in use:"
  echo "   lsof -i :${PORT}"
  exit 1
fi

# --- Wait and open browser ---
sleep 3
if command -v xdg-open &> /dev/null; then
  xdg-open "http://localhost:${PORT}"
elif command -v open &> /dev/null; then
  open "http://localhost:${PORT}"
fi

echo ""
echo "✅ noHuman Team is running at http://localhost:${PORT}"
echo ""
echo "To stop:    docker stop ${CONTAINER_NAME}"
echo "To restart: docker restart ${CONTAINER_NAME}"
echo "To update:  curl -sL https://www.nohuman.team/install.sh | bash"
