volumes:
  db_data: {}
  batcher_db_data: {}
  redis_data: {}
  backend_media: {}
  backend_static: {}

services:
  backend:
    build: ./backend
    depends_on:
      postgres: &healthy-dependency
        condition: service_healthy
      rabbitmq: *healthy-dependency
    volumes:
      - backend_media:/app/media
      - backend_static:/app/static
    command: /app/scripts/start.sh
    restart: on-failure
    env_file: &backend-env-files
      - .env/dev/pg
      - .env/dev/back
      - .env/dev/rmq
      - .env/dev/bot
      - .env/dev/web
    ports:
      - '8000:8000'

  postgres: &pg-conf
    image: postgres:17-alpine
    volumes:
      - db_data:/var/lib/postgresql/data
    env_file:
      - .env/dev/pg
    ports:
      - '5432:5432'
    user: postgres
    healthcheck: &pg-healthcheck
      test: [ "CMD-SHELL", "pg_isready" ]
      interval: 5s
      timeout: 2s
      retries: 5

  celery:
    build: ./backend
    command: /app/scripts/start_celery.sh
    env_file: *backend-env-files
    environment:
      - CELERY_WORKER_COUNT=1
    depends_on:
      backend:
        condition: service_started
      rabbitmq: *healthy-dependency

  rabbitmq:
    container_name: 'rabbitmq'
    image: 'rabbitmq:3-management-alpine'
    env_file:
      - .env/dev/rmq
    ports:
      - '5672:5672'
      - '15672:15672'
    healthcheck:
      <<: *pg-healthcheck
      test: rabbitmq-diagnostics -q ping
      interval: 10s
      timeout: 2s

  redis:
    env_file:
      - .env/dev/redis
    image: redis
    command: bash -c "redis-server --appendonly yes --requirepass $${REDIS_PASSWORD}"
    ports:
      - '6379:6379'
    volumes:
      - redis_data:/data
    healthcheck:
      <<: *pg-healthcheck
      test: "[ $$(redis-cli -a $$REDIS_PASSWORD ping) = 'PONG' ]"

  batcher:
    build: ./batcher
    depends_on:
      redis: *healthy-dependency
      batcher-postgres: *healthy-dependency
      rabbitmq: *healthy-dependency
    env_file:
      - .env/dev/rmq
      - .env/dev/redis
      - .env/dev/batcher-pg
      - .env/dev/batcher
      - .env/dev/bot
      - .env/dev/web
    ports:
      - '8080:8080'

  batcher-postgres:
    <<: *pg-conf
    volumes:
      - batcher_db_data:/var/lib/postgresql/data
    env_file:
      - .env/dev/batcher-pg
    ports:
      - '5433:5432'
