#syntax=docker/dockerfile:1
FROM python:3.12-slim AS base

WORKDIR /app

RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
    --mount=type=cache,target=/var/lib/apt,sharing=locked \
    # prevent apt cache cleanup to be able to use cache mount | or maybe its not worth it?
    rm /etc/apt/apt.conf.d/docker-clean && \
    apt-get update && apt-get --no-install-recommends install -y \
    build-essential \
    curl \
    git

RUN --mount=type=cache,target=/tmp/cache/pip \
    --mount=type=bind,source=requirements.txt,target=requirements.txt \
    --mount=type=bind,source=requirements-base.txt,target=requirements-base.txt \
    pip install -r requirements.txt --cache-dir=/tmp/cache/pip

FROM base AS prod

COPY . .

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PYTHONPATH=/app:$PYTHONPATH \
    HF_HOME=/app/storage/huggingface \
    REFLECTOMETRY_ROOT_DIR=/app/storage/reflectometry \
    VIPR_RESULTS_DIR=/app/storage/results \
    VIPR_UPLOAD_DIR=/app/storage/uploads \
    VIPR_CONFIG_DIR=/app/storage/config \
    VIPR_PLUGIN_CONFIG=/app/storage/plugins/vipr-plugins.yaml \
    MPLCONFIGDIR=/app/storage/.matplotlib \
    TORCHINDUCTOR_CACHE_DIR=/app/storage/.torch_cache

# ✅ create storage directories with correct permissions
RUN mkdir -p /app/storage/huggingface \
    /app/storage/reflectometry \
    /app/storage/reflectometry/reflectorch \
    /app/storage/reflectometry/flow_models \
    /app/storage/reflectometry/panpe \
    /app/storage/results \
    /app/storage/uploads \
    /app/storage/config \
    /app/storage/plugins \
    /app/storage/.torch_cache && \
    chown -R 1001:1001 /app/storage

USER 1001

EXPOSE 8000

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