#!/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="taiga-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:
  taiga-front:
    image: taigaio/taiga-front:latest
    container_name: taiga-front
    restart: unless-stopped
    depends_on:
      - taiga-back
    ports:
      - "9000:80"
    environment:
      - TAIGA_URL=http://localhost:9000
      - TAIGA_WEBSOCKETS_URL=ws://localhost:9000

  taiga-back:
    image: taigaio/taiga-back:latest
    container_name: taiga-back
    restart: unless-stopped
    depends_on:
      - taiga-db
      - taiga-redis
      - taiga-async-rabbitmq
    environment:
      - POSTGRES_DB=taiga
      - POSTGRES_USER=taiga
      - POSTGRES_PASSWORD=taiga
      - TAIGA_SECRET_KEY=exe3quu8Su2wohx0uNgo0eif4wohphah

  taiga-db:
    image: postgres:13-alpine
    container_name: taiga-db
    restart: unless-stopped
    environment:
      - POSTGRES_DB=taiga
      - POSTGRES_USER=taiga
      - POSTGRES_PASSWORD=taiga
    volumes:
      - taiga_db_data:/var/lib/postgresql/data

  taiga-async-rabbitmq:
    image: rabbitmq:3.8-management-alpine
    container_name: taiga-rabbitmq
    restart: unless-stopped
    environment:
      - RABBITMQ_ERLANG_COOKIE=secret-cookie
      - RABBITMQ_DEFAULT_USER=taiga
      - RABBITMQ_DEFAULT_PASS=taiga

  taiga-redis:
    image: redis:6-alpine
    container_name: taiga-redis
    restart: unless-stopped

volumes:
  taiga_db_data:

INNER_EOF

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

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