FROM php:8.4-apache

# Install dependencies
RUN apt-get update && apt-get install -y \
    ffmpeg \
    libfreetype6-dev \
    libjpeg62-turbo-dev \
    libpng-dev \
    libzip-dev \
    libonig-dev \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j$(nproc) gd \
    && docker-php-ext-install mbstring zip

# Enable Apache modules
RUN a2enmod rewrite

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

# Copy the application files
COPY . /var/www/html/

# Create tmp directory for output files and make it writable
RUN mkdir -p /tmp && chmod 777 /tmp

# Create fontconfig cache directories and set permissions
RUN mkdir -p /var/cache/fontconfig /usr/share/fonts/truetype/
RUN chmod 777 /var/cache/fontconfig

# Configure Apache to listen on port 8080
RUN echo 'Listen 80' > /etc/apache2/ports.conf

# Configure Apache to use the application's directory
RUN echo '<VirtualHost *:80> \n\
    DocumentRoot /var/www/html \n\
    <Directory /var/www/html> \n\
        Options Indexes FollowSymLinks \n\
        AllowOverride All \n\
        Require all granted \n\
    </Directory> \n\
</VirtualHost>' > /etc/apache2/sites-available/000-default.conf

# Expose port 80
EXPOSE 80

# Start Apache
CMD ["apache2-foreground"]
