services:
  postgres:
    image: pgvector/pgvector:pg16
    restart: unless-stopped
    environment:
      POSTGRES_DB: pedquiz
      POSTGRES_USER: pedquiz
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U pedquiz"]
      interval: 5s
      timeout: 5s
      retries: 10

  frontend:
    build: ./frontend
    env_file:
      - ./frontend/.env
    ports:
      - "127.0.0.1:8081:80"
    depends_on:
      - backend
    restart: unless-stopped

  backend:
    build: ./backend
    command: uvicorn app.main:app --host 0.0.0.0 --port 8000 --workers 4
    env_file:
      - ./backend/.env
    environment:
      - ANONYMIZED_TELEMETRY=False
      - LOG_LEVEL=${LOG_LEVEL:-INFO}
    volumes:
      - uploads_data:/app/uploads
      - chroma_data:/app/chroma_data
    networks:
      - default
      - speech_net
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_started
    restart: unless-stopped

  celery:
    build: ./backend
    command: celery -A app.tasks worker --loglevel=${LOG_LEVEL:-info} --concurrency=2
    env_file:
      - ./backend/.env
    environment:
      - ANONYMIZED_TELEMETRY=False
      - LOG_LEVEL=${LOG_LEVEL:-INFO}
    volumes:
      - uploads_data:/app/uploads
      - chroma_data:/app/chroma_data
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_started
    restart: unless-stopped

  redis:
    image: redis:7-alpine
    volumes:
      - redis_data:/data
    restart: unless-stopped

  # ── Logging: Loki + Promtail + Grafana ──────────────────────────────
  loki:
    image: grafana/loki:3.3.2
    restart: unless-stopped
    command: -config.file=/etc/loki/loki-config.yml
    volumes:
      - ./loki/loki-config.yml:/etc/loki/loki-config.yml:ro
      - loki_data:/loki
    ports:
      - "127.0.0.1:3100:3100"

  promtail:
    image: grafana/promtail:3.3.2
    restart: unless-stopped
    command: -config.file=/etc/promtail/promtail-config.yml
    volumes:
      - ./promtail/promtail-config.yml:/etc/promtail/promtail-config.yml:ro
      - /var/lib/docker/containers:/var/lib/docker/containers:ro
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - promtail_positions:/positions
    depends_on:
      - loki

  grafana:
    image: grafana/grafana:10.3.1
    restart: unless-stopped
    environment:
      GF_SECURITY_ADMIN_PASSWORD: ${GRAFANA_ADMIN_PASSWORD:?set GRAFANA_ADMIN_PASSWORD}
      GF_AUTH_ANONYMOUS_ENABLED: "false"
    volumes:
      - grafana_data:/var/lib/grafana
      - ./grafana/provisioning:/etc/grafana/provisioning:ro
    ports:
      - "127.0.0.1:3002:3000"
    depends_on:
      - loki

  db-backup:
    image: prodrigestivill/postgres-backup-local:16
    restart: unless-stopped
    environment:
      POSTGRES_HOST: postgres
      POSTGRES_DB: pedquiz
      POSTGRES_USER: pedquiz
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD}
      SCHEDULE: "@daily"
      BACKUP_KEEP_DAYS: 14
      BACKUP_KEEP_WEEKS: 4
      BACKUP_KEEP_MONTHS: 6
      HEALTHCHECK_PORT: 8080
    volumes:
      - ./backups:/backups
    depends_on:
      postgres:
        condition: service_healthy

volumes:
  uploads_data:
  chroma_data:
  postgres_data:
  redis_data:
  loki_data:
  grafana_data:
  promtail_positions:

networks:
  speech_net:
    external: true
