# Use PHP with Apache as the base image
FROM php:8.4-apache

# Environment
ENV TZ="Europe/Prague" \
    APACHE_DOCUMENT_ROOT="/var/www/html/public" \
	PHP_INI_FILE="$PHP_INI_DIR/php.ini" \
	PHP_OPCACHE_VALIDATE_TIMESTAMPS="1" \
    PHP_OPCACHE_MAX_ACCELERATED_FILES="10000" \
    PHP_OPCACHE_MEMORY_CONSUMPTION="192" \
    PHP_OPCACHE_MAX_WASTED_PERCENTAGE="10" \
    COMPOSER_ALLOW_SUPERUSER=1

# Timezone
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# Install Additional System Dependencies
RUN rm -f /etc/apt/apt.conf.d/docker-clean
RUN --mount=type=cache,target=/var/cache/apt \
    apt-get update && apt-get install -yqq --no-install-recommends \
    git \
    libpq-dev \
    libzip-dev \
    zip \
    unzip \
    zlib1g-dev \
    libicu-dev \
    g++ \
    nodejs \
    npm \
    cron \
    nano \
    default-mysql-client \
	postgresql-client \
	imagemagick \
    libmagickwand-dev \
    librsvg2-2 \
    librsvg2-dev \
    librsvg2-bin \
    supervisor && \
	git clone https://github.com/Imagick/imagick --depth 1 /tmp/imagick && \
	cd /tmp/imagick && \
	phpize && \
	./configure --with-rsvg && \
    ldconfig && \
	make && \
    make install && \
    apt-get autoremove && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/* && \
    npm config set cache /tmp --global && \
    mkdir -p /var/log/supervisor

# Enable Apache mod_rewrite for URL rewriting
RUN a2enmod rewrite

RUN docker-php-ext-configure intl

# Install PHP extensions
RUN docker-php-ext-configure \
    opcache --enable-opcache

RUN docker-php-ext-install \
    pdo_mysql \
    pgsql \
    pdo_pgsql \
    zip \
    opcache \
    pcntl \
    sockets\
    bcmath \
    soap \
    gd \
    intl

RUN docker-php-ext-enable \
    imagick \
    opcache

# PHP
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_FILE" && \
    sed -i "s|memory_limit = 128M|memory_limit = 256M |g" "$PHP_INI_FILE" && \
    sed -i "s|upload_max_filesize = 2M|upload_max_filesize = 100M |g" "$PHP_INI_FILE" && \
    sed -i "s|post_max_size = 8M|post_max_size = 100M |g" "$PHP_INI_FILE" && \
	printf '[Date]\ndate.timezone="%s"\n', $TZ > "$PHP_INI_DIR/conf.d/tzone.ini"

#OPCACHE
RUN echo "\n\
[opcache]\n\
opcache.enable=1\n\
opcache.revalidate_freq=0\n\
opcache.validate_timestamps=${PHP_OPCACHE_VALIDATE_TIMESTAMPS}\n\
opcache.max_accelerated_files=${PHP_OPCACHE_MAX_ACCELERATED_FILES}\n\
opcache.memory_consumption=${PHP_OPCACHE_MEMORY_CONSUMPTION}\n\
opcache.max_wasted_percentage=${PHP_OPCACHE_MAX_WASTED_PERCENTAGE}\n\
opcache.interned_strings_buffer=16\n\
opcache.fast_shutdown=1" >> "$PHP_INI_DIR/conf.d/opcache.ini"

#SUPERVISORD
RUN mkdir -p /var/log/supervisor
COPY ./supervisor/supervisord.conf /etc/supervisor/supervisord.conf
COPY ./supervisor/conf.d/laravel-queues.conf /etc/supervisor/conf.d/laravel-queues.conf

# Setup Crone
RUN mkdir -p /var/log/cron && \
echo "* * * * * www-data cd /var/www/html && /usr/local/bin/php artisan schedule:run >> /dev/null 2>&1" >> /etc/crontab

# Install composer # bun
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Configure Apache DocumentRoot to point to Laravel's public directory
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf && \
    sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf && \
    sed -i 's/^exec /printenv > \/etc\/environment\n\nexec /' /usr/local/bin/apache2-foreground && \
    sed -i 's/^exec /service cron start\n\nexec /' /usr/local/bin/apache2-foreground

#Cleaning
RUN apt-get clean && \
    rm -rf /var/lib/apt/lists/* && \
    rm -rf /var/cache/apt/* && \
    rm -rf /usr/share/man/* && \
    rm -rf /usr/share/doc/* && \
    rm -rf /tmp/*