# syntax=docker/dockerfile:1
ARG PYTHON_VERSION=undefined
FROM plone/server-builder:uv-${PYTHON_VERSION} AS builder

# Install project dependencies based on uv.lock and pyproject.toml
# We expect pyproject.toml to have an entry cointainer inside [dependency-groups]
RUN --mount=type=cache,target=/root/.cache \
    --mount=type=bind,source=uv.lock,target=uv.lock \
    --mount=type=bind,source=pyproject.toml,target=pyproject.toml \
    uv sync \
        --locked \
        --no-dev \
        --no-group test \
        --group container \
        --no-install-project

COPY . /src
WORKDIR /src

# Install package
RUN --mount=type=cache,target=/root/.cache \
    uv sync \
        --locked \
        --no-dev \
        --no-group test \
        --group container \
        --no-editable

# Move skeleton files to /app
RUN <<EOT
    set -e
    mv /app_skeleton/* /app
    rm -Rf /app_skeleton
    chmod +x /app/docker-entrypoint.sh
    mv scripts/default.json /app/scripts/default.json
    mv scripts/create_site.py /app/scripts/create_site.py
EOT

# Compile translation files
RUN <<EOT
    /app/bin/python /compile_mo.py
EOT

FROM plone/server-prod-config:uv-${PYTHON_VERSION}

LABEL maintainer="PloneGov-BR <gov@plone.org.br>" \
      org.label-schema.name="intranet-backend" \
      org.label-schema.description="PortalBrasil: Intranet backend image." \
      org.label-schema.vendor="PloneGov-BR"

ENV ALLOWED_DISTRIBUTIONS=portalbrasil-intranet

# Copy the pre-built `/app` directory to the runtime container
# and change the ownership to user app and group app in one step.
COPY --from=builder --chown=500:500 /app /app

RUN <<EOT
    ln -s /data /app/var
    chown -R 500:500 /data
EOT
