services:
  # PostgreSQL - Central Database
  postgres:
    image: ${CORE_POSTGRES_IMAGE:-postgres:16-alpine}
    container_name: ${CORE_COMPOSE_PROJECT_NAME}_postgres
    restart: unless-stopped
    environment:
      TZ: ${TIMEZONE:-Europe/Amsterdam}
      POSTGRES_USER: ${DB_USER}
      POSTGRES_PASSWORD: ${DB_PASSWORD}
      # Performance tuning
      POSTGRES_MAX_CONNECTIONS: ${CORE_POSTGRES_MAX_CONNECTIONS:-100}
      POSTGRES_SHARED_BUFFERS: ${CORE_POSTGRES_SHARED_BUFFERS:-256MB}
      # Preventing password authentication failed for user $USER error
      POSTGRES_HOST_AUTH_METHOD: scram-sha-256
      POSTGRES_INITDB_ARGS: --auth-host=scram-sha-256
    ports:
      - 5432:5432
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./postgres/init:/docker-entrypoint-initdb.d:ro
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${DB_USER}"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
      
    networks:
      - compose_network
      
  redis:
    image: ${CORE_REDIS_IMAGE:-redis:7-alpine}
    container_name: ${CORE_COMPOSE_PROJECT_NAME}_redis
    restart: unless-stopped
    environment:
      TZ: ${TIMEZONE:-Europe/Amsterdam}
    volumes:
      - redis_data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "--raw", "incr", "ping"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 20s
      
    networks:
      - compose_network
      
volumes:
  postgres_data:
    name: ${CORE_COMPOSE_PROJECT_NAME}_postgres_data
  redis_data:
    name: ${CORE_COMPOSE_PROJECT_NAME}_redis_data
