services:
  client_app:
    container_name: client_app
    build:
      context: ./client
      dockerfile: Dockerfile
    ports:
      - 5173:5173
    restart: always

  redis_db:
    image: "redis:alpine"
    container_name: redis_db
    ports:
      - ${REDIS_PORT}:${REDIS_PORT}
    environment:
      - REDIS_PASSWORD=${REDIS_PASSWORD}
    command: [ "redis-server", "--requirepass", "${REDIS_PASSWORD}" ]
    volumes:
      - ./redis_data:/data
    expose:
      - ${REDIS_PORT}:${REDIS_PORT}
    restart: unless-stopped

  ems:
    container_name: ems
    build:
      context: ./ems
      dockerfile: Dockerfile
    volumes:
      - ./ems/public:/app/public
    links:
      - redis_db:redis_db
      - psql_db:psql_db
    depends_on:
      - redis_db
      - psql_db
    environment:
      - REDIS_PASSWORD=${REDIS_PASSWORD}
      - REDIS_HOST=${REDIS_HOST}
      - REDIS_PORT=${REDIS_PORT}
      - EMS_PORT=${EMS_PORT}
      - DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@psql_db:${POSTGRES_PORT}/${POSTGRES_DB}?schema=public
    ports:
      - ${EMS_PORT}:${EMS_PORT}
    restart: always
  
  monitor:
    container_name: monitor
    build:
      context: ./monitor
      dockerfile: Dockerfile
    environment:
      - MONITOR_PORT=${MONITOR_PORT}
    ports:
      - ${MONITOR_PORT}:${MONITOR_PORT}
    volumes:
      - ./monitor/data:/app/data
    restart: always
  
  psql_db:
    container_name: psql_db
    image: postgres:16.4-alpine
    volumes:
      - ./psql_data:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=${POSTGRES_DB}
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
    ports:
      - ${POSTGRES_PORT}:${POSTGRES_PORT}
    expose:
      - ${POSTGRES_PORT}
    healthcheck:
      test:
        ['CMD-SHELL', 'pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}']
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 10s
    restart: always
  
  clickhouse_test:
    container_name: clickhouse_test
    image: clickhouse/clickhouse-server
    environment:
      - CLICKHOUSE_DB=${CLICKHOUSE_DB}
      - CLICKHOUSE_USER=${CLICKHOUSE_USER}
      - CLICKHOUSE_DEFAULT_ACCESS_MANAGEMENT=${CLICKHOUSE_DEFAULT_ACCESS_MANAGEMENT}
      - CLICKHOUSE_PASSWORD=${CLICKHOUSE_PASSWORD}
    ports:
      - 8123:8123
      - 9000:9000
    expose:
      - 8123
      - 9000
