version: '3.8'

services:
version: '3.8'

services:
  # API Gateway Service (based on PM2 config path)
  api-gateway:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: api-gateway
    ports:
      - "3000:3000"
    environment:
      - NODE_ENV=production
      - PORT=3000
      - USER_SERVICE_URL=http://user-service:3001
      - PRODUCT_SERVICE_URL=http://product-service:3002
    command: ["node", "dist/api-gateway/src/server.js"]
    depends_on:
      - user-service
      - product-service
    networks:
      - microservice-network
    restart: unless-stopped

  # User Service (based on PM2 config path)
  user-service:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: user-service
    ports:
      - "3001:3001"
    environment:
      - NODE_ENV=production
      - PORT=3001
      - DB_HOST=${DB_HOST:-mysql}
      - DB_PORT=${DB_PORT:-3306}
      - DB_NAME=${DB_NAME:-userdb}
      - DB_USER=${DB_USER:-microservice_user}
      - DB_PASS=${DB_PASS:-password}
    command: ["node", "dist/user-service/src/cluster.js"]
    depends_on:
      - mysql
    networks:
      - microservice-network
    restart: unless-stopped

  # Product Service (based on PM2 config path)
  product-service:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: product-service
    ports:
      - "3002:3002"
    environment:
      - NODE_ENV=production
      - PORT=3002
      - DB_HOST=${DB_HOST:-mysql}
      - DB_PORT=${DB_PORT:-3306}
      - DB_NAME=${DB_NAME:-productdb}
      - DB_USER=${DB_USER:-microservice_user}
      - DB_PASS=${DB_PASS:-password}
    command: ["node", "dist/product-service/src/cluster.js"]
    depends_on:
      - mysql
    networks:
      - microservice-network
    restart: unless-stopped

  # MySQL Database
  mysql:
    image: mysql:8.0
    container_name: mysql-db
    environment:
      - MYSQL_ROOT_PASSWORD=rootpassword
      - MYSQL_DATABASE=microservicedb
      - MYSQL_USER=microservice_user
      - MYSQL_PASSWORD=password
    ports:
      - "3306:3306"
    volumes:
      - mysql_data:/var/lib/mysql
      - ./init-db.sql:/docker-entrypoint-initdb.d/init-db.sql
    networks:
      - microservice-network
    restart: unless-stopped
    command: --default-authentication-plugin=mysql_native_password

networks:
  microservice-network:
    driver: bridge

volumes:
  mysql_data:

  # Optional: Database service

networks:
  microservice-network:
    driver: bridge

volumes:
  postgres_data: