# Stage 1: Build frontend
FROM node:22-slim AS frontend-build
WORKDIR /build
COPY frontend/package*.json ./
RUN npm ci
COPY frontend/ ./
RUN npm run build

# Stage 2: Production
FROM python:3.11-slim
WORKDIR /app

# Install system dependencies
# tzdata ist im python:3.11-slim bereits enthalten - nur gcc nachinstallieren.
RUN apt-get update && apt-get install -y --no-install-recommends \
    gcc \
    && rm -rf /var/lib/apt/lists/*

# Install Python dependencies
COPY backend/requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt gunicorn

# Copy backend
COPY backend/ ./

# Copy frontend build
COPY --from=frontend-build /build/dist ./static

# Create data directory
RUN mkdir -p /app/data/files

# Environment
ENV FLASK_ENV=production
ENV TZ=Europe/Berlin
ENV DATABASE_PATH=/app/data/minicloud.db
ENV UPLOAD_PATH=/app/data/files

EXPOSE 5000

# Single worker with many threads. The SSE broadcaster lives in process
# memory - mit mehreren Workern wuerden Events den Empfaenger nicht
# erreichen wenn Sender und Empfaenger auf verschiedenen Workern haengen.
# 32 Threads = bis zu 32 gleichzeitige Requests/SSE-Streams.
CMD ["gunicorn", "--bind", "0.0.0.0:5000", \
     "--worker-class", "gthread", "--workers", "1", "--threads", "32", \
     "--timeout", "120", "--keep-alive", "65", \
     "wsgi:application"]
