# Multi-stage build for better security and smaller image size
FROM registry.access.redhat.com/ubi10/ubi-minimal:1791784933 AS builder

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    UV_COMPILE_BYTECODE=1 \
    UV_NO_EDITABLE=1 \
    UV_LINK_MODE=copy \
    UV_PYTHON=3.11 \
    UV_PYTHON_INSTALL_DIR=/opt/python \
    UV_PROJECT_ENVIRONMENT=/app/.venv \
    VIRTUAL_ENV=/app/.venv \
    PATH="/app/.venv/bin:$PATH"

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

WORKDIR /build

RUN microdnf install -y \
    git \
    gcc \
    gcc-c++ \
    make \
    && microdnf clean all

# Create the venv at the exact path it will live at in the runtime stage
RUN uv venv /app/.venv

# Copy dependency files first for layer caching
COPY pyproject.toml uv.lock README.md ./

# Install dependencies into a virtual environment, without project source
RUN uv sync --frozen --no-install-project --no-dev

# Now copy source and install project + plugins
COPY src/ ./src/
COPY plugins ./plugins/

RUN uv sync --frozen --no-dev && \
    uv pip install --no-cache-dir \
        plugins/ares-litellm \
        plugins/ares-pyrit \
        plugins/ares-vllm-connector


# Final stage: minimal runtime image
FROM registry.access.redhat.com/ubi10/ubi-minimal:1791784933 AS runtime

WORKDIR /app

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    UV_PYTHON_INSTALL_DIR=/opt/python \
    VIRTUAL_ENV=/app/.venv \
    PATH="/app/.venv/bin:$PATH"

RUN microdnf install -y \
    shadow-utils \
    && groupadd --gid 1000 aresgroup \
    && useradd --uid 1000 --gid aresgroup ares \
    && microdnf remove -y shadow-utils \
    && microdnf clean all

COPY --from=builder --chown=ares:aresgroup /opt/python /opt/python
COPY --from=builder --chown=ares:aresgroup /app/.venv /app/.venv

COPY --chown=ares:aresgroup assets/ ./assets/
COPY --chown=ares:aresgroup example_configs/ ./example_configs/

# Create runtime directories and fix ownership/permissions in one layer.
# Directories written at runtime (results, tempdir, docs) get g+rwX with GID 0
# so OpenShift's arbitrary-UID injection (restricted SCC) can write to them.
RUN mkdir -p /app/results /app/ares_gui_tempdir /app/docs/source/_static && \
    touch /app/docs/source/_static/favicon.ico && \
    chown -R ares:root /app && \
    chmod -R g=u /app

# Switch to non-root user (numeric UID required for OpenShift SCC validation)
USER 1000

EXPOSE 8081

CMD ["ares", "gui"]
