x-common-env: &common-env
  DB_HOST: ${DB_HOST}
  DB_PORT: ${DB_PORT}
  DB_USER: ${DB_USER}
  DB_PASSWORD: ${DB_PASSWORD}
  DB_NAME: ${DB_NAME}
  REDIS_HOST: ${REDIS_HOST}
  REDIS_PORT: ${REDIS_PORT}

services:
  nest-app:
    build:
      context: .
    container_name: vc-be
    restart: always
    ports:
      - "${APP_PORT}:${APP_PORT}"
    env_file:
        - .env
    volumes:
      - .:/usr/src/app
      - /usr/src/app/node_modules
    command: npm run start:${NODE_ENV}
    environment:
      NODE_ENV: ${NODE_ENV}
      APP_PORT: ${APP_PORT}
      <<: *common-env
    depends_on:
      mysql-db:
        condition: service_healthy
      redis:
        condition: service_started

  mysql-db:
    image: mysql:8.0
    container_name: mysql-db
    restart: always
    command: --port=${DB_PORT}
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_USER: ashok
      MYSQL_PASSWORD: ashokpass
      MYSQL_DATABASE: westegmi_wosa_dbs_erp
    ports:
      - "${DB_PORT}:${DB_PORT}"
    volumes:
      - ./mysql_data:/var/lib/mysql
    healthcheck:
      test: ["CMD-SHELL", "mysqladmin ping -h localhost -P ${DB_PORT} -u $$MYSQL_USER -p$$MYSQL_PASSWORD || exit 1"]
      interval: 5s
      timeout: 5s
      retries: 10

  redis:   # 👈 make sure this block is aligned with nest-app and mysql-db
    image: redis:7-alpine
    container_name: redis
    restart: always
    ports:
      - "${REDIS_PORT}:${REDIS_PORT}"
    volumes:
      - ./redis_data:/data
