# SET50 alternative-data platform - single-container image.
# Serves: built Vue SPA (frontend/dist) via nginx, proxying /api to the
# Flask backend (127.0.0.1:5000). Data persists under /app/backend/data
# (mount a volume there or set DATA_DIR to a mounted path).
#
# Build strategy: PREBUILT frontend dist (no node/npm/vite in this image) —
# build with `cd frontend && npm run build` and commit frontend/dist/.
FROM python:3.11-slim

# nginx for serving the SPA
RUN apt-get update && apt-get install -y --no-install-recommends nginx \
    && rm -rf /var/lib/apt/lists/* \
    && rm -f /etc/nginx/sites-enabled/default /etc/nginx/conf.d/default.conf 2>/dev/null || true

WORKDIR /app

# 1) frontend static (prebuilt) — must exist on-disk (committed)
COPY frontend/dist/ ./frontend/dist/

# 2) backend deps + code
COPY backend/requirements.txt ./backend/requirements.txt
RUN pip install --no-cache-dir -r backend/requirements.txt
COPY backend/ ./backend/

# 3) deploy config (nginx conf + entrypoint)
COPY deploy/nginx.conf /etc/nginx/conf.d/set50.conf
COPY deploy/entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh

# persistent data (factor vintages, dividend ledger, forward runs, prices)
ENV DATA_DIR=/app/backend/data
ENV HOST=0.0.0.0
ENV PORT=5000
VOLUME ["/app/backend/data"]

EXPOSE 80
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
  CMD python -c "import urllib.request,sys; sys.exit(0 if urllib.request.urlopen('http://127.0.0.1/api/v1/dashboard/summary', timeout=4).status==200 else 1)"

ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
