FROM node:24-alpine AS frontend-build
WORKDIR /build/frontend
COPY frontend/package*.json ./
RUN npm ci
COPY frontend/ ./
RUN npm run build

FROM python:3.14-slim AS runtime
ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    APP_ENV=production \
    APP_HOST=0.0.0.0 \
    APP_PORT=8765 \
    APP_DATA_DIR=/app/data \
    APP_FRONTEND_DIST=/app/frontend/dist
WORKDIR /app
RUN addgroup --system xiaobai && adduser --system --ingroup xiaobai xiaobai
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY backend/ ./backend/
COPY config/ ./config/
COPY tools/ ./tools/
COPY --from=frontend-build /build/frontend/dist ./frontend/dist
RUN mkdir -p /app/data && chown -R xiaobai:xiaobai /app
USER xiaobai
EXPOSE 8765
HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
  CMD ["python", "-c", "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8765/api/health', timeout=3)"]
CMD ["python", "-m", "uvicorn", "backend.main:app", "--host", "0.0.0.0", "--port", "8765", "--proxy-headers", "--no-server-header"]
