FROM python:3.11-slim AS base

ARG DEBIAN_FRONTEND=noninteractive
ARG JRE_VERSION=17.0.18+10
ARG JRE_DEB="bellsoft-jre${JRE_VERSION}-linux-amd64-full.deb"
ARG JRE_URL=https://download.bell-sw.com/java/${JRE_VERSION}/${JRE_DEB}
ARG INSEL_VERSION=8.3.4.0b
ARG INSEL_DEB="insel_${INSEL_VERSION}_x64_mini.deb"
ARG INSEL_URL=https://insel.eu/download/${INSEL_DEB}
ARG SIMSTADT_URL=https://simstadt.hft-stuttgart.de/download/InstallFiles/SimStadt2_latest.zip

# Java 17 + INSEL + SimStadt itself.
RUN apt-get update && \
    apt-get install curl unzip --no-install-recommends -y && \
    curl ${JRE_URL} -o /tmp/${JRE_DEB} -k && \
    curl ${INSEL_URL} -o /tmp/${INSEL_DEB} -k && \
    curl ${SIMSTADT_URL} -o /tmp/simstadt.zip -k && \
    apt-get install /tmp/${JRE_DEB} /tmp/${INSEL_DEB} --no-install-recommends -y && \
    mkdir -p /opt/SimStadt2 && \
    unzip -q /tmp/simstadt.zip -d /opt/SimStadt2 && \
    apt-get remove curl unzip -y && \
    rm -rf /var/lib/apt/lists/* && \
    apt-get clean && \
    rm /tmp/${JRE_DEB} /tmp/${INSEL_DEB} /tmp/simstadt.zip

ENV SIMSTADT_FOLDER=/opt/SimStadt2

# SRA
RUN apt-get update && \
    apt-get install libglu1-mesa --no-install-recommends -y && \
    rm -rf /var/lib/apt/lists/* && \
    apt-get clean
ADD https://simstadt.hft-stuttgart.de/download/InstallFiles/citysim_sra /usr/local/bin/
RUN chmod a+rx /usr/local/bin/citysim_sra
ADD https://simstadt.hft-stuttgart.de/download/InstallFiles/libshortwave.so /usr/lib/
RUN chmod a+r /usr/lib/libshortwave.so

RUN groupadd -g 1000 simstadt \
  && useradd simstadt --create-home --shell /bin/bash -u 1000 -g 1000

COPY --from=ghcr.io/astral-sh/uv:0.12.17 /uv /uvx /usr/local/bin/

RUN mkdir -p /opt/venv && chown simstadt:simstadt /opt/venv
ENV VIRTUAL_ENV=/opt/venv
ENV UV_PROJECT_ENVIRONMENT=/opt/venv
ENV PATH="/opt/venv/bin:${PATH}"

USER simstadt
RUN mkdir -p /home/simstadt/app
WORKDIR /home/simstadt/app

FROM base AS cli

COPY --chown=simstadt:simstadt pyproject.toml uv.lock README.md ./
RUN uv sync --frozen --no-install-project

RUN uv pip install simstadt

COPY --chown=simstadt:simstadt tests ./tests

CMD ["simstadt"]
