# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#  http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

FROM ubuntu:24.04

# Set non-interactive mode to prevent tzdata prompt
ENV DEBIAN_FRONTEND=noninteractive

# Install required dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
    curl \
    ca-certificates \
    wget \
    jq \
    && rm -rf /var/lib/apt/lists/*

# Adding all dependencies available in GHA `ubuntu-latest`
RUN curl https://raw.githubusercontent.com/actions/runner-images/155611075f91237686880f896e7a532baad72efd/images/ubuntu/toolsets/toolset-2404.json > toolset-2404.json && \
    export APT_PACKAGES="$(cat toolset-2404.json | sed s/netcat/netcat-traditional/g | jq -r '.apt | [.vital_packages[], .common_packages[], .cmd_packages[]] | join(" ")')" && \
    apt-get update && \
    apt-get install -y --no-install-recommends $APT_PACKAGES && \
    rm -rf /var/lib/apt/lists/* toolset-2404.json

    
# Configure deb installer to automatically accept mscorefonts eula and install fonts
RUN echo "ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true" | debconf-set-selections && \
    apt-get update && apt-get install -y --no-install-recommends \
        fonts-ubuntu-classic \
        fonts-dejavu-core \
        fonts-dejavu-extra \
        fonts-freefont-ttf \
        fonts-liberation \
        fonts-noto-core \
        fonts-noto-ui-core \
        fonts-noto-color-emoji \
        fonts-noto-mono \
        fonts-droid-fallback \
        fontconfig \
        ttf-mscorefonts-installer && \
    rm -rf /var/lib/apt/lists/* && \
    fc-cache -fv

# Set Node.js version
ENV NODE_VERSION=24.13.1

# Download and install Node.js
RUN curl -fsSL "https://nodejs.org/dist/v${NODE_VERSION}/node-v${NODE_VERSION}-linux-x64.tar.gz" -o /tmp/node.tar.gz && \
    tar -xzf /tmp/node.tar.gz -C /usr/local --strip-components=1 && \
    rm -rf /tmp/node.tar.gz

# Set Playwright cache folder
ENV PLAYWRIGHT_BROWSERS_PATH=/home/node/kie-tools-playwright/.cache/ms-playwright

# Installing `pnpm` to keep it consistent with the kie-tools repository
RUN npm install --global pnpm@10.29.2
# Installing Playwright browser and dependencies
RUN npx playwright@1.45.2 install-deps && npx playwright@1.45.2 install chrome chromium webkit

WORKDIR /home/node/kie-tools-playwright

ENTRYPOINT ["/bin/bash"]