# =============================================================================
# RWA Platform - Shared Infrastructure
# =============================================================================
# This file defines shared infrastructure services (PostgreSQL, Redis, Kafka)
# that are used by all microservices.
#
# Usage:
#   docker compose -f docker-compose.infra.yml up -d
# =============================================================================

services:
  # PostgreSQL - Shared database server (each service uses different database)
  postgres:
    image: postgres:16-alpine
    container_name: rwa-postgres
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: password
      # Create multiple databases on startup
      POSTGRES_MULTIPLE_DATABASES: rwa_identity,rwa_mpc,rwa_blockchain
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./init-multiple-dbs.sh:/docker-entrypoint-initdb.d/init-multiple-dbs.sh:ro
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      timeout: 5s
      retries: 10
    restart: unless-stopped
    networks:
      - rwa-network

  # Redis - Shared cache/session store
  redis:
    image: redis:7-alpine
    container_name: rwa-redis
    command: redis-server --appendonly yes --databases 20
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 5s
      retries: 10
    restart: unless-stopped
    networks:
      - rwa-network

  # Zookeeper - Required by Kafka
  zookeeper:
    image: confluentinc/cp-zookeeper:7.5.0
    container_name: rwa-zookeeper
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000
    healthcheck:
      test: ["CMD", "nc", "-z", "localhost", "2181"]
      interval: 10s
      timeout: 5s
      retries: 5
    restart: unless-stopped
    networks:
      - rwa-network

  # Kafka - Event streaming platform
  kafka:
    image: confluentinc/cp-kafka:7.5.0
    container_name: rwa-kafka
    depends_on:
      zookeeper:
        condition: service_healthy
    ports:
      - "9092:9092"
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092,PLAINTEXT_INTERNAL://kafka:29092
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_INTERNAL:PLAINTEXT
      KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092,PLAINTEXT_INTERNAL://0.0.0.0:29092
      KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT_INTERNAL
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
      KAFKA_AUTO_CREATE_TOPICS_ENABLE: "true"
    healthcheck:
      test: ["CMD", "kafka-topics", "--bootstrap-server", "localhost:9092", "--list"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 30s
    restart: unless-stopped
    networks:
      - rwa-network

networks:
  rwa-network:
    name: rwa-network
    driver: bridge

volumes:
  postgres_data:
  redis_data:
