# Define a build argument to choose architecture
ARG BASE_ARCH=AMD64
ARG BASE_VERSION=0.104.0

# Define the base image dynamically
FROM ghcr.io/microsoft/documentdb/documentdb-oss:ubuntu22.04-PG17-${BASE_ARCH}-${BASE_VERSION} AS stage

# Install dependencies
RUN sudo apt-get update && \
    sudo apt-get install -y --no-install-recommends \
    jq curl sudo git make build-essential openssl pkg-config libssl-dev \
    && sudo rm -rf /var/lib/apt/lists/*

USER documentdb
WORKDIR /home/documentdb/code

COPY . /home/documentdb/code

RUN sudo chown -R documentdb:documentdb /home/documentdb/code

# Install rustup (which includes rustc and cargo) in user directory
ENV RUSTUP_HOME=/home/documentdb/.rustup \
    CARGO_HOME=/home/documentdb/.cargo \
    PATH=/home/documentdb/.cargo/bin:$PATH

RUN . /home/documentdb/code/scripts/install_rustup.sh

RUN rustc --version && cargo --version

WORKDIR /home/documentdb/code/pg_documentdb_gw

# Compile the code
RUN cargo build --profile=release-with-symbols

#------------------------------------------------------------------------------------------------

# Same logic for the final image
ARG BASE_ARCH=AMD64
ARG BASE_VERSION=0.104.0
FROM ghcr.io/microsoft/documentdb/documentdb-oss:ubuntu22.04-PG17-${BASE_ARCH}-${BASE_VERSION} AS final

# Set postgres user and group to desired UID/GID
RUN sudo groupmod -g 103 postgres && sudo usermod -u 105 -g 103 postgres

RUN sudo apt-get update && \
    sudo apt-get install -y --no-install-recommends \
    jq openssl lsof && \
    sudo apt-get upgrade -y && \
    sudo rm -rf /var/lib/apt/lists/*

ENV LANGUAGE=en_US.UTF-8 \
    TERM=xterm-256color

ENV CERT_PATH="" \
    KEY_FILE="" \
    DATA_PATH="/data" \
    DOCUMENTDB_PORT="10260" \
    ENABLE_TELEMETRY="false" \
    LOG_LEVEL="info" \
    USERNAME="default_user" \
    CREATE_USER="true" \
    START_POSTGRESQL="true" \
    POSTGRESQL_PORT="9712" \
    OWNER="documentdb" \
    ALLOW_EXTERNAL_CONNECTIONS="false" \
    PATH=/usr/lib/postgresql/16/bin:$PATH

RUN sudo mkdir /home/documentdb/gateway

COPY --from=stage /home/documentdb/code/pg_documentdb_gw/target/release-with-symbols/documentdb_gateway \
                  /home/documentdb/gateway/pg_documentdb_gw/target/release-with-symbols/documentdb_gateway
COPY --from=stage /home/documentdb/code/pg_documentdb_gw/SetupConfiguration.json /home/documentdb/gateway/pg_documentdb_gw/SetupConfiguration.json
COPY --from=stage /home/documentdb/code/scripts/start_oss_server.sh /home/documentdb/gateway/scripts/start_oss_server.sh
COPY --from=stage /home/documentdb/code/scripts/build_and_start_gateway.sh /home/documentdb/gateway/scripts/build_and_start_gateway.sh
COPY --from=stage /home/documentdb/code/scripts/emulator_entrypoint.sh /home/documentdb/gateway/scripts/emulator_entrypoint.sh
COPY --from=stage /home/documentdb/code/scripts/utils.sh /home/documentdb/gateway/scripts/utils.sh
COPY --from=stage /home/documentdb/code/scripts/setup_psqlrc.sh /home/documentdb/gateway/scripts/setup_psqlrc.sh

RUN sudo chown -R documentdb:documentdb /home/documentdb/gateway

WORKDIR /home/documentdb/gateway/scripts
ENTRYPOINT ["/bin/bash", "-c", "/home/documentdb/gateway/scripts/emulator_entrypoint.sh \"$@\"", "--"]
