# GnommoEditor — standalone development
# Usage: docker compose up

services:
  db:
    image: postgres:16-alpine
    container_name: gnommoeditor-db
    restart: unless-stopped
    environment:
      POSTGRES_DB: gnommoeditor
      POSTGRES_USER: gnommoeditor
      POSTGRES_PASSWORD: gnommoeditor_secret
    volumes:
      - postgres_data:/var/lib/postgresql/data
    ports:
      - "5433:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U gnommoeditor"]
      interval: 5s
      timeout: 5s
      retries: 5

  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: gnommoeditor-backend
    restart: unless-stopped
    # Mount source so changes are picked up by --watch without rebuilding.
    # node_modules stay in the container image (not mounted).
    volumes:
      - ./backend/src:/app/src
      - ./backend/migrations:/app/migrations
    command: npm run dev
    environment:
      DATABASE_URL: postgresql://gnommoeditor:gnommoeditor_secret@db:5432/gnommoeditor?sslmode=disable
      INGEST_API_KEY: ${INGEST_API_KEY:-dev-ingest-key-change-me}
      JWT_SECRET: ${JWT_SECRET:-dev-jwt-secret-change-me}
      CORS_ORIGIN: http://localhost:5173
      PORT: 3001
      MINIO_ENDPOINT: http://minio:9000
      MINIO_PUBLIC_URL: http://localhost:9000
      MINIO_BUCKET: glitch-university
      MINIO_ROOT_USER: ${MINIO_ROOT_USER:-minioadmin}
      MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD:-minioadmin}
    ports:
      - "3001:3001"
    depends_on:
      db:
        condition: service_healthy
      minio:
        condition: service_healthy

  minio:
    image: minio/minio:latest
    container_name: gnommoeditor-minio
    restart: unless-stopped
    environment:
      MINIO_ROOT_USER: ${MINIO_ROOT_USER:-minioadmin}
      MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD:-minioadmin}
    command: server /data --console-address ":9001"
    ports:
      - "9000:9000"   # S3 API
      - "9001:9001"   # Web console  http://localhost:9001
    volumes:
      - minio_data:/data
    healthcheck:
      test: ["CMD", "mc", "ready", "local"]
      interval: 10s
      timeout: 5s
      retries: 5

  minio-setup:
    image: minio/mc
    depends_on:
      minio:
        condition: service_healthy
    entrypoint: >
      /bin/sh -c "
      mc alias set local http://minio:9000 $${MINIO_ROOT_USER:-minioadmin} $${MINIO_ROOT_PASSWORD:-minioadmin};
      mc mb --ignore-existing local/glitch-university;
      mc anonymous set download local/glitch-university;
      echo 'MinIO bucket ready.';
      exit 0;
      "
    restart: on-failure

volumes:
  postgres_data:
  minio_data:
