##############################################################################
# Genesis OS — docker-compose.yml
#
# PURPOSE: Local development infrastructure that MIRRORS the Elestio production
#          services. In production, all connection strings point to Elestio hosts
#          via environment variables. This file is for local dev / CI only.
#
# RULE: NEVER hardcode credentials. All secrets via .env file.
# RULE: SQLite is FORBIDDEN in Genesis — all persistence through these services.
# RULE: Production hosts are on Elestio — see config/secrets.env for live URLs.
#
# Usage:
#   docker-compose up -d          # Start all services
#   docker-compose down -v        # Stop and remove volumes (resets state)
#   docker-compose logs -f postgres # Tail a single service
##############################################################################

version: "3.9"

services:

  # ---------------------------------------------------------------------------
  # PostgreSQL — Primary relational store
  # Elestio production equivalent: ${ELESTIO_POSTGRES_URL}
  # Used by: genesis-core, genesis-prime-web (auth/billing), genesis-types
  # ---------------------------------------------------------------------------
  postgres:
    image: postgres:16-alpine
    container_name: genesis_postgres_local
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-genesis}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-genesis_dev_password}
      POSTGRES_DB: ${POSTGRES_DB:-genesis_os}
    ports:
      - "${POSTGRES_PORT:-5432}:5432"
    volumes:
      - genesis_postgres_data:/var/lib/postgresql/data
      - ./infra/postgres/init:/docker-entrypoint-initdb.d
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-genesis} -d ${POSTGRES_DB:-genesis_os}"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - genesis_net

  # ---------------------------------------------------------------------------
  # Redis — Caching, job queues, session store, working memory
  # Elestio production equivalent: ${ELESTIO_REDIS_URL}
  # Used by: genesis-gateway (WebRTC session state), genesis-core (task queues)
  # ---------------------------------------------------------------------------
  redis:
    image: redis:7-alpine
    container_name: genesis_redis_local
    restart: unless-stopped
    command: >
      redis-server
      --requirepass ${REDIS_PASSWORD:-genesis_dev_redis}
      --maxmemory 512mb
      --maxmemory-policy allkeys-lru
      --appendonly yes
    ports:
      - "${REDIS_PORT:-6379}:6379"
    volumes:
      - genesis_redis_data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "-a", "${REDIS_PASSWORD:-genesis_dev_redis}", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - genesis_net

  # ---------------------------------------------------------------------------
  # Qdrant — Vector database for semantic search and embeddings
  # Elestio production equivalent: ${ELESTIO_QDRANT_URL}
  # Used by: genesis-core RLM swarm, cognitive layer, memory retrieval
  # ---------------------------------------------------------------------------
  qdrant:
    image: qdrant/qdrant:v1.13.0
    container_name: genesis_qdrant_local
    restart: unless-stopped
    environment:
      QDRANT__SERVICE__API_KEY: ${QDRANT_API_KEY:-genesis_dev_qdrant_key}
      QDRANT__SERVICE__GRPC_PORT: 6334
      QDRANT__LOG_LEVEL: INFO
    ports:
      - "${QDRANT_HTTP_PORT:-6333}:6333"
      - "${QDRANT_GRPC_PORT:-6334}:6334"
    volumes:
      - genesis_qdrant_data:/qdrant/storage
    healthcheck:
      test: ["CMD-SHELL", "curl -sf http://localhost:6333/healthz || exit 1"]
      interval: 15s
      timeout: 5s
      retries: 5
    networks:
      - genesis_net

  # ---------------------------------------------------------------------------
  # FalkorDB — Graph database for knowledge graph operations
  # Used by: genesis-core falkordb_client.py, KG traversal, entity relations
  # NOTE: In production, FalkorDB runs on Elestio or dedicated host.
  # ---------------------------------------------------------------------------
  falkordb:
    image: falkordb/falkordb:latest
    container_name: genesis_falkordb_local
    restart: unless-stopped
    ports:
      - "${FALKORDB_PORT:-6380}:6379"
    volumes:
      - genesis_falkordb_data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "-p", "6379", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - genesis_net

volumes:
  genesis_postgres_data:
    driver: local
  genesis_redis_data:
    driver: local
  genesis_qdrant_data:
    driver: local
  genesis_falkordb_data:
    driver: local

networks:
  genesis_net:
    driver: bridge
    name: genesis_network
