version: '3.8'

services:
  # =====================================
  # Core Infrastructure Services
  # =====================================
  
  # PostgreSQL - Main database
  postgres:
    image: postgres:15
    container_name: pipeline_postgres
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./databases/scripts/init.sql:/docker-entrypoint-initdb.d/01-init.sql
      - ./databases/scripts/schemas.sql:/docker-entrypoint-initdb.d/02-schemas.sql
    ports:
      - "5432:5432"
    networks:
      - pipeline_network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
      interval: 30s
      timeout: 10s
      retries: 5
    restart: unless-stopped

  # Redis - Caching, queues, and real-time data
  redis:
    image: redis:7-alpine
    container_name: pipeline_redis
    command: redis-server --appendonly yes --requirepass ${REDIS_PASSWORD}
    volumes:
      - redis_data:/data
    ports:
      - "6379:6379"
    networks:
      - pipeline_network
    healthcheck:
      test: ["CMD", "redis-cli", "-a", "${REDIS_PASSWORD}", "ping"]
      interval: 30s
      timeout: 10s
      retries: 3
    restart: unless-stopped

  # MongoDB - Document storage for generated code and templates
  mongodb:
    image: mongo:7
    container_name: pipeline_mongodb
    environment:
      MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}
      MONGO_INITDB_DATABASE: code_repository
    volumes:
      - mongodb_data:/data/db
      - ./databases/scripts/mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js
    ports:
      - "27017:27017"
    networks:
      - pipeline_network
    restart: unless-stopped

  # RabbitMQ - Message queue for service communication
  rabbitmq:
    build:
      context: ./infrastructure/rabbitmq
      dockerfile: Dockerfile
    container_name: pipeline_rabbitmq
    hostname: rabbitmq-server
    environment:
      RABBITMQ_DEFAULT_USER: ${RABBITMQ_DEFAULT_USER}
      RABBITMQ_DEFAULT_PASS: ${RABBITMQ_DEFAULT_PASS}
      RABBITMQ_DEFAULT_VHOST: /
      RABBITMQ_DEFINITIONS_FILE: /etc/rabbitmq/definitions.json
      RABBITMQ_CONFIG_FILE: /etc/rabbitmq/rabbitmq.conf
    volumes:
      - rabbitmq_data:/var/lib/rabbitmq
      - rabbitmq_logs:/var/log/rabbitmq
    ports:
      - "5672:5672"   # AMQP port
      - "15672:15672" # Management UI
    networks:
      - pipeline_network
    healthcheck:
      test: ["CMD", "rabbitmq-diagnostics", "ping"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 60s
    restart: unless-stopped
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy

# =====================================
# Volumes
# =====================================
volumes:
  postgres_data:
    driver: local
  redis_data:
    driver: local
  mongodb_data:
    driver: local
  rabbitmq_data:
    driver: local
  rabbitmq_logs:
    driver: local

# =====================================
# Networks
# =====================================
networks:
  pipeline_network:
    driver: bridge
    ipam:
      config:
        - subnet: 172.20.0.0/16
