# --- Stage 1: Builder ---
FROM openfoam/openfoam10-paraview510:latest AS builder
USER root

# Install base utilities (including ca-certificates for SSL)
RUN apt-get update \
    && apt-get install -y \
       build-essential \
       wget \
       git \
       python3 \
       python3-pip \
       libboost-all-dev \
       ca-certificates \
    && rm -rf /var/lib/apt/lists/*

# Copy the Miniconda installer into the image
COPY Miniconda3-latest-Linux-x86_64.sh /tmp/

# Install Miniconda without auto-initialization
ENV CONDA_DIR=/opt/conda
ENV PATH=$CONDA_DIR/bin:$PATH
RUN bash /tmp/Miniconda3-latest-Linux-x86_64.sh -b -p "$CONDA_DIR" \
    && rm -f /tmp/Miniconda3-latest-Linux-x86_64.sh \
    && ln -s "$CONDA_DIR/bin/conda" /usr/bin/conda \
    && conda --version

# Environment variables
ENV FoamBench_PATH="/home/openfoam/FoamBench"
ENV MetaOpenFOAM_PATH="$FoamBench_PATH/algorithm/MetaOpenFOAM"
ENV MetaGPT_PATH="$MetaOpenFOAM_PATH/MetaGPT"

# Copy FoamBench sources into working dir
WORKDIR $FoamBench_PATH
COPY cfdllmbench/FoamBench/ ./

# Copy MetaOpenFOAM sources into working dir
WORKDIR $MetaOpenFOAM_PATH
COPY MetaOpenFOAM/ ./

# Switch to bash to source OpenFOAM environment
SHELL ["/bin/bash", "-c"]

# First install run: install all components, skip completed on reruns
RUN chmod +x install_metaopenfoam.sh && \
    ./install_metaopenfoam.sh 2>&1 | tee /tmp/metaopenfoam_install1.log && \
    [ -d "metaopenfoam_env" ] \
       && echo "MetaOpenFOAM first installation successful" \
       || { echo "ERROR: First install failed!"; cat /tmp/metaopenfoam_install1.log; exit 1; }

# Retry install to pick up any remaining steps per FAQ
RUN ./install_metaopenfoam.sh 2>&1 | tee /tmp/metaopenfoam_install2.log && \
    echo "MetaOpenFOAM second installation (retry) completed"

# Install extra libraries into metaopenfoam_env without activation
RUN conda config --add channels conda-forge && \
    conda install --prefix $MetaOpenFOAM_PATH/metaopenfoam_env --yes \
      pyvista \
      rouge-score && \
    echo "Extras added to metaopenfoam_env"

# Source OpenFOAM and verify
RUN source /opt/openfoam10/etc/bashrc && \
    echo "WM_PROJECT_DIR: $WM_PROJECT_DIR"

RUN conda clean --all --yes && \
    rm -rf "$CONDA_DIR/pkgs" && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# --- Stage 2: Runtime ---
FROM openfoam/openfoam10-paraview510:latest
USER root

# Install runtime dependencies
RUN apt-get update \
    && apt-get install -y \
       git \
       libboost-all-dev \
       ca-certificates \
    && rm -rf /var/lib/apt/lists/*

# Environment variables in a FoamBench container
ENV FoamBench_PATH="/home/openfoam/FoamBench"
ENV MetaOpenFOAM_PATH="$FoamBench_PATH/algorithm/MetaOpenFOAM"
ENV MetaGPT_PATH="$MetaOpenFOAM_PATH/MetaGPT"
ENV CONDA_DIR="/opt/conda"
ENV PATH=$CONDA_DIR/bin:$PATH

# Copy Conda runtime and FoamBench from builder
COPY --from=builder $CONDA_DIR $CONDA_DIR
COPY --from=builder $FoamBench_PATH $FoamBench_PATH


# ======================================================================================================
# Uncomment to exclude root access but create a larger image
# ======================================================================================================
# # Ensure openfoam user owns entire home directory
# RUN chown -R openfoam:openfoam /home/openfoam

# # Switch to non-root user
# USER openfoam
# ======================================================================================================

# Set default working directory
WORKDIR /home/openfoam

# The key difference - using the working entrypoint configuration
ENTRYPOINT ["/bin/bash", "-lc", "source /opt/conda/etc/profile.d/conda.sh && exec bash"]
