services:
  postgresql:
    image: postgres:16-alpine
    container_name: meal-planner-postgres
    restart: unless-stopped
    environment:
      POSTGRES_DB: payload-example-multi-tenant
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
    ports:
      - "5433:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    networks:
      - meal-planner-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5

  minio:
    image: minio/minio:latest
    container_name: meal-planner-minio
    restart: unless-stopped
    command: server /data --console-address ":9001"
    environment:
      MINIO_ROOT_USER: minioadmin
      MINIO_ROOT_PASSWORD: minioadmin
    ports:
      - "9100:9000"
      - "9101:9001"
    volumes:
      - minio_data:/data
    networks:
      - meal-planner-network
    healthcheck:
      test: ["CMD", "mc", "ready", "local"]
      interval: 10s
      timeout: 5s
      retries: 5

  minio-setup:
    image: minio/mc:latest
    container_name: meal-planner-minio-setup
    depends_on:
      minio:
        condition: service_healthy
    entrypoint: >
      /bin/sh -c "
      mc alias set myminio http://minio:9000 minioadmin minioadmin;
      mc mb myminio/meal-planner --ignore-existing;
      mc anonymous set download myminio/meal-planner;
      echo 'MinIO bucket setup complete';
      "
    networks:
      - meal-planner-network

  app:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: meal-planner-app
    restart: unless-stopped
    environment:
      DATABASE_URI: postgres://postgres:postgres@postgresql:5432/payload-example-multi-tenant
      PAYLOAD_SECRET: e0d8e16cc9b401535ecf6c6f
      PAYLOAD_PUBLIC_SERVER_URL: http://localhost:3100
      SEED_DB: "true"
      NODE_ENV: production
      # MinIO/S3 configuration
      MINIO_ENDPOINT: http://minio:9000
      S3_BUCKET: meal-planner
      S3_REGION: us-east-1
      S3_ACCESS_KEY_ID: minioadmin
      S3_SECRET_ACCESS_KEY: minioadmin
      # OpenAI API key for form image analysis
      OPENAI_API_KEY: ${OPENAI_API_KEY:-}
    ports:
      - "3100:3000"
    depends_on:
      postgresql:
        condition: service_healthy
      minio:
        condition: service_healthy
      minio-setup:
        condition: service_completed_successfully
    networks:
      - meal-planner-network

volumes:
  postgres_data:
  minio_data:

networks:
  meal-planner-network:
    driver: bridge
