services:
  postgres:
    image: postgres:16-alpine
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    environment:
      - POSTGRES_USER=orcheo
      - POSTGRES_PASSWORD=orcheo
      - POSTGRES_DB=orcheo
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U orcheo"]
      interval: 10s
      timeout: 5s
      retries: 5

  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
    command: redis-server --appendonly yes --appendfsync everysec
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5

  backend:
    build:
      context: .
      dockerfile: Dockerfile.backend
    ports:
      - "8000:8000"
    volumes:
      - .:/app
      - backend_venv:/app/.venv
      - uv_cache:/root/.cache/uv
    environment:
      - REDIS_URL=redis://redis:6379/0
      - ORCHEO_POSTGRES_DSN=postgresql://orcheo:orcheo@postgres:5432/orcheo
      - ORCHEO_REPOSITORY_BACKEND=postgres
      - ORCHEO_CHECKPOINT_BACKEND=postgres
      - ORCHEO_CHATKIT_BACKEND=postgres
      - ORCHEO_VAULT_BACKEND=postgres
      - ORCHEO_VAULT_ENCRYPTION_KEY=0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
      - ORCHEO_AUTH_SERVICE_TOKEN_BACKEND=postgres
      - ORCHEO_AUTH_MODE=${ORCHEO_AUTH_MODE:-optional}
      - ORCHEO_AUTH_JWKS_URL=${ORCHEO_AUTH_JWKS_URL:-}
      - ORCHEO_AUTH_AUDIENCE=${ORCHEO_AUTH_AUDIENCE:-}
      - ORCHEO_AUTH_ISSUER=${ORCHEO_AUTH_ISSUER:-}
      - ORCHEO_AUTH_DEV_LOGIN_ENABLED=${ORCHEO_AUTH_DEV_LOGIN_ENABLED:-false}
      - ORCHEO_AUTH_BOOTSTRAP_SERVICE_TOKEN=${ORCHEO_AUTH_BOOTSTRAP_SERVICE_TOKEN:-}
      - ORCHEO_CHATKIT_PUBLIC_BASE_URL=${ORCHEO_CHATKIT_PUBLIC_BASE_URL:-https://orcheo-canvas.ai-colleagues.com}
      - ORCHEO_CORS_ALLOW_ORIGINS=http://localhost:5173,http://127.0.0.1:5173,https://orcheo-canvas.ai-colleagues.com,https://ai-colleagues.com,https://www.ai-colleagues.com
      # Use system Python from base image instead of downloading
      - UV_PYTHON_PREFERENCE=system
      - ORCHEO_MCP_STDIO_LOG=/dev/stderr
    depends_on:
      redis:
        condition: service_healthy
      postgres:
        condition: service_healthy
    command: uv run uvicorn orcheo_backend.app:app --host 0.0.0.0 --port 8000 --reload

  worker:
    build:
      context: .
      dockerfile: Dockerfile.backend
    volumes:
      - .:/app
      - worker_venv:/app/.venv
      - uv_cache:/root/.cache/uv
    environment:
      - REDIS_URL=redis://redis:6379/0
      - ORCHEO_POSTGRES_DSN=postgresql://orcheo:orcheo@postgres:5432/orcheo
      - ORCHEO_REPOSITORY_BACKEND=postgres
      - ORCHEO_CHECKPOINT_BACKEND=postgres
      - ORCHEO_CHATKIT_BACKEND=postgres
      - ORCHEO_VAULT_BACKEND=postgres
      - ORCHEO_VAULT_ENCRYPTION_KEY=0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
      - ORCHEO_AUTH_SERVICE_TOKEN_BACKEND=postgres
      - ORCHEO_AUTH_MODE=${ORCHEO_AUTH_MODE:-optional}
      - ORCHEO_AUTH_JWKS_URL=${ORCHEO_AUTH_JWKS_URL:-}
      - ORCHEO_AUTH_AUDIENCE=${ORCHEO_AUTH_AUDIENCE:-}
      - ORCHEO_AUTH_ISSUER=${ORCHEO_AUTH_ISSUER:-}
      - ORCHEO_AUTH_BOOTSTRAP_SERVICE_TOKEN=${ORCHEO_AUTH_BOOTSTRAP_SERVICE_TOKEN:-}
      - ORCHEO_CHATKIT_PUBLIC_BASE_URL=${ORCHEO_CHATKIT_PUBLIC_BASE_URL:-https://orcheo-canvas.ai-colleagues.com}
      # Use system Python from base image instead of downloading
      - UV_PYTHON_PREFERENCE=system
      - ORCHEO_MCP_STDIO_LOG=/dev/stderr
    depends_on:
      redis:
        condition: service_healthy
      postgres:
        condition: service_healthy
    command: uv run celery -A orcheo_backend.worker.celery_app worker --loglevel=info

  celery-beat:
    build:
      context: .
      dockerfile: Dockerfile.backend
    volumes:
      - .:/app
      - celery_venv:/app/.venv
      - uv_cache:/root/.cache/uv
    environment:
      - REDIS_URL=redis://redis:6379/0
      - ORCHEO_POSTGRES_DSN=postgresql://orcheo:orcheo@postgres:5432/orcheo
      - ORCHEO_REPOSITORY_BACKEND=postgres
      - ORCHEO_CHECKPOINT_BACKEND=postgres
      - ORCHEO_CHATKIT_BACKEND=postgres
      - ORCHEO_VAULT_BACKEND=postgres
      - ORCHEO_VAULT_ENCRYPTION_KEY=0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
      - ORCHEO_AUTH_SERVICE_TOKEN_BACKEND=postgres
      - ORCHEO_AUTH_MODE=${ORCHEO_AUTH_MODE:-optional}
      - ORCHEO_AUTH_JWKS_URL=${ORCHEO_AUTH_JWKS_URL:-}
      - ORCHEO_AUTH_AUDIENCE=${ORCHEO_AUTH_AUDIENCE:-}
      - ORCHEO_AUTH_ISSUER=${ORCHEO_AUTH_ISSUER:-}
      - ORCHEO_AUTH_BOOTSTRAP_SERVICE_TOKEN=${ORCHEO_AUTH_BOOTSTRAP_SERVICE_TOKEN:-}
      - ORCHEO_CHATKIT_PUBLIC_BASE_URL=${ORCHEO_CHATKIT_PUBLIC_BASE_URL:-https://orcheo-canvas.ai-colleagues.com}
      # Use system Python from base image instead of downloading
      - UV_PYTHON_PREFERENCE=system
      - ORCHEO_MCP_STDIO_LOG=/dev/stderr
      - CELERY_BEAT_SCHEDULE_FILE=/app/.orcheo/celerybeat-schedule
    depends_on:
      redis:
        condition: service_healthy
      postgres:
        condition: service_healthy
    command: >
      sh -c 'uv run celery -A orcheo_backend.worker.celery_app beat --loglevel=info -s "$$CELERY_BEAT_SCHEDULE_FILE"'

  canvas:
    build:
      context: .
      dockerfile: Dockerfile.canvas
    ports:
      - "5173:5173"
    volumes:
      - ./apps/canvas:/app
      - canvas_node_modules:/app/node_modules
    environment:
      - VITE_ORCHEO_BACKEND_URL=${VITE_ORCHEO_BACKEND_URL:-https://orcheo.ai-colleagues.com}
      - VITE_ORCHEO_AUTH_ISSUER=${VITE_ORCHEO_AUTH_ISSUER:-}
      - VITE_ORCHEO_AUTH_CLIENT_ID=${VITE_ORCHEO_AUTH_CLIENT_ID:-}
      - VITE_ORCHEO_AUTH_REDIRECT_URI=${VITE_ORCHEO_AUTH_REDIRECT_URI:-}
      - VITE_ORCHEO_AUTH_SCOPES=${VITE_ORCHEO_AUTH_SCOPES:-openid profile email}
      - VITE_ORCHEO_AUTH_AUDIENCE=${VITE_ORCHEO_AUTH_AUDIENCE:-}
      - VITE_ORCHEO_AUTH_ORGANIZATION="org_nM333QpnvUAR3dqa"
      - VITE_ORCHEO_AUTH_PROVIDER_PARAM=${VITE_ORCHEO_AUTH_PROVIDER_PARAM:-}
      - VITE_ORCHEO_AUTH_PROVIDER_GOOGLE=${VITE_ORCHEO_AUTH_PROVIDER_GOOGLE:-}
      - VITE_ORCHEO_AUTH_PROVIDER_GITHUB=${VITE_ORCHEO_AUTH_PROVIDER_GITHUB:-}
      - VITE_ORCHEO_CHATKIT_DOMAIN_KEY=domain_pk_6954ef8b091c8190b0734f266b51edd00094f73ed7d04989
      - VITE_ALLOWED_HOSTS=orcheo-canvas.ai-colleagues.com
    depends_on:
      - backend
    command: sh -c "npm ci && npm run dev -- --host 0.0.0.0"

volumes:
  postgres_data:
  redis_data:
  uv_cache:
  canvas_node_modules:
  backend_venv:
  worker_venv:
  celery_venv:
