# FROM mcr.microsoft.com/devcontainers/universal
# FROM mcr.microsoft.com/devcontainers/javascript-node:20-bullseye
ARG BASE_IMAGE=mcr.microsoft.com/devcontainers/universal
FROM ${BASE_IMAGE}

# OS packages.
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
    && apt-get -y install --no-install-recommends wget sed curl ripgrep

RUN sudo mkdir -p -m 755 /etc/apt/keyrings \
    && out=$(mktemp) && wget -nv -O$out https://cli.github.com/packages/githubcli-archive-keyring.gpg \
    && cat $out | sudo tee /etc/apt/keyrings/githubcli-archive-keyring.gpg > /dev/null \
    && sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg \
    && echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
    && sudo apt update -y \
    && sudo apt install gh -y

# Install Rust and Cargo via rustup
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y \
    && /root/.cargo/bin/rustup default stable \
    && /root/.cargo/bin/rustup update

# Install playwright
# RUN npx playwright install --with-deps
RUN npx playwright install chromium --with-deps

# Install PM2 globally and ensure it's accessible
RUN npm install -g pm2 && \
    ln -sf /usr/local/node/bin/pm2 /usr/local/bin/pm2 && \
    pm2 --version

# install Gitleaks (supports x64 and arm64)
RUN ARCH=$(dpkg --print-architecture) && \
    if [ "$ARCH" = "amd64" ]; then \
        GITLEAKS_URL="https://github.com/gitleaks/gitleaks/releases/download/v8.28.0/gitleaks_8.28.0_linux_x64.tar.gz"; \
    elif [ "$ARCH" = "arm64" ]; then \
        GITLEAKS_URL="https://github.com/gitleaks/gitleaks/releases/download/v8.28.0/gitleaks_8.28.0_linux_arm64.tar.gz"; \
    else \
        GITLEAKS_URL=""; \
    fi && \
    if [ -n "$GITLEAKS_URL" ]; then \
        echo "Installing gitleaks for architecture: $ARCH" && \
        wget -O /tmp/gitleaks.tar.gz "$GITLEAKS_URL" && \
        tar -xzf /tmp/gitleaks.tar.gz -C /tmp && \
        mv /tmp/gitleaks /usr/local/bin/gitleaks && \
        chmod +x /usr/local/bin/gitleaks && \
        rm /tmp/gitleaks.tar.gz; \
    else \
        echo "Skipping gitleaks install: unsupported architecture $ARCH"; \
    fi

# Install goose CLI
ARG CACHEBUST=1
RUN echo "${CACHEBUST}" && \
    export CONFIGURE=false && \
    export PATH="/root/.local/bin:$PATH" && \
    curl -fsSL https://github.com/block/goose/releases/download/stable/download_cli.sh | bash

# Ensure cargo and goose are in PATH for future layers and container runtime
ENV PATH="/root/.cargo/bin:/root/.local/bin:$PATH"