services:
  postgres:
    image: postgres:17-alpine
    container_name: baron_postgres
    environment:
      POSTGRES_USER: "${DB_USER:-baron}"
      POSTGRES_PASSWORD: "${DB_PASSWORD:-password}"
      POSTGRES_DB: "${DB_NAME:-baron_sso}"
    ports:
      - "${DB_PORT:-5432}:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./docker/init-metadata:/docker-entrypoint-initdb.d
    networks:
      - baron_net
    healthcheck:
      test:
        [
          "CMD-SHELL",
          "pg_isready -U ${DB_USER:-baron} -d ${DB_NAME:-baron_sso}",
        ]
      interval: 5s
      timeout: 5s
      retries: 5
    restart: always

  clickhouse:
    image: clickhouse/clickhouse-server:latest
    container_name: baron_clickhouse
    restart: always
    volumes:
      - clickhouse_data:/var/lib/clickhouse
    environment:
      CLICKHOUSE_USER: "${CLICKHOUSE_USER:-baron}"
      CLICKHOUSE_PASSWORD: "${CLICKHOUSE_PASSWORD:-password}"
    networks:
      - baron_net

  redis:
    image: redis:7-alpine
    container_name: baron_redis
    restart: always
    command: redis-server --port 6389
    ports:
      - "6389:6389"
    volumes:
      - redis_data:/data
    networks:
      - baron_net

  gateway:
    build:
      context: ./gateway
      dockerfile: Dockerfile
    container_name: baron_gateway
    restart: always
    ports:
      - "${USERFRONT_PORT:-5000}:5000"
    networks:
      - baron_net
      - public_net
    healthcheck:
      test: ["CMD", "wget", "-qO-", "http://127.0.0.1:5000/"]
      interval: 10s
      timeout: 5s
      retries: 3
      start_period: 10s

volumes:
  postgres_data:
  clickhouse_data:
  redis_data:

networks:
  baron_net:
    external: true
    name: baron_net
  public_net:
    external: true
    name: public_net
