FROM        086679231553.dkr.ecr.us-east-1.amazonaws.com/docker-parent-images:debian13-php84 AS base

USER        root

# Install packages
RUN         apt-get update && \
            apt-get -y install git curl unzip zip && \
            apt-get clean all && \
            rm -rf /var/lib/apt/lists/*

# Set and prepare the app directory
ENV         APP_HOME=/var/www/html

WORKDIR     $APP_HOME
RUN         mkdir -p ~/.ssh && \
            ssh-keyscan github.com >> ~/.ssh/known_hosts

USER        worker

COPY        composer.json composer.lock $APP_HOME/

FROM        base AS app

USER        root

# Install apache and git
RUN         apt-get update && \
            apt-get -y install apache2 && \
            apt-get clean all && \
            rm -rf /var/lib/apt/lists/* && \
            chown -R worker /var/log/apache2

# Install the DataDog PHP tracer
RUN         curl -LO https://github.com/DataDog/dd-trace-php/releases/download/1.21.0/datadog-php-tracer_1.21.0_amd64.deb && \
            dpkg -i datadog-php-tracer_1.21.0_amd64.deb && \
            rm datadog-php-tracer_1.21.0_amd64.deb

# Install app php dependencies
RUN         --mount=type=secret,id=COMPOSER_AUTH,required=true,uid=1010 COMPOSER_AUTH=$(cat /run/secrets/COMPOSER_AUTH) composer install --no-dev --no-autoloader && \
            composer dump-autoload --optimize && \
            apt-get -y remove --purge git && \
            apt-get -y autoremove

# Copy app source code
COPY        resources $APP_HOME/resources
COPY        spec $APP_HOME/spec
COPY        src $APP_HOME/src
COPY        web $APP_HOME/web
COPY        app.php $APP_HOME/
RUN         chgrp -R www-data "$APP_HOME"
COPY        deploy/apache2.conf /etc/apache2/apache2.conf

RUN         chown -R worker "$APP_HOME"

STOPSIGNAL  SIGWINCH
EXPOSE      8081

COPY        deploy/entry_point.sh /usr/local/bin/entry_point.sh

USER        worker

ENTRYPOINT  ["/usr/local/bin/entry_point.sh"]

FROM        base AS lint_and_test

USER        root

# Install dev system packages
RUN         apt-get update && \
            apt-get -y install ant yamllint && \
            apt-get clean all && \
            rm -rf /var/lib/apt/lists/*

# Install app php dependencies
RUN         --mount=type=secret,id=COMPOSER_AUTH,required=true,uid=1010 COMPOSER_AUTH=$(cat /run/secrets/COMPOSER_AUTH) composer install && \
            composer dump-autoload --optimize && \
            apt-get -y remove --purge git && \
            apt-get -y autoremove

# Copy app source code
COPY        resources $APP_HOME/resources
COPY        spec $APP_HOME/spec
COPY        src $APP_HOME/src
COPY        web $APP_HOME/web
COPY        app.php $APP_HOME/
COPY        build/build.xml lint-and-test.sh $APP_HOME/
COPY        tests $APP_HOME/tests
COPY        docker-compose.yml $APP_HOME/docker-compose.yml

RUN         mkdir -p $APP_HOME/build/logs $APP_HOME/build/coverage && \
            chown -R worker $APP_HOME/build

USER        worker

ENTRYPOINT  [ "/var/www/html/lint-and-test.sh" ]

FROM        base AS integration_tests

USER        root

RUN         apt-get update && \
            apt-get -y install python3 python3-venv python3-pip && \
            apt-get clean all && \
            rm -rf /var/lib/apt/lists/*

COPY        tests/Integration $APP_HOME/tests/Integration
COPY        deploy/integration-test.sh $APP_HOME/

RUN         python3 -m venv tests/Integration/venv
RUN         tests/Integration/venv/bin/pip install --upgrade pip && \
            tests/Integration/venv/bin/pip install --no-cache-dir -r tests/Integration/requirements.txt

USER        worker

ENTRYPOINT  [ "/var/www/html/integration-test.sh" ]

FROM        base AS composer_update

# Re-generate composer.lock
COPY        --chown=worker composer.json deploy/update_composer_lock.sh $APP_HOME/

ENTRYPOINT  ["/var/www/html/update_composer_lock.sh"]
