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_orders.py:/app/generate_orders.py
    working_dir: /app
    command: >
      bash -c "pip install psycopg2-binary --quiet &&
               python generate_orders.py"
    environment:
      PG_DSN: "host=postgres dbname=postgres user=postgres password=postgres"

  jupyter:
    build:
      context: ./jupyter
      dockerfile: Dockerfile
    depends_on:
      postgres:
        condition: service_healthy
    ports:
      - "8888:8888"
    environment:
      JUPYTER_TOKEN: demo
    volumes:
      - ./notebooks:/home/jovyan/work

volumes:
  minio_data:
