services:
  # MongoDB Database
  mongodb:
    image: mongo:7.0
    container_name: business_platform_mongodb
    restart: always
    ports:
      - "27017:27017"
    environment:
      MONGO_INITDB_DATABASE: business_platform_db
    volumes:
      - mongodb_data:/data/db
      - mongodb_config:/data/configdb
    networks:
      - business_network
    healthcheck:
      test: echo 'db.runCommand("ping").ok' | mongosh localhost:27017/test --quiet
      interval: 10s
      timeout: 5s
      retries: 5

  # Django Application
  django:
    build: .
    container_name: business_platform_django
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/app
      - static_volume:/app/staticfiles
      - media_volume:/app/media
    ports:
      - "8000:8000"
    environment:
      - DEBUG=True
      - MONGO_HOST=mongodb
      - MONGO_PORT=27017
    env_file:
      - .env
    depends_on:
      mongodb:
        condition: service_healthy
    networks:
      - business_network
    restart: always

  # Weekly newsletter digest + hourly expiring-item checks (offers/campaigns/
  # subscriptions) — reuses the Django image, loops each on its own cycle in
  # the background rather than pulling in Celery+Redis for two cron jobs.
  scheduler:
    build: .
    container_name: business_platform_scheduler
    command: sh -c "(while true; do python manage.py send_weekly_digest; sleep 604800; done) & (while true; do python manage.py check_expiring_items; sleep 3600; done) & wait"
    volumes:
      - .:/app
      - media_volume:/app/media
    environment:
      - MONGO_HOST=mongodb
      - MONGO_PORT=27017
    env_file:
      - .env
    depends_on:
      mongodb:
        condition: service_healthy
    networks:
      - business_network
    restart: always

  # MongoDB Express (Web UI for MongoDB)
  mongo-express:
    image: mongo-express:latest
    container_name: business_platform_mongo_express
    restart: always
    ports:
      - "8081:8081"
    environment:
      ME_CONFIG_MONGODB_URL: mongodb://mongodb:27017/
      ME_CONFIG_BASICAUTH_USERNAME: admin
      ME_CONFIG_BASICAUTH_PASSWORD: admin123
    depends_on:
      - mongodb
    networks:
      - business_network

volumes:
  mongodb_data:
    driver: local
  mongodb_config:
    driver: local
  static_volume:
  media_volume:

networks:
  business_network:
    driver: bridge
