---
services:

  # MongoDB for Overleaf data storage
  mongo:
    restart: always
    image: "${MONGO_IMAGE}:${MONGO_VERSION}"
    command: --replSet overleaf
    container_name: mongo
    volumes:
      - "${MONGO_DATA_PATH}:/data/db"
    expose:
      - 27017
    healthcheck:
      test: echo 'db.stats().ok' | mongosh localhost:27017/test --quiet
      interval: 10s
      timeout: 10s
      retries: 5
    networks:
      - overleaf

  # Redis for caching and sessions
  redis:
    restart: always
    image: "${REDIS_IMAGE}"
    container_name: redis
    command: redis-server --appendonly yes
    volumes:
      - "${REDIS_DATA_PATH}:/data"
    expose:
      - 6379
    networks:
      - overleaf

  # Overleaf (ShareLaTeX) application
  sharelatex:
    restart: always
    image: "${SHARELATEX_IMAGE}"
    container_name: sharelatex
    depends_on:
      mongo:
        condition: service_healthy
      redis:
        condition: service_started
    ports:
      - "${OVERLEAF_LISTEN_IP}:${OVERLEAF_PORT}:80"
    volumes:
      # Data volume
      - "${OVERLEAF_DATA_PATH}:/var/lib/overleaf"
      # Docker socket for sandboxed compiles
      - "${DOCKER_SOCKET_PATH}:/var/run/docker.sock"
    environment:
      # Connectivity
      OVERLEAF_MONGO_URL: "${MONGO_URL}"
      OVERLEAF_REDIS_HOST: "${REDIS_HOST}"
      OVERLEAF_REDIS_PORT: "${REDIS_PORT}"

      # Docker and compilation settings
      DOCKER_RUNNER: 'true'
      SANDBOXED_COMPILES: 'true'
      SANDBOXED_COMPILES_SIBLING_CONTAINERS: 'true'
      SANDBOXED_COMPILES_HOST_DIR: "${OVERLEAF_DATA_PATH}/data/compiles"
      SYNCTEX_BIN_HOST_PATH: "${OVERLEAF_DATA_PATH}/bin/synctex"

      # Git bridge (disabled for Community Edition)
      GIT_BRIDGE_ENABLED: 'false'

      # Load additional environment variables
    env_file:
      - variables.env
    links:
      - mongo
      - redis
    networks:
      - overleaf

networks:
  overleaf:
    driver: bridge
