FROM python:3.12-slim-bookworm

WORKDIR /app

# Install system deps, download frontend assets, purge curl in one layer
RUN apt-get update && apt-get install -y --no-install-recommends curl \
    && mkdir -p /app/imptune/static \
    && curl -sL --fail -o /app/imptune/static/pico.min.css \
       "https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css" \
    && curl -sL --fail -o /app/imptune/static/htmx.min.js \
       "https://unpkg.com/htmx.org@2/dist/htmx.min.js" \
    && curl -sL --fail -o /app/imptune/static/alpine.min.js \
       "https://cdn.jsdelivr.net/npm/alpinejs@3/dist/cdn.min.js" \
    && apt-get purge -y curl && apt-get autoremove -y \
    && rm -rf /var/lib/apt/lists/*

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY imptune/ /app/imptune/

VOLUME ["/data"]

HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
    CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/health')" || exit 1

EXPOSE 8000

CMD ["uvicorn", "imptune.main:app", "--host", "0.0.0.0", "--port", "8000"]
