#!/bin/bash
# 🚀 Auto-generated by The AltStack
# https://thealtstack.com

echo "🔵 Starting AltStack Deployment..."

# 1. Check/Install Docker
if ! command -v docker &> /dev/null; then
    echo "📦 Docker not found. Installing..."
    curl -fsSL https://get.docker.com -o get-docker.sh
    sh get-docker.sh
    rm get-docker.sh
    echo "✅ Docker installed."
else
    echo "✅ Docker is already installed."
fi

# 2. Setup Directory
APP_DIR="activepieces-deploy"
mkdir -p $APP_DIR
cd $APP_DIR
echo "📂 Created directory: $APP_DIR"

# 3. Create docker-compose.yml
echo "📄 Writing configuration..."
cat << 'INNER_EOF' > docker-compose.yml

# -------------------------------------------------------------------------
# 🚀 Created and distributed by The AltStack
# 🌍 https://thealtstack.com
# -------------------------------------------------------------------------

version: '3.8'

services:
  activepieces:
    image: activepieces/activepieces:latest
    container_name: activepieces
    restart: unless-stopped
    depends_on:
      - db
      - redis
    ports:
      - "8080:80"
    environment:
      - AP_FRONTEND_URL=http://localhost:8080
      - AP_POSTGRES_DATABASE=activepieces
      - AP_POSTGRES_HOST=db
      - AP_POSTGRES_PORT=5432
      - AP_POSTGRES_USERNAME=activepieces
      - AP_POSTGRES_PASSWORD=activepieces
      - AP_REDIS_HOST=redis
      - AP_REDIS_PORT=6379

  db:
    image: postgres:14-alpine
    container_name: activepieces-db
    restart: unless-stopped
    environment:
      - POSTGRES_USER=activepieces
      - POSTGRES_PASSWORD=activepieces
      - POSTGRES_DB=activepieces
    volumes:
      - activepieces_db_data:/var/lib/postgresql/data

  redis:
    image: redis:alpine
    container_name: activepieces-redis
    restart: unless-stopped

volumes:
  activepieces_db_data:

INNER_EOF

# 4. Start Services
echo "🚀 Starting services..."
docker compose up -d

echo ""
echo "✅ Deployment Complete!"
echo "👉 Your activepieces stack is running."
