services:
  # MongoDB Service
  mongodb:
    image: mongo:6.0
    container_name: infinity-mongodb-2
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}
      MONGO_INITDB_DATABASE: ${MONGO_INITDB_DATABASE}
    volumes:
      - mongodb_data:/data/db
    ports:
      - "27018:27017"
    networks:
      - infinity-network
    healthcheck:
      test: ["CMD", "mongosh", "--eval", "db.adminCommand('ping')"]
      interval: 10s
      timeout: 5s
      retries: 5

  # Web Application (Next.js)
  webapp:
    build:
      context: .
      dockerfile: webapp/Dockerfile
      args:
        MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME}
        MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}
        DB_URI: mongodb://${MONGO_INITDB_ROOT_USERNAME}:${MONGO_INITDB_ROOT_PASSWORD}@mongodb:27017/Infinity?authSource=admin
    container_name: infinity-webapp-2
    restart: always
    depends_on:
      mongodb:
        condition: service_healthy
    environment:
      - NODE_ENV=${NODE_ENV}
      - DB_URI=mongodb://${MONGO_INITDB_ROOT_USERNAME}:${MONGO_INITDB_ROOT_PASSWORD}@mongodb:27017/Infinity?authSource=admin
      - NEXT_PUBLIC_API_URL=${NEXT_PUBLIC_API_URL}
    env_file: .env
    ports:
      - "8082:3000"  # Let Docker assign a dynamic host port
    networks:
      - infinity-network

  # Worker Service
  worker:
    build:
      context: .
      dockerfile: worker/Dockerfile
      args:
        MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME}
        MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}
        DB_URI: mongodb://${MONGO_INITDB_ROOT_USERNAME}:${MONGO_INITDB_ROOT_PASSWORD}@mongodb:27017/Infinity?authSource=admin
    container_name: infinity-worker-2
    restart: always
    depends_on:
      mongodb:
        condition: service_healthy
    environment:
      - DB_URI=mongodb://${MONGO_INITDB_ROOT_USERNAME}:${MONGO_INITDB_ROOT_PASSWORD}@mongodb:27017/Infinity?authSource=admin
    env_file: .env
    networks:
      - infinity-network

  # Helper Service (runs once to create admin account)
  create-admin:
    build:
      context: .
      dockerfile: helpers/Dockerfile
      args:
        MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME}
        MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}
        DB_URI: mongodb://${MONGO_INITDB_ROOT_USERNAME}:${MONGO_INITDB_ROOT_PASSWORD}@mongodb:27017/Infinity?authSource=admin
    container_name: infinity-create-admin-2
    depends_on:
      mongodb:
        condition: service_healthy
    environment:
      - DB_URI=mongodb://${MONGO_INITDB_ROOT_USERNAME}:${MONGO_INITDB_ROOT_PASSWORD}@mongodb:27017/Infinity?authSource=admin
      - ADMIN_USERNAME=${ADMIN_USERNAME}
      - ADMIN_PASSWORD=${ADMIN_PASSWORD}
    env_file: .env
    networks:
      - infinity-network
    # This ensures the container exits after creating the admin account
    command: sh -c "python create_account.py"

networks:
  infinity-network:
    driver: bridge

volumes:
  mongodb_data:
    driver: local