# =============================================================
# Lageplan - Portainer Stack
# =============================================================
# Deployment:
#   1. Image auf Server laden:  docker load -i lageplan-web-v1.0.0.tar
#   2. In Portainer: Stacks → Add Stack → Web editor
#   3. Diese YAML einfügen + Environment Variables setzen
# =============================================================

services:
  # --- PostgreSQL Database ---
  db:
    image: postgres:16-alpine
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    networks:
      - lageplan-net
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER}"]
      interval: 10s
      timeout: 5s
      retries: 5

  # --- MinIO Object Storage (S3-kompatibel, für Symbole/Icons) ---
  # Kein externer Port nötig — Icons werden über die Web-App gestreamt
  minio:
    image: minio/minio:latest
    restart: unless-stopped
    command: server /data --console-address ":9001"
    environment:
      MINIO_ROOT_USER: ${MINIO_ROOT_USER}
      MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD}
    volumes:
      - minio_data:/data
    networks:
      - lageplan-net
    healthcheck:
      test: ["CMD", "mc", "ready", "local"]
      interval: 10s
      timeout: 5s
      retries: 5

  # --- MinIO Bucket Initialisierung (einmalig) ---
  minio-init:
    image: minio/mc:latest
    depends_on:
      minio:
        condition: service_healthy
    networks:
      - lageplan-net
    entrypoint: >
      /bin/sh -c "
      mc alias set myminio http://minio:9000 ${MINIO_ROOT_USER} ${MINIO_ROOT_PASSWORD};
      mc mb myminio/${MINIO_BUCKET} --ignore-existing;
      mc anonymous set download myminio/${MINIO_BUCKET};
      echo 'Bucket ready';
      exit 0;
      "

  # --- Lageplan Web App (Next.js) ---
  web:
    image: lageplan-web:v1.0.0
    restart: unless-stopped
    environment:
      DATABASE_URL: postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB}
      NEXTAUTH_URL: ${NEXTAUTH_URL}
      NEXTAUTH_SECRET: ${NEXTAUTH_SECRET}
      MINIO_ENDPOINT: minio
      MINIO_PORT: "9000"
      MINIO_ACCESS_KEY: ${MINIO_ROOT_USER}
      MINIO_SECRET_KEY: ${MINIO_ROOT_PASSWORD}
      MINIO_BUCKET: ${MINIO_BUCKET}
      MINIO_USE_SSL: "false"
    ports:
      - "${WEB_PORT:-3000}:3000"
    depends_on:
      db:
        condition: service_healthy
      minio:
        condition: service_healthy
    networks:
      - lageplan-net

volumes:
  postgres_data:
    driver: local
  minio_data:
    driver: local

networks:
  lageplan-net:
    driver: bridge
