---
# FlexMeasures Stack — Docker Compose
# Adapté pour Traefik avec sous-domaines digitribe.fr

services:
  # ─── PostgreSQL ────────────────────────────────────────────────
  flexmeasures-db:
    image: postgres:17
    container_name: flexmeasures-db
    restart: unless-stopped
    environment:
      POSTGRES_DB: flexmeasures
      POSTGRES_USER: flexmeasures
      POSTGRES_PASSWORD: ${FLEXMEASURES_DB_PASSWORD:-Digitribe972}
    volumes:
      - flexmeasures_db_data:/var/lib/postgresql/data
    networks:
      - traefik_network
    mem_limit: 1g
    cpus: 1

  # ─── Redis (queue) ─────────────────────────────────────────────
  flexmeasures-redis:
    image: redis:7-alpine
    container_name: flexmeasures-redis
    restart: unless-stopped
    command: redis-server --loglevel warning --requirepass ${FLEXMEASURES_REDIS_PASSWORD:-Digitribe972} --aclfile /dev/null
    volumes:
      - flexmeasures_redis_data:/data
    networks:
      - traefik_network
    mem_limit: 512m
    cpus: 0.5

  # ─── FlexMeasures Server ───────────────────────────────────────
  flexmeasures-server:
    image: lfenergy/flexmeasures:latest
    container_name: flexmeasures-server
    restart: unless-stopped
    depends_on:
      - flexmeasures-db
      - flexmeasures-redis
    environment:
      SQLALCHEMY_DATABASE_URI: "postgresql://flexmeasures:${FLEXMEASURES_DB_PASSWORD:-Digitribe972}@flexmeasures-db:5432/flexmeasures"
      SECRET_KEY: ${FLEXMEASURES_SECRET_KEY:-flexmeasures-secret-key-change-me}
      SECURITY_TOTP_SECRETS: '{"1": "totp-secret-change-me"}'
      FLEXMEASURES_ENV: production
      FLEXMEASURES_REDIS_URL: "flexmeasures-redis"
      FLEXMEASURES_REDIS_PORT: "6379"
      FLEXMEASURES_REDIS_DB_NR: "0"
      FLEXMEASURES_REDIS_PASSWORD: "${FLEXMEASURES_REDIS_PASSWORD:-Digitribe972}"
      LOGGING_LEVEL: INFO
      FLEXMEASURES_ALLOW_DATA_DELETION: "true"
      FLEXMEASURES_PUBLIC_ACCOUNTS: "true"
    networks:
      - traefik_network
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.cariflex.rule=Host(`cariflex.digitribe.fr`)"
      - "traefik.http.routers.cariflex.entrypoints=websecure"
      - "traefik.http.routers.cariflex.tls.certresolver=letsencrypt"
      - "traefik.http.services.cariflex.loadbalancer.server.port=5000"
      - "traefik.http.routers.cariflex.middlewares=redirect-https"
    mem_limit: 2g
    cpus: 2

  # ─── FlexMeasures Worker (jobs) ────────────────────────────────
  flexmeasures-worker:
    image: lfenergy/flexmeasures:latest
    container_name: flexmeasures-worker
    restart: unless-stopped
    depends_on:
      - flexmeasures-db
      - flexmeasures-redis
      - flexmeasures-server
    environment:
      SQLALCHEMY_DATABASE_URI: "postgresql://flexmeasures:${FLEXMEASURES_DB_PASSWORD:-Digitribe972}@flexmeasures-db:5432/flexmeasures"
      SECRET_KEY: ${FLEXMEASURES_SECRET_KEY:-flexmeasures-secret-key-change-me}
      SECURITY_TOTP_SECRETS: '{"1": "totp-secret-change-me"}'
      FLEXMEASURES_ENV: production
      FLEXMEASURES_REDIS_URL: "flexmeasures-redis"
      FLEXMEASURES_REDIS_PORT: "6379"
      FLEXMEASURES_REDIS_DB_NR: "0"
      FLEXMEASURES_REDIS_PASSWORD: "${FLEXMEASURES_REDIS_PASSWORD:-Digitribe972}"
      LOGGING_LEVEL: INFO
    networks:
      - traefik_network
    mem_limit: 2g
    cpus: 2

volumes:
  flexmeasures_db_data:
    driver: local
  flexmeasures_redis_data:
    driver: local

networks:
  traefik_network:
    name: traefik-public
    external: true
