FROM ubuntu:noble AS peddlers-node

# Allow the caller to specify the host UID/GID so files created by the
# container match host ownership when volumes are mounted. Defaults to 1000.
ARG HOST_UID=1000
ARG HOST_GID=1000

RUN apt-get -q update \
  && DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y \
  ca-certificates \
  gnupg \
  curl \
  nano \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/{apt,dpkg,cache,log}
RUN mkdir -p /etc/apt/keyrings

RUN curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg

ENV NODE_MAJOR=22
RUN echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list

RUN apt-get -q update \
  && DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y \
  nodejs \
  sqlite3 \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/{apt,dpkg,cache,log}

RUN if ! getent group ${HOST_GID} >/dev/null 2>&1; then \
  groupadd -g ${HOST_GID} hostgroup; \
  else \
  EXISTING=$(getent group ${HOST_GID} | cut -d: -f1) && echo "Using existing group $EXISTING for GID ${HOST_GID}"; \
  fi

RUN if ! getent passwd ${HOST_UID} >/dev/null 2>&1; then \
  useradd -m -u ${HOST_UID} -g ${HOST_GID} -s /bin/bash hostuser ; \
  else \
  EXISTING_USER=$(getent passwd ${HOST_UID} | cut -d: -f1) && echo "Found existing user $EXISTING_USER with UID ${HOST_UID}"; \
  fi

RUN if [ ! -d /home/hostuser ]; then \
  mkdir -p /home/hostuser ; \
  else \
  echo "/home/hostuser already exists"; \
  fi

RUN chown -R ${HOST_UID}:${HOST_GID} /home/hostuser

ENV HOME=/home/hostuser

COPY /Dockerfile /Dockerfile

FROM peddlers-node AS pok-server

RUN { \
  echo "#!/bin/bash"; \
  echo "cd /server"; \
  echo "npm install --legacy-peer-deps --no-fund"; \
  echo "npm start"; \
  } > /entrypoint.sh && chmod +x /entrypoint.sh


USER ${HOST_UID}:${HOST_GID}

COPY --chown=${HOST_UID}:${HOST_GID} server /server
WORKDIR /server
RUN npm install -s sqlite3
RUN if [ -f package-lock.json ]; then \
  echo "package-lock.json found, running npm ci"; \
  npm ci --legacy-peer-deps --no-audit --no-fund; \
  else \
  echo "No package-lock.json found, running npm install"; \
  npm install --legacy-peer-deps --no-audit --no-fund; \
  fi
RUN npm run build

CMD ["/entrypoint.sh"]

FROM peddlers-node AS pok-client

RUN { \
  echo "#!/bin/bash"; \
  echo "cd /client"; \
  echo "npm install --legacy-peer-deps --no-fund"; \
  echo "npm start"; \
  } > /entrypoint.sh && chmod +x /entrypoint.sh

USER ${HOST_UID}:${HOST_GID}

COPY --chown=${HOST_UID}:${HOST_GID} client /client
WORKDIR /client 

RUN if [ -f package-lock.json ]; then \
  echo "package-lock.json found, running npm ci"; \
  npm ci --legacy-peer-deps --no-audit --no-fund; \
  else \
  echo "No package-lock.json found, running npm install"; \
  npm install --legacy-peer-deps --no-audit --no-fund; \
  fi

ARG VITE_BASEPATH
ENV VITE_BASEPATH=$VITE_BASEPATH

RUN npm run build

CMD ["/entrypoint.sh"]

FROM peddlers-node AS pok-test

# Install Chromium and related deps at image build time so test runs are fast
RUN apt-get -q update \
  && DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y \
  chromium \
  fonts-liberation \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/{apt,dpkg,cache,log}

# Avoid puppeteer downloading its own Chromium when using puppeteer-core
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true

USER ${HOST_UID}:${HOST_GID}

COPY --chown=${HOST_UID}:${HOST_GID} tools/puppeteer-test/ /opt/puppeteer-test/
WORKDIR /opt/puppeteer-test

# Install dependencies (if package-lock.json missing, npm ci will fail; fall back to npm i)
RUN if [ -f package-lock.json ]; then \
  echo "package-lock.json found, running npm ci"; \
  npm ci --legacy-peer-deps --no-audit --no-fund; \
  else \
  echo "No package-lock.json found, running npm install"; \
  npm install --legacy-peer-deps --no-audit --no-fund; \
  fi

# Default entrypoint runs the test; the workspace is mounted by docker-compose
ENTRYPOINT ["node", "test.js"]
