version: '3.8'

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

  # MinIO Object Storage (S3-compatible)
  minio:
    image: minio/minio:latest
    container_name: lageplan-minio
    restart: unless-stopped
    command: server /data --console-address ":9001"
    environment:
      MINIO_ROOT_USER: ${MINIO_ROOT_USER:-minioadmin}
      MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD:-minioadmin123}
    volumes:
      - minio_data:/data
    ports:
      - "${MINIO_API_PORT:-9002}:9000"
      - "${MINIO_CONSOLE_PORT:-9003}:9001"
    healthcheck:
      test: ["CMD", "mc", "ready", "local"]
      interval: 5s
      timeout: 5s
      retries: 5

  # MinIO Bucket Initialization
  minio-init:
    image: minio/mc:latest
    container_name: lageplan-minio-init
    depends_on:
      minio:
        condition: service_healthy
    entrypoint: >
      /bin/sh -c "
      mc alias set myminio http://minio:9000 ${MINIO_ROOT_USER:-minioadmin} ${MINIO_ROOT_PASSWORD:-minioadmin123};
      mc mb myminio/${MINIO_BUCKET:-lageplan-icons} --ignore-existing;
      mc anonymous set download myminio/${MINIO_BUCKET:-lageplan-icons};
      echo 'Bucket initialized successfully';
      exit 0;
      "

  # Next.js Web Application
  web:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: lageplan-web
    restart: unless-stopped
    environment:
      DATABASE_URL: postgresql://${POSTGRES_USER:-lageplan}:${POSTGRES_PASSWORD:-lageplan_secret}@db:5432/${POSTGRES_DB:-lageplan}
      NEXTAUTH_URL: ${NEXTAUTH_URL:-http://localhost:3000}
      NEXTAUTH_SECRET: ${NEXTAUTH_SECRET:?NEXTAUTH_SECRET muss gesetzt sein (mind. 32 Zeichen) - z.B. via .env oder stack.env}
      MINIO_ENDPOINT: minio
      MINIO_PORT: 9000
      MINIO_ACCESS_KEY: ${MINIO_ROOT_USER:-minioadmin}
      MINIO_SECRET_KEY: ${MINIO_ROOT_PASSWORD:-minioadmin123}
      MINIO_BUCKET: ${MINIO_BUCKET:-lageplan-icons}
      MINIO_USE_SSL: "false"
      MINIO_PUBLIC_URL: ${MINIO_PUBLIC_URL:-http://localhost:9000}
    ports:
      - "${WEB_PORT:-3000}:3000"
    depends_on:
      db:
        condition: service_healthy
      minio:
        condition: service_healthy
    volumes:
      - ./prisma:/app/prisma

volumes:
  postgres_data:
  minio_data:
