# ==============================================
# Langfuse v3 self-hosted — Module 6
# ==============================================
# Usage :
#   docker compose up -d
#   Ouvrir http://localhost:3000
#   Login : formateur@ajc.fr / formation2026
#
# Arrêter :
#   docker compose down          (conserve les données)
#   docker compose down -v       (supprime tout)

services:

  langfuse-db:
    image: postgres:16-alpine
    restart: unless-stopped
    environment:
      POSTGRES_USER: langfuse
      POSTGRES_PASSWORD: langfuse
      POSTGRES_DB: langfuse
    volumes:
      - langfuse_pgdata:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U langfuse"]
      interval: 5s
      timeout: 3s
      retries: 10

  langfuse-clickhouse:
    image: clickhouse/clickhouse-server:24.3
    restart: unless-stopped
    environment:
      CLICKHOUSE_USER: langfuse
      CLICKHOUSE_PASSWORD: langfuse
    volumes:
      - langfuse_chdata:/var/lib/clickhouse
      - ./clickhouse-config.xml:/etc/clickhouse-server/config.d/langfuse.xml:ro
    healthcheck:
      test: ["CMD-SHELL", "clickhouse-client --user langfuse --password langfuse -q 'SELECT 1'"]
      interval: 10s
      timeout: 5s
      retries: 15
      start_period: 30s

  langfuse-redis:
    image: redis:7-alpine
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 3s
      retries: 10

  langfuse-minio:
    image: minio/minio:latest
    restart: unless-stopped
    command: server /data --console-address ":9001"
    environment:
      MINIO_ROOT_USER: langfuse
      MINIO_ROOT_PASSWORD: langfuse
    volumes:
      - langfuse_minio:/data
    healthcheck:
      test: ["CMD", "mc", "ready", "local"]
      interval: 5s
      timeout: 3s
      retries: 10

  # Crée le bucket au démarrage
  langfuse-minio-init:
    image: minio/mc:latest
    depends_on:
      langfuse-minio:
        condition: service_healthy
    entrypoint: >
      /bin/sh -c "
        mc alias set myminio http://langfuse-minio:9000 langfuse langfuse &&
        mc mb --ignore-existing myminio/langfuse-events &&
        exit 0
      "

  langfuse-worker:
    image: langfuse/langfuse-worker:3
    restart: unless-stopped
    depends_on:
      langfuse-db:
        condition: service_healthy
      langfuse-clickhouse:
        condition: service_healthy
      langfuse-redis:
        condition: service_healthy
      langfuse-minio:
        condition: service_healthy
    environment:
      DATABASE_URL: postgresql://langfuse:langfuse@langfuse-db:5432/langfuse
      CLICKHOUSE_URL: http://langfuse-clickhouse:8123
      CLICKHOUSE_MIGRATION_URL: clickhouse://langfuse:langfuse@langfuse-clickhouse:9000
      CLICKHOUSE_USER: langfuse
      CLICKHOUSE_PASSWORD: langfuse
      REDIS_CONNECTION_STRING: redis://langfuse-redis:6379
      LANGFUSE_S3_EVENT_UPLOAD_BUCKET: langfuse-events
      LANGFUSE_S3_EVENT_UPLOAD_REGION: us-east-1
      LANGFUSE_S3_EVENT_UPLOAD_ACCESS_KEY_ID: langfuse
      LANGFUSE_S3_EVENT_UPLOAD_SECRET_ACCESS_KEY: langfuse
      LANGFUSE_S3_EVENT_UPLOAD_ENDPOINT: http://langfuse-minio:9000
      LANGFUSE_S3_EVENT_UPLOAD_FORCE_PATH_STYLE: "true"
      LANGFUSE_WORKER_PASSWORD: worker-password
      SALT: formation-ajc-salt-changez-moi
      ENCRYPTION_KEY: b7d096dc6abd2085239df34aea85bef26853b3065d7e582081bfb05a635223d0

  langfuse:
    image: langfuse/langfuse:3
    restart: unless-stopped
    depends_on:
      langfuse-db:
        condition: service_healthy
      langfuse-clickhouse:
        condition: service_healthy
      langfuse-redis:
        condition: service_healthy
      langfuse-minio:
        condition: service_healthy
    ports:
      - "3000:3000"
    environment:
      # --- Postgres ---
      DATABASE_URL: postgresql://langfuse:langfuse@langfuse-db:5432/langfuse
      # --- Redis ---
      REDIS_CONNECTION_STRING: redis://langfuse-redis:6379
      # --- Worker (traitement des events) ---
      LANGFUSE_WORKER_HOST: http://localhost:3000
      LANGFUSE_WORKER_PASSWORD: worker-password
      LANGFUSE_INGESTION_QUEUE_PROCESSING_ENABLED: "true"
      # --- ClickHouse ---
      CLICKHOUSE_URL: http://langfuse-clickhouse:8123
      CLICKHOUSE_MIGRATION_URL: clickhouse://langfuse:langfuse@langfuse-clickhouse:9000
      CLICKHOUSE_USER: langfuse
      CLICKHOUSE_PASSWORD: langfuse
      # --- S3 / MinIO (blob storage pour les événements) ---
      LANGFUSE_S3_EVENT_UPLOAD_BUCKET: langfuse-events
      LANGFUSE_S3_EVENT_UPLOAD_REGION: us-east-1
      LANGFUSE_S3_EVENT_UPLOAD_ACCESS_KEY_ID: langfuse
      LANGFUSE_S3_EVENT_UPLOAD_SECRET_ACCESS_KEY: langfuse
      LANGFUSE_S3_EVENT_UPLOAD_ENDPOINT: http://langfuse-minio:9000
      LANGFUSE_S3_EVENT_UPLOAD_FORCE_PATH_STYLE: "true"
      # --- Auth ---
      NEXTAUTH_SECRET: formation-ajc-secret-changez-moi
      SALT: formation-ajc-salt-changez-moi
      NEXTAUTH_URL: http://localhost:3000
      # --- Chiffrement des clés LLM (requis pour LLM Connections) ---
      ENCRYPTION_KEY: b7d096dc6abd2085239df34aea85bef26853b3065d7e582081bfb05a635223d0
      # --- Init auto (compte + projet) ---
      LANGFUSE_INIT_ORG_ID: "formation"
      LANGFUSE_INIT_ORG_NAME: "AJC Formation"
      LANGFUSE_INIT_PROJECT_ID: "formation-agents"
      LANGFUSE_INIT_PROJECT_NAME: "Agents IA"
      LANGFUSE_INIT_PROJECT_PUBLIC_KEY: "pk-lf-formation"
      LANGFUSE_INIT_PROJECT_SECRET_KEY: "sk-lf-formation"
      LANGFUSE_INIT_USER_EMAIL: "formateur@ajc.fr"
      LANGFUSE_INIT_USER_NAME: "Formateur"
      LANGFUSE_INIT_USER_PASSWORD: "formation2026"
      TELEMETRY_ENABLED: "false"

volumes:
  langfuse_pgdata:
  langfuse_chdata:
  langfuse_minio:
