# Use the latest Debian Slim as the base image
FROM debian:bullseye-slim

# Define the PHP version as an argument
ARG PHP_VERSION=8.2

# Install essential dependencies and configure the PHP repository
RUN apt-get update && apt-get install -y \
    apt-transport-https \
    lsb-release \
    ca-certificates \
    wget \
    nano \
    curl \
    gnupg2 \
    iproute2 \
    iputils-ping && \
    wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg && \
    echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list && \
    apt-get update && apt-get install -y --no-install-recommends \
    apache2 \
    libapache2-mod-php${PHP_VERSION} \
    php${PHP_VERSION} \
    php${PHP_VERSION}-cli \
    php${PHP_VERSION}-fpm \
    php${PHP_VERSION}-pdo \
    php${PHP_VERSION}-mbstring \
    php${PHP_VERSION}-xml \
    php${PHP_VERSION}-curl \
    php${PHP_VERSION}-gd \
    php${PHP_VERSION}-mysql \
    php${PHP_VERSION}-zip \
    php${PHP_VERSION}-xdebug \
    php${PHP_VERSION}-dev \
    php${PHP_VERSION}-soap \
    php${PHP_VERSION}-intl \
    mariadb-client && \
    a2enmod rewrite proxy proxy_http headers && \
    apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# NODE
RUN apt-get update && apt-get install -y software-properties-common
RUN curl -sL https://deb.nodesource.com/setup_21.x | bash - 
RUN apt-get update && apt-get install -y \
    nodejs

# Check if PHP and Apache were installed correctly
RUN php -v && apache2 -v

# Configure Xdebug and set permissions for the log file
RUN touch /tmp/xdebug.log && chmod 777 /tmp/xdebug.log

# Install Composer globally
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer && \
    composer --version

# Set the working directory to /var/www/html
WORKDIR /var/www/html

# Copy and configure the entrypoint script
COPY docker-entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/docker-entrypoint.sh

# Adjust permissions to ensure proper access rights for web server
RUN chown -R www-data:www-data /var/www && chmod -R 755 /var/www

RUN echo "date.timezone = Europe/Lisbon" > /etc/php/${PHP_VERSION}/apache2/conf.d/timezone.ini

# Set the entrypoint and expose port 80
ENTRYPOINT ["docker-entrypoint.sh"]
EXPOSE 80

# Default command to start Apache in the foreground
CMD ["apache2ctl", "-D", "FOREGROUND"]
