#!/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="coolify-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
# -------------------------------------------------------------------------

# Docker Compose for Coolify
# Note: Coolify is a self-hosted PaaS.
version: '3.8'

services:
  coolify:
    image: ghcr.io/coollabsio/coolify:latest
    container_name: coolify
    ports:
      - "8000:8000"
    environment:
      - APP_ENV=production
      - DB_CONNECTION=pgsql
      - DB_HOST=db
      - DB_DATABASE=coolify
      - DB_USERNAME=coolify
      - DB_PASSWORD=${DB_PASSWORD:-password}
    volumes:
      - coolify_data:/var/www/html/storage
      - /var/run/docker.sock:/var/run/docker.sock # Essential for controlling Docker
    depends_on:
      db:
        condition: service_healthy
    networks:
      - coolify_net
    healthcheck:
      test: [ "CMD", "curl", "-f", "http://localhost:8000/api/health" ]
      interval: 30s
      timeout: 10s
      retries: 3
    restart: unless-stopped

  db:
    image: postgres:15-alpine
    container_name: coolify-db
    environment:
      POSTGRES_USER: coolify
      POSTGRES_PASSWORD: ${DB_PASSWORD:-password}
      POSTGRES_DB: coolify
    volumes:
      - coolify_db_data:/var/lib/postgresql/data
    networks:
      - coolify_net
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready -U coolify" ]
      interval: 5s
      timeout: 5s
      retries: 5

networks:
  coolify_net:
    driver: bridge

volumes:
  coolify_data:
    name: coolify_data
  coolify_db_data:
    name: coolify_db_data

INNER_EOF

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

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