services:
  postgres:
    build:
      context: ../../
      dockerfile: Dockerfile.demo
    environment:
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: postgres
    volumes:
      - ./postgres/init.sql:/docker-entrypoint-initdb.d/init.sql
    ports:
      - "5432:5432"
    command: >
      postgres
        -c shared_preload_libraries=pg_trickle
        -c listen_addresses='*'
        -c pg_trickle.ducklake_sink_s3_endpoint=http://minio:9000
        -c pg_trickle.ducklake_sink_s3_region=us-east-1
        -c pg_trickle.ducklake_sink_s3_access_key=minioadmin
        -c pg_trickle.ducklake_sink_s3_secret_key=minioadmin
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      timeout: 5s
      retries: 10

  minio:
    image: minio/minio:latest
    command: server /data --console-address ":9001"
    environment:
      MINIO_ROOT_USER: minioadmin
      MINIO_ROOT_PASSWORD: minioadmin
    ports:
      - "9000:9000"
      - "9001:9001"
    volumes:
      - minio_data:/data
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
      interval: 5s
      timeout: 5s
      retries: 10

  minio-setup:
    image: minio/mc:latest
    depends_on:
      minio:
        condition: service_healthy
    entrypoint: >
      /bin/sh -c "
      mc alias set local http://minio:9000 minioadmin minioadmin;
      mc mb --ignore-existing local/pg-trickle-demo;
      mc anonymous set download local/pg-trickle-demo;
      "

  generator:
    image: python:3.12-slim
    depends_on:
      postgres:
        condition: service_healthy
    volumes:
      - ./generator/generate_scores.py:/app/generate_scores.py
    working_dir: /app
    command: >
      bash -c "pip install psycopg2-binary --quiet &&
               python generate_scores.py"
    environment:
      PG_DSN: "host=postgres dbname=postgres user=postgres password=postgres"

  grafana:
    image: grafana/grafana:latest
    depends_on:
      postgres:
        condition: service_healthy
    ports:
      - "3000:3000"
    environment:
      GF_SECURITY_ADMIN_PASSWORD: admin
      GF_SECURITY_ADMIN_USER: admin
      GF_AUTH_ANONYMOUS_ENABLED: "true"
      GF_AUTH_ANONYMOUS_ORG_ROLE: Viewer
    volumes:
      - ./grafana/provisioning:/etc/grafana/provisioning:ro
      - ./grafana/dashboards:/var/lib/grafana/dashboards:ro

volumes:
  minio_data:
