FROM php:7.4-apache

# Set working directory
WORKDIR /var/www/html/ng-crm/yiiapp

# Debian bullseye (base of php:7.4) is EOL; its packages now live on archive.debian.org
RUN echo 'deb http://archive.debian.org/debian bullseye main' > /etc/apt/sources.list \
    && rm -f /etc/apt/sources.list.d/* \
    && echo 'Acquire::Check-Valid-Until "false";' > /etc/apt/apt.conf.d/99archive

# Install required packages and PHP extensions
RUN apt-get update && apt-get install -y \
    sudo \
    libxml2-dev \
    libzip-dev \
    zlib1g-dev \
    libpng-dev \
    cron \
    vim \
    nano \
    git \
    curl \
    zip \           
    unzip \         
    && rm -rf /var/lib/apt/lists/* \
    && docker-php-ext-install \
    soap \
    gd \
    zip

    # && pecl install mongodb 

# pecl's channel/REST lookup can no longer parse pecl.php.net responses; install from the tarball directly
RUN curl -fsSL -o /tmp/mongodb.tgz https://pecl.php.net/get/mongodb-1.11.1.tgz \
    && pecl install /tmp/mongodb.tgz \
    && rm -f /tmp/mongodb.tgz

# Enable MongoDB and SOAP extensions
RUN echo "extension=mongodb.so" > $PHP_INI_DIR/conf.d/mongo.ini
    # echo "extension=soap" > $PHP_INI_DIR/conf.d/required-extension.ini

# Configure PHP settings
RUN echo "upload_max_filesize=20M" > /usr/local/etc/php/conf.d/uploads.ini && \
    echo "post_max_size=25M" >> /usr/local/etc/php/conf.d/uploads.ini && \
    echo "memory_limit=1024M" > /usr/local/etc/php/conf.d/memory-limit.ini && \
    echo "error_log=/var/log/php/error.log" >> /usr/local/etc/php/conf.d/uploads.ini

# Enable Apache rewrite module
RUN a2enmod rewrite

# Add the sudoers configuration safely
RUN echo 'www-data ALL=(root) NOPASSWD: /usr/bin/crontab' | tee -a /etc/sudoers > /dev/null

ENV TZ=Asia/Dubai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# Copy Apache configuration file
COPY ./deployment/apache2.conf /etc/apache2/sites-available/000-default.conf

# Restart Apache service
RUN service apache2 restart

# Copy Composer from the latest Composer image
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer

# Copy application source code
COPY . .

RUN chmod 777 ./dockerBootstrap.sh
# Install PHP dependencies
RUN composer install

# Run the script once during container startup
CMD ["/bin/bash", "-c", "./dockerBootstrap.sh && apache2-foreground"]

# Expose port 80
EXPOSE 80