# Build the frontend and wheel from this checkout; only the installed wheel and
# runtime dependencies enter the final image.
FROM node:22-bookworm-slim AS frontend
WORKDIR /build/web-ui
COPY web-ui/package.json web-ui/package-lock.json ./
RUN npm ci
COPY web-ui/ ./
# postbuild copies dist into /build/src/shidoshi/web/static.
RUN npm run build

FROM python:3.13-slim AS wheel
WORKDIR /build
RUN pip install --no-cache-dir hatchling
COPY pyproject.toml README.md ./
COPY src/ ./src/
COPY --from=frontend /build/src/shidoshi/web/static/ ./src/shidoshi/web/static/
RUN python -m hatchling build -t wheel

FROM python:3.13-slim AS runtime
RUN apt-get update \
    && apt-get install -y --no-install-recommends bash git ca-certificates tini \
    && rm -rf /var/lib/apt/lists/* \
    && groupadd --gid 1000 shidoshi \
    && useradd --uid 1000 --gid 1000 --create-home shidoshi \
    && install -d -o 1000 -g 1000 /workspace /data
COPY --from=wheel /build/dist/*.whl /tmp/wheels/
RUN pip install --no-cache-dir /tmp/wheels/*.whl && rm -rf /tmp/wheels
ENV SHIDOSHI_HOME=/data/shidoshi
USER 1000:1000
WORKDIR /workspace
VOLUME ["/workspace", "/data"]
EXPOSE 8420
HEALTHCHECK --interval=10s --timeout=3s --start-period=30s --retries=3 \
    CMD ["python", "-c", "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8420/healthz', timeout=2).close()"]
ENTRYPOINT ["tini", "--"]
CMD ["shidoshi", "/workspace", "--ip", "0.0.0.0", "--port", "8420", "--port-retries", "0", "--no-browser"]
