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

# Timezone
ENV TZ="Europe/Prague"
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN printf '[Date]\ndate.timezone="%s"\n', $TZ > /usr/local/etc/php/conf.d/tzone.ini

# OPcache
ENV PHP_OPCACHE_VALIDATE_TIMESTAMPS="1" \
    PHP_OPCACHE_MAX_ACCELERATED_FILES="10000" \
    PHP_OPCACHE_MEMORY_CONSUMPTION="192" \
    PHP_OPCACHE_MAX_WASTED_PERCENTAGE="10"

# PHP
RUN export PHP_INI_DIR="$PHP_INI_DIR/php.ini"
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" && \
    sed -i "s|memory_limit = 128M|memory_limit = 256M |g" "$PHP_INI_DIR/php.ini" && \
    sed -i "s|upload_max_filesize = 2M|upload_max_filesize = 100M |g" "$PHP_INI_DIR/php.ini" && \
    sed -i "s|post_max_size = 8M|post_max_size = 100M |g" "$PHP_INI_DIR/php.ini"

# 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 \
    nodejs \
    npm \
    cron \
    nano \
    default-mysql-client \
	postgresql-client \
	imagemagick \
    libmagickwand-dev \
    supervisor && \
	git clone https://github.com/Imagick/imagick --depth 1 /tmp/imagick && \
	cd /tmp/imagick && \
	phpize && \
	./configure && \
	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

# Install PHP extensions
RUN docker-php-ext-install pdo_mysql pgsql pdo_pgsql zip opcache pcntl sockets
RUN docker-php-ext-enable imagick

#OPCACHE
RUN echo '[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
COPY supervisor/* /etc/supervisor

# Configure Apache DocumentRoot to point to Laravel's public directory
# and update Apache configuration files
ENV APACHE_DOCUMENT_ROOT=/var/www/html/public
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

# Install composer # bun
ENV COMPOSER_ALLOW_SUPERUSER=1
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN chown -R www-data:www-data /var/www/html && \
    chmod -R 775 /var/www/html

# Patch Docker Entrypoint
RUN 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

# Install project composer dependencies
USER www-data
COPY --chown=www-data:www-data composer.* /var/www/html
RUN --mount=type=cache,target=/root/.composer/vendor \
    composer install --prefer-dist --no-dev -o --no-scripts

# Install project npm dependencies
COPY --chown=www-data:www-data package*.json /var/www/html
RUN npm install

# Copy the application code
COPY --chown=www-data:www-data  . /var/www/html
RUN --mount=type=cache,target=/root/.composer/vendor \
    composer install --prefer-dist --no-dev -o
RUN npm run build
USER root

# Fix Permissions
RUN chown -R www-data:www-data /var/www/html && \
    find /var/www/html -type d ! -perm 775 -print0 | xargs -0 -r -n 100 -P 10000 chmod 775 && \
    find /var/www/html -type f -not -name "*.sh" ! -perm 664 -print0 | xargs -0 -r -n 100 -P 10000 chmod 664

#Start Container
RUN chmod +x /var/www/html/start.sh
CMD ["/var/www/html/start.sh"]

# Set the working directory
VOLUME /var/www/html/storage/app

RUN mkdir -p /config
VOLUME /config

EXPOSE 8899

# Start Apache
WORKDIR /var/www/html/
