services:
  app:
    container_name: ${CONTAINER_NAME}-app
    build:
      context: ./
      dockerfile: ./docker/app/Dockerfile
      target: app-dev
      args:
        COMPOSER_AUTH: "{}"
        APP_BASE_DIR: ${APP_BASE_DIR-.}
    depends_on:
      db:
        condition: service_healthy
    environment:
      XDEBUG_IDE_KEY: ${XDEBUG_IDE_KEY}
    restart: unless-stopped
    extra_hosts:
      - "host.docker.internal:host-gateway"
    volumes:
      - ${APP_BASE_DIR-.}:/app

  web:
    container_name: ${CONTAINER_NAME}-web
    build:
      context: ./
      dockerfile: ./docker/web/Dockerfile
      args:
        APP_BASE_DIR: ${APP_BASE_DIR-.}
    restart: unless-stopped
    ports:
      - ${HTTP_PORT}:8080
    environment:
      PHP_FPM_HOST: app
    volumes:
      - ${APP_BASE_DIR-.}/public:/app/public
    depends_on:
      app:
        condition: service_healthy

  db:
    container_name: ${CONTAINER_NAME}-db
    image: postgres:16-alpine3.18
    environment:
      USER_ID: ${USER_ID}
      GROUP_ID: ${GROUP_ID}
      PGDATA: /data/postgres
      PGUSER: ${DB_ROOT_USERNAME}
      POSTGRES_DB: ${DB_DATABASE}
      POSTGRES_USER: ${DB_USERNAME}
      POSTGRES_PASSWORD: ${DB_PASSWORD}
    volumes:
      - sqldata:/data/postgres
    ports:
      - ${DB_PORT}:5432
    restart: unless-stopped
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready", "-d" ]
      timeout: 5s
      retries: 3

  mailer:
    container_name: ${CONTAINER_NAME}-mailer
    image: axllent/mailpit
    ports:
      - "1025"
      - "8025"
    environment:
      MP_SMTP_AUTH_ACCEPT_ANY: 1
      MP_SMTP_AUTH_ALLOW_INSECURE: 1
    restart: unless-stopped

  redis:
    container_name: ${CONTAINER_NAME}-redis
    image: redis:6.2-alpine
    restart: unless-stopped
    ports:
      - ${REDIS_PORT}:${REDIS_PORT}
    command: redis-server --save 20 1 --loglevel warning
    volumes:
      - redis:/data

  zookeeper:
    container_name: ${CONTAINER_NAME}-zookeeper
    image: confluentinc/cp-zookeeper:latest
    restart: unless-stopped
    environment:
      ZOOKEEPER_CLIENT_PORT: ${ZOOKEEPER_CLIENT_PORT}
      ZOOKEEPER_TICK_TIME: 2000
    ports:
      - ${ZOOKEEPER_PORT}:${ZOOKEEPER_CLIENT_PORT}

  kafka:
    container_name: ${CONTAINER_NAME}-kafka
    image: confluentinc/cp-kafka:latest
    restart: unless-stopped
    depends_on:
      - zookeeper
    ports:
      - ${KAFKA_PORT}:${KAFKA_PORT}
    environment:
      KAFKA_BROKER_ID: ${KAFKA_BROKER_ID}
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:${ZOOKEEPER_CLIENT_PORT}
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092,PLAINTEXT_HOST://localhost:${KAFKA_PORT}
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
      KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1

volumes:
  redis:
    driver: local
  sqldata:
