services:
  mongodb:
    image: mongo:7
    restart: unless-stopped
    command:
      - mongod
      - --wiredTigerCacheSizeGB
      - "0.25"
      - --setParameter
      - diagnosticDataCollectionEnabled=false
      - --auth
    mem_limit: 384m
    mem_reservation: 192m
    environment:
      MONGO_INITDB_DATABASE: Invario_Website
      MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME:-website_root}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD:-website_root_dev_password}
      MONGO_APP_USER: ${MONGO_APP_USER:-website_app}
      MONGO_APP_PASSWORD: ${MONGO_APP_PASSWORD:-website_app_dev_password}
    volumes:
      - mongo_data:/data/db
      - ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
    healthcheck:
      test: ["CMD", "mongosh", "--quiet", "--username", "${MONGO_INITDB_ROOT_USERNAME:-website_root}", "--password", "${MONGO_INITDB_ROOT_PASSWORD:-website_root_dev_password}", "--authenticationDatabase", "admin", "--eval", "db.adminCommand('ping').ok"]
      interval: 10s
      timeout: 5s
      retries: 8

  website:
    build:
      context: .
      dockerfile: Dockerfile
    restart: unless-stopped
    depends_on:
      mongodb:
        condition: service_healthy
    environment:
      MONGO_URI: mongodb://${MONGO_APP_USER:-website_app}:${MONGO_APP_PASSWORD:-website_app_dev_password}@mongodb:27017/${MONGO_DB_NAME:-Invario_Website}?authSource=${MONGO_DB_NAME:-Invario_Website}
      MONGO_DB_NAME: Invario_Website
      MONGO_MAX_POOL_SIZE: ${MONGO_MAX_POOL_SIZE:-12}
      MONGO_MIN_POOL_SIZE: ${MONGO_MIN_POOL_SIZE:-0}
      MONGO_MAX_IDLE_MS: ${MONGO_MAX_IDLE_MS:-60000}
      MONGO_CONNECT_TIMEOUT_MS: ${MONGO_CONNECT_TIMEOUT_MS:-1500}
      MONGO_SOCKET_TIMEOUT_MS: ${MONGO_SOCKET_TIMEOUT_MS:-30000}
      MONGO_WAIT_QUEUE_TIMEOUT_MS: ${MONGO_WAIT_QUEUE_TIMEOUT_MS:-2000}
      SESSION_COOKIE_SECURE: ${SESSION_COOKIE_SECURE:-0}
      JWT_SECRET_KEY: change-this-in-production
      INSTANCE_BASE_DIR: /opt/Invario_Website
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    ports:
      - "127.0.0.1:4999:4999"
    mem_limit: 256m
    mem_reservation: 128m

volumes:
  mongo_data:
