x-defaults: &defaults
  restart: unless-stopped
  logging:
    driver: json-file
    options:
      max-size: 100m
      max-file: '3'

x-mongo: &mongo
  <<: *defaults
  image: ${GLOBAL_REGISTRY:-}mongo:${MONGO_VERSION:-8.3.3}
  environment:
    TZ: ${TZ:-UTC}
    MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME:-root}
    MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD:-password}
    MONGO_INITDB_DATABASE: ${MONGO_INITDB_DATABASE:-admin}
  entrypoint:
    - bash
    - -c
    - |
      cp /data/rs0.key /tmp/rs0.key
      chmod 400 /tmp/rs0.key
      chown 999:999 /tmp/rs0.key
      export MONGO_INITDB_ROOT_USERNAME MONGO_INITDB_ROOT_PASSWORD MONGO_INITDB_DATABASE
      exec docker-entrypoint.sh mongod --replSet ${MONGO_REPLICA_SET_NAME:-rs0} --keyFile /tmp/rs0.key
  healthcheck:
    test: mongosh --eval "db.adminCommand('ping')"
    interval: 5s
    timeout: 3s
    retries: 10
    start_period: 30s
  extra_hosts:
    - 'host.docker.internal:host-gateway'
  deploy:
    resources:
      limits:
        cpus: ${MONGO_REPLICA_SINGLE_CPU_LIMIT:-1.0}
        memory: ${MONGO_REPLICA_SINGLE_MEMORY_LIMIT:-2048M}
      reservations:
        cpus: ${MONGO_REPLICA_SINGLE_CPU_RESERVATION:-0.1}
        memory: ${MONGO_REPLICA_SINGLE_MEMORY_RESERVATION:-1024M}

services:
  mongo1:
    <<: *mongo
    ports:
      - '${MONGO_PORT_OVERRIDE_1:-27017}:27017'
    volumes:
      - mongo_data:/data/db
      - mongo_configdb:/data/configdb
      - ./secrets/rs0.key:/data/rs0.key:ro

  mongo-init:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}mongo:${MONGO_VERSION:-8.2.3}
    restart: on-failure
    depends_on:
      mongo1:
        condition: service_healthy
    environment:
      TZ: ${TZ:-UTC}
      MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME:-root}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD:-password}
      MONGO_REPLICA_SET_NAME: ${MONGO_REPLICA_SET_NAME:-rs0}
      MONGO_PORT_1: ${MONGO_PORT_OVERRIDE_1:-27017}
      MONGO_HOST: ${MONGO_HOST:-host.docker.internal}
    volumes:
      - ./secrets/rs0.key:/data/rs0.key:ro
    extra_hosts:
      - 'host.docker.internal:host-gateway'
    entrypoint:
      - bash
      - -c
      - |
        set -e
        echo "Waiting for MongoDB nodes to be ready..."
        sleep 5

        mongosh \
          --host "$${MONGO_HOST}:$${MONGO_PORT_1}" \
          --username "$${MONGO_INITDB_ROOT_USERNAME}" \
          --password "$${MONGO_INITDB_ROOT_PASSWORD}" \
          --authenticationDatabase admin \
          --eval "
            const config = {
              _id: '$${MONGO_REPLICA_SET_NAME}',
              members: [
                { _id: 0, host: '$${MONGO_HOST}:$${MONGO_PORT_1}' }
              ]
            };

            try {
              const result = rs.status();
              print('Replica set already initialized');
            } catch (e) {
              print('Initializing replica set...');
              rs.initiate(config);
              print('Replica set initialized successfully');
            }
          "

        echo "Init container completed successfully"
    deploy:
      resources:
        limits:
          cpus: ${MONGO_REPLICA_SINGLE_INIT_CPU_LIMIT:-1.0}
          memory: ${MONGO_REPLICA_SINGLE_INIT_MEMORY_LIMIT:-2048M}
        reservations:
          cpus: ${MONGO_REPLICA_SINGLE_INIT_CPU_RESERVATION:-0.1}
          memory: ${MONGO_REPLICA_SINGLE_INIT_MEMORY_RESERVATION:-1024M}

volumes:
  mongo_data:
  mongo_configdb:
