ARG PYTHON_VERSION=3.13
FROM python:${PYTHON_VERSION}-alpine
LABEL author="John Ahlroos <john@ahlroos.me>"
ARG POETRY_VERSION=2.3.3
ENV PYTHONFAULTHANDLER=1 \
    PYTHONUNBUFFERED=1 \
    PYTHONHASHSEED=random \
    PIP_NO_CACHE_DIR=off \
    PIP_DISABLE_PIP_VERSION_CHECK=on \
    PIP_DEFAULT_TIMEOUT=100 \
    POETRY_NO_INTERACTION=1 \
    POETRY_VIRTUALENVS_CREATE=false \
    POETRY_CACHE_DIR='/var/cache/pypoetry' \
    POETRY_HOME='/usr/local'

RUN apk add --no-cache tini
RUN pip install -Iv --no-cache-dir "poetry==${POETRY_VERSION}"

WORKDIR /app

COPY poetry.lock pyproject.toml ./
RUN poetry install --only=main --no-interaction --no-ansi

COPY . /app
VOLUME /app/data

RUN touch .streamlit/secrets.toml \
    && toml add_section --toml-path='.streamlit/secrets.toml' 'connections.sqlite' \
    && toml set --toml-path='.streamlit/secrets.toml' 'connections.sqlite.type' 'queries' \
    && toml set --toml-path='.streamlit/secrets.toml' 'connections.sqlite.url' 'sqlite:///data/daily-counter.db'

HEALTHCHECK --interval=60s --retries=5 CMD wget -qO- http://127.0.0.1:8501/_stcore/health || exit 1
ENTRYPOINT ["/sbin/tini", "--"]
CMD ["/app/entrypoint.sh"]
