# Dockerfile for E2E testing
FROM ubuntu:24.04

# Set environment variables
ENV DEBIAN_FRONTEND=noninteractive
ENV NODE_VERSION=22.x

#ARG LEAN_TOOLCHAIN=leanprover/lean4:v4.33.0
ARG LEAN_TOOLCHAIN=leanprover/lean4:v4.34.0-rc2

#ARG TZ=America/New_York
ENV LANG=C.UTF-8
ENV LC_ALL=C.UTF-8

# Install system dependencies for Playwright

RUN --mount=type=cache,target=/var/lib/apt/lists,id=apt-cache-lists \
    --mount=type=cache,target=/var/cache/apt/archives,id=apt-cache-archives \
    (echo "ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true" | debconf-set-selections) && \
    apt-get update && apt-get install -y \
    \
    sudo \
    procps \
    bash-completion \
    zsh \
    \
    ca-certificates \
    curl \
    wget \
    git \
    rsync \
    git-lfs \
    \
    ripgrep \
    \
    xxd \
    jq \
    less \
    unzip \
    xz-utils \
    zip \
    \
    build-essential \
    pkg-config \
    llvm \
    libclang-dev \
    libbz2-dev \
    libffi-dev \
    liblzma-dev \
    libncurses5-dev \
    libncursesw5-dev \
    libreadline-dev \
    libsqlite3-dev \
    libssl-dev \
    libxml2-dev \
    libxmlsec1-dev \
    tk-dev \
    zlib1g-dev \
    # Playwright specific dependencies
    libfontenc1 libgl1 libice6 libsm6 libxaw7 libxfont2 libxkbfile1 libxmu6 \
    libxmuu1 libxpm4 libxt6t64 x11-xkb-utils xauth xfonts-encodings xfonts-utils \
    xserver-common \
    libfontconfig1 \
    libjpeg-turbo8 \
    libx11-6 \
    libxcomposite1 \
    libxdamage1 \
    libxext6 \
    libxfixes3 \
    libxrandr2 \
    libxrender1 \
    libxtst6 \
    libnss3 \
    libatk1.0-0 \
    libatk-bridge2.0-0 \
    libcups2 \
    libdrm2 \
    libgbm1 \
    libglib2.0-0 \
    libgtk-3-0 \
    libnspr4 \
    libxcb1 \
    libxkbcommon0 \
    libxshmfence1 \
    libasound2t64 \
    libatspi2.0-0 \
    libgdk-pixbuf2.0-0 \
    libgstreamer-plugins-base1.0-0 \
    libgstreamer1.0-0 \
    libharfbuzz0b \
    libsecret-1-0 \
    libvulkan1 \
    libwebpdemux2 \
    libwoff1 \
    libwebp7 \
    libenchant-2-2 \
    libepoxy0 \
    libgomp1 \
    libhyphen0 \
    libjpeg8 \
    liblcms2-2 \
    libopenjp2-7 \
    libpng16-16 \
    libpoppler-glib8 \
    librsvg2-2 \
    libthai0 \
    libtiff6 \
    libwebpdemux2 \
    libwoff1 \
    libxml2 \
    libxslt1.1 \
    libxss1 \
    libxtst6 \
    libegl1 \
    libgles2 \
    libglx-mesa0 \
    libglx0 \
    libsqlite3-0 \
    libvulkan1 \
    libwebpdemux2 \
    libwoff1 \
    libxml2 \
    libxslt1.1 \
    libxss1 \
    libxtst6 \
    \
    libpulse0 \
    pulseaudio-utils \
    \
    xvfb \
    fonts-freefont-ttf fonts-ipafont-gothic fonts-liberation \
    fonts-noto-color-emoji fonts-tlwg-loma-otf fonts-unifont fonts-wqy-zenhei \
    xfonts-cyrillic xfonts-scalable \
    \
    python-is-python3 \
    python3-argcomplete \
    python3-pip \
    python3-venv \
    python3-virtualenvwrapper \
    pipx \
    # uv \
    \
    python3-pytest \
    python3-pytest-mock \
    python3-pytest-cov \
    python3-pytest-xdist \
    python3-hypothesis \
    \
    libzmq3-dev \
    \
    podman-remote \
    \
    --no-install-recommends && \
    rm -rf /var/lib/apt/lists/*

# Install PDF inspection tooling outside the project and system Python envs.
RUN python3 -m venv /opt/pdf-tools && \
    /opt/pdf-tools/bin/pip install --no-cache-dir pymupdf==1.28.2

ENV PATH="/opt/pdf-tools/bin:${PATH}"

# Install Node.js and npm
RUN curl -fsSL https://deb.nodesource.com/setup_$NODE_VERSION | bash - && \
    apt-get install -y nodejs && \
    rm -rf /var/lib/apt/lists/*

# Create a non-root user
ARG USERNAME=appuser
ARG USER_UID=1000
ARG USER_GID=$USER_UID
RUN (set -x; \
    userdel -r ubuntu \
    && groupadd --gid "$USER_GID" "$USERNAME" \
    && useradd --uid "$USER_UID" --gid "$USER_GID" -m "$USERNAME" \
    && echo "$USERNAME ALL=(root) NOPASSWD:ALL" > "/etc/sudoers.d/$USERNAME" \
    && chmod 0440 "/etc/sudoers.d/$USERNAME")

# Create app directory and set permissions
RUN mkdir -p /app && chown -R "$USERNAME:$USERNAME" /app

# Pre-create XDG_RUNTIME_DIR to prevent root ownership from Docker volume mounts
RUN mkdir -p /run/user/$USER_UID && chown -R "$USERNAME:$USERNAME" /run/user/$USER_UID


USER $USERNAME
RUN (set -x; \
    echo "# setup user homedir" \
    && mkdir -p "/home/$USERNAME/.npm" && ls -al "/home/$USERNAME/.npm" \
    && mkdir -p "/home/$USERNAME/.cache/ms-playwright")
    #&& chown -R 1000:1000 "/home/$USERNAME/.npm" \
    #&& chown -Rv "$USERNAME:$USERNAME" "/home/$USERNAME" \

# Install the Lean toolchain used by the Signals project.
ENV ELAN_HOME=/home/appuser/.elan
ENV PATH=/home/appuser/.elan/bin:${PATH}
# Cache the complete Elan home, then copy it into the image-backed installation.
USER root
RUN mkdir -p "$ELAN_HOME" /mnt/elan-cache && \
    chown -R "$USERNAME:$USERNAME" "$ELAN_HOME" /mnt/elan-cache
USER $USERNAME
RUN --mount=type=cache,target=/mnt/elan-cache,uid=1000,gid=1000,id=elan-home-cache,sharing=locked \
    export ELAN_HOME=/mnt/elan-cache; \
    export PATH="/mnt/elan-cache/bin:$PATH"; \
    curl https://elan.lean-lang.org/elan-init.sh -sSf | sh -s -- \
    --default-toolchain "$LEAN_TOOLCHAIN" -y && \
    elan toolchain install "$LEAN_TOOLCHAIN" && \
    elan default "$LEAN_TOOLCHAIN" && \
    rm -rf /home/appuser/.elan && \
    mkdir -p /home/appuser/.elan && \
    cp -a /mnt/elan-cache/. /home/appuser/.elan/ && \
    lean --version && \
    lake --version

# Switch to the non-root user
WORKDIR /app

# Copy only package files first for better cache usage
COPY --chown=appuser:appuser package.json package-lock.json /app/
COPY --chown=appuser:appuser e2e/package.json e2e/package-lock.json /app/e2e/

# Install Playwright browsers matching package.json version
ENV DEBIAN_FRONTEND=noninteractive
ENV PLAYWRIGHT_BROWSERS_PATH=/home/appuser/.cache/ms-playwright

RUN --mount=type=cache,target=/var/lib/apt/lists,id=apt-cache-lists \
    --mount=type=cache,target=/var/cache/apt/archives,id=apt-cache-archives \
    --mount=type=cache,target=/tmp/pw-cache,uid=1000,gid=1000,id=pw-cache \
    set -x -e; \
        npm install; \
        if [ -f /app/e2e/package.json ]; then cd /app/e2e && npm install && cd ..; fi; \
        PLAYWRIGHT_BROWSERS_PATH=/tmp/pw-cache npx playwright install --with-deps chromium firefox; \
        rsync -a /tmp/pw-cache/ /home/appuser/.cache/ms-playwright/; \
        npx playwright install --list; \
        echo "Playwright install complete"


# Copy the rest of the project files and set ownership
# USER root
# COPY --chown=appuser:appuser . /app

# RUN ls -al /home/appuser/.cache/**

# Switch to root user to change ownership
# USER root
# # Change ownership of /app to the non-root user
# RUN chgrp -R $USERNAME /app && \
#     chmod -R g+w /app


# Switch to the non-root user
USER $USERNAME
# COPY --chown=$USERNAME package.json /app/${PROJECT_NAME:-my-project}/

# Install Next.js dependencies
WORKDIR /app
#RUN --mount=type=cache,target=/home/appuser/.npm,id=npm-cache \
# RUN set -x +e; \
#     ls -al /home/appuser/.npm/*; \
#     ls -al . ; \
#     test -f package.json ; \
#     npm install --dev; \
#     test -d /home/appuser/.npm/_logs || mkdir -p /home/appuser/.npm/_logs; \
#     cat /home/appuser/.npm/_logs/**

# Install Playwright for E2E only (using e2e/package.json)
#WORKDIR /app/e2e
#RUN --mount=type=cache,target=/home/appuser/.npm,id=npm-cache \
#RUN --mount=type=cache,target=/home/$USERNAME/.cache/ms-playwright,id=playwright \
#RUN npm install

# Set working directory (just like devcontainers defaults)
#WORKDIR /workspaces

VOLUME /home/appuser/.npm/_logs
#VOLUME /home/appuser/.cache/ms-playwright
VOLUME /home/appuser/e2e-logs


# Explanation of /app vs /workspaces/<project-name>:
# - /app: Used during container BUILD time. Standard `docker build` does not support
#   bind-mounting host directories (unlike `podman build`). We COPY configuration
#   files (like package.json) into /app to install/cache dependencies.
# - /workspaces/<project-name>: Used at container RUN time. DevContainers and our testing
#   scripts dynamically bind-mount the live host project repository here. This setup
#   enables live code editing and test execution without rebuilding the container image.



# Expose ports (adjust as needed for your applications)
# For Next.js dev server: 3000/tcp
EXPOSE 3000/tcp


WORKDIR /app

# Default command (can be overridden by devcontainer.json)
CMD ["/bin/bash"]
