services:
  # MongoDB service
  textbee-db:
    container_name: textbee-db
    image: mongo:latest
    restart: always
    environment:
      - MONGO_INITDB_ROOT_USERNAME=${MONGO_ROOT_USER:-adminUser}
      - MONGO_INITDB_ROOT_PASSWORD=${MONGO_ROOT_PASS:-adminPassword}
      - MONGO_INITDB_DATABASE=textbee
    volumes:
      # - ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
      - mongodb_data:/data/db
    ports:
      - "${MONGO_PORT:-27018}:27017"
    networks:
      - textbee-network
    healthcheck:
      test: ["CMD", "mongosh", "--eval", "db.adminCommand('ping')"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 20s

  # MongoDB Express (optional admin UI)
  mongo-express:
    container_name: textbee-mongo-express
    image: mongo-express:latest
    restart: always
    ports:
      - "${MONGO_EXPRESS_PORT:-8081}:8081"
    environment:
      - ME_CONFIG_MONGODB_ADMINUSERNAME=${MONGO_ROOT_USER:-adminUser}
      - ME_CONFIG_MONGODB_ADMINPASSWORD=${MONGO_ROOT_PASS:-adminPassword}
      - ME_CONFIG_MONGODB_SERVER=textbee-db
    depends_on:
      textbee-db:
        condition: service_healthy
    networks:
      - textbee-network

  # NestJS API
  textbee-api:
    container_name: textbee-api
    # image: ghcr.io/vernu/textbee/api:latest
    build:
      context: ./api
      dockerfile: Dockerfile
    restart: always
    ports:
      - "${PORT:-3001}:3001"
    env_file:
      - ./api/.env
    environment:
      - PORT=${PORT:-3001}
      - REDIS_URL=${REDIS_URL:-redis://textbee-redis:6379}

    depends_on:
      textbee-db:
        condition: service_healthy
    networks:
      - textbee-network

  # Next.js Web
  textbee-web:
    container_name: textbee-web
    # image: ghcr.io/vernu/textbee/web:latest
    build:
      context: ./web
      dockerfile: Dockerfile
    restart: always
    ports:
      - "${PORT:-3000}:3000"
    env_file:
      - ./web/.env
    environment:
      - PORT=${PORT:-3000}
      - NEXT_PUBLIC_API_BASE_URL=${NEXT_PUBLIC_API_BASE_URL:-http://localhost:3001/api/v1}

    depends_on:
      - textbee-api
    networks:
      - textbee-network

  # Redis (if SMS queue is needed)
  textbee-redis:
    container_name: textbee-redis
    image: redis:alpine
    restart: always
    ports:
      - "${REDIS_PORT:-6379}:6379"
    volumes:
      - redis_data:/data
    networks:
      - textbee-network
    command: redis-server --appendonly yes
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 20s

networks:
  textbee-network:
    driver: bridge

volumes:
  mongodb_data:
  redis_data: 