FROM python:3.12-slim

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PIP_NO_CACHE_DIR=1

WORKDIR /app

RUN pip install --no-cache-dir pipenv

COPY Pipfile Pipfile.lock ./
RUN pipenv install --system --deploy

COPY src ./src
COPY lib ./lib
RUN mkdir -p src/static/bundled-lib && cp -f lib/*.py src/static/bundled-lib/
COPY workspace ./workspace
# Sanity-check: the canonical demos under `src/static/bundled-demos/` (used by
# the editor's "Reset demos" button) must stay in sync with `workspace/code/`.
# Files in `bundled-demos/` are committed to git and copied via `COPY src`
# above; this step just fails the build if a checked-in copy drifted from the
# canonical version, so the mismatch is caught here instead of at runtime.
RUN for f in pattern_rainbow_demo.py pattern_twinkle_demo.py pattern_chase_demo.py \
             adc_slider_demo.py pin_demo.py serial_demo.py; do \
        diff -q "workspace/code/$f" "src/static/bundled-demos/$f" \
            || { echo "ERROR: $f out of sync between workspace/code/ and src/static/bundled-demos/" >&2; exit 1; }; \
    done

EXPOSE 8080

CMD ["uvicorn", "app:app", "--app-dir", "src", "--host", "0.0.0.0", "--port", "8080"]
