# syntax=docker/dockerfile:1.4
# Use dev account's ECR registry
ARG AWS_ACCOUNT_ID=594765360358
ARG AWS_REGION=ca-central-1

FROM ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/tbi-php-84:8.4.27

# tbi-php-84:8.4.27 is Debian 13 (trixie) with current apt repositories, so the
# previous bullseye archive.debian.org workaround (tbi-php-80) is no longer needed.
RUN apt-get update

RUN curl -LO https://github.com/DataDog/dd-trace-php/releases/latest/download/datadog-setup.php && \
    php datadog-setup.php --php-bin=all
# Install Node.js 18, Git, Zip, and node-gyp build deps.
# Debian 13 (trixie) ships Python 3.13, which removed the stdlib distutils module
# and the python3-distutils package; use python3-setuptools (provides a distutils
# shim) instead for any native npm module that still relies on node-gyp.
RUN curl -fsSL https://nodejs.org/dist/v18.20.8/node-v18.20.8-linux-x64.tar.xz | tar -xJ -C /usr/local --strip-components=1 && \
    apt-get install -y git zip python3 python3-setuptools make g++

# Create directories
RUN mkdir /data && \
    mkdir /data/websites

# Copy project files
COPY . /data/websites/

# Install Composer
RUN curl -L https://github.com/composer/composer/releases/download/2.3.7/composer.phar > /usr/local/bin/composer && \
    chmod +x /usr/local/bin/composer

# Set working directory
WORKDIR /data/websites

# Define the user
USER root

# Change ownership and permissions
RUN chmod 777 -R storage bootstrap/cache public

# Setup SSH for Git access to private repos (uses BuildKit SSH mount)
RUN mkdir -p -m 0700 /root/.ssh && \
    ssh-keyscan github.com >> /root/.ssh/known_hosts

# Install dependencies and build assets
# Mount SSH socket from host (forwarded by CircleCI)
RUN --mount=type=ssh \
    composer install -n && \
    npm install --force && \
    npm update redlabel-web-resources --force && \
    npm run build

# Change working directory
WORKDIR /data

# Change ownership again (if needed)
RUN chown -R www-data:www-data /data/websites
