# Use official PHP 7.4 with Apache
FROM php:7.4-apache

# Install system dependencies including SVG rendering support
RUN apt-get update && apt-get install -y \
    libpng-dev \
    libjpeg-dev \
    libfreetype6-dev \
    libzip-dev \
    # Build tools for ImageMagick
    build-essential \
    autoconf \
    automake \
    libtool \
    pkg-config \
    # ImageMagick dependencies
    libmagickcore-dev \
    libmagickwand-dev \
    # SVG rendering dependencies
    librsvg2-bin \
    librsvg2-dev \
    libcairo2-dev \
    libpango1.0-dev \
    libgdk-pixbuf2.0-dev \
    libgs-dev \
    ghostscript \
    # Additional libraries for DomPDF image support
    libxrender1 \
    libfontconfig1 \
    libx11-dev \
    libjpeg62-turbo \
    fontconfig \
    # Other utilities
    unzip \
    git \
    curl \
    wget \
    && rm -rf /var/lib/apt/lists/*

# Remove existing ImageMagick (if any) and compile from source with RSVG support
RUN apt-get remove -y imagemagick libmagickcore-6.q16-6 libmagickwand-6.q16-6 || true

# Compile and install ImageMagick 6 from source with RSVG support
ENV IMAGEMAGICK_VERSION 6.9.12-93
RUN cd /tmp \
    && wget https://github.com/ImageMagick/ImageMagick6/archive/refs/tags/${IMAGEMAGICK_VERSION}.tar.gz \
    && tar xzf ${IMAGEMAGICK_VERSION}.tar.gz \
    && cd ImageMagick6-${IMAGEMAGICK_VERSION} \
    && ./configure \
        --prefix=/usr/local \
        --enable-shared \
        --disable-static \
        --with-modules \
        --with-perl=no \
        --with-rsvg=yes \
        --with-png=yes \
        --with-jpeg=yes \
        --with-webp=yes \
        --with-freetype=yes \
        --with-fontconfig=yes \
        --with-ghostscript=yes \
        --with-gslib=yes \
    && make -j$(nproc) \
    && make install \
    && ldconfig /usr/local/lib \
    && cd / \
    && rm -rf /tmp/*

# Configure ImageMagick policy to allow SVG processing
RUN mkdir -p /usr/local/etc/ImageMagick-6 \
    && echo '<?xml version="1.0" encoding="UTF-8"?>\n<policymap>\n  <policy domain="coder" rights="read|write" pattern="SVG" />\n  <policy domain="coder" rights="read|write" pattern="PNG" />\n  <policy domain="coder" rights="read|write" pattern="JPEG" />\n  <policy domain="resource" name="memory" value="256MiB"/>\n  <policy domain="resource" name="map" value="512MiB"/>\n  <policy domain="resource" name="width" value="16KP"/>\n  <policy domain="resource" name="height" value="16KP"/>\n  <policy domain="resource" name="area" value="128MB"/>\n  <policy domain="resource" name="disk" value="1GiB"/>\n</policymap>' > /usr/local/etc/ImageMagick-6/policy.xml

# Create symlinks for ImageMagick binaries
RUN ldconfig /usr/local/lib

# Install PHP extensions
RUN docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j$(nproc) \
    gd \
    pdo \
    pdo_mysql \
    zip \
    opcache \
    exif \
    fileinfo

# Install Imagick PHP extension from source to use our custom ImageMagick
RUN export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig" \
    && pecl install imagick \
    && docker-php-ext-enable imagick

# Enable Apache mod_rewrite
RUN a2enmod rewrite

# Set working directory
WORKDIR /var/www/html

# Copy composer binary from official image
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer

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

# Configure Apache document root
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
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf

# Set proper permissions
RUN mkdir -p /var/www/html/bootstrap/cache \
    && chown -R www-data:www-data /var/www/html \
    && chmod -R 755 /var/www/html \
    && chmod -R 775 /var/www/html/storage \
    && chmod -R 775 /var/www/html/bootstrap/cache

# Create necessary directories for document processing
RUN mkdir -p /var/www/html/storage/resources/savedDocuments \
    && mkdir -p /var/www/html/storage/resources/tempDocuments \
    && mkdir -p /var/www/html/storage/fonts \
    && mkdir -p /tmp/dompdf \
    && chown -R www-data:www-data /var/www/html/storage/resources \
    && chown -R www-data:www-data /var/www/html/storage/fonts \
    && chown -R www-data:www-data /tmp/dompdf \
    && chmod -R 775 /var/www/html/storage/resources \
    && chmod -R 775 /var/www/html/storage/fonts \
    && chmod -R 775 /tmp/dompdf

# Configure PHP settings for document processing
RUN echo "memory_limit=256M" > /usr/local/etc/php/conf.d/docker-php-memlimit.ini \
    && echo "upload_max_filesize=20M" >> /usr/local/etc/php/conf.d/docker-php-memlimit.ini \
    && echo "post_max_size=20M" >> /usr/local/etc/php/conf.d/docker-php-memlimit.ini \
    && echo "max_execution_time=300" >> /usr/local/etc/php/conf.d/docker-php-memlimit.ini \
    && echo "allow_url_fopen=On" >> /usr/local/etc/php/conf.d/docker-php-memlimit.ini \
    && echo "allow_url_include=On" >> /usr/local/etc/php/conf.d/docker-php-memlimit.ini

# Install application dependencies
RUN composer install --no-interaction --no-dev --optimize-autoloader

# Copy .env.example to .env if it doesn't exist
RUN if [ ! -f .env ]; then cp .env.example .env 2>/dev/null || echo "No .env.example found"; fi

# Test ImageMagick installation and SVG/RSVG support
RUN convert -version \
    && convert -list configure | grep -i rsvg \
    && php -r "echo 'Imagick SVG support: ' . (class_exists('Imagick') && in_array('SVG', \Imagick::queryFormats()) ? 'YES' : 'NO') . PHP_EOL;" \
    && php -r "echo 'Imagick RSVG support: ' . (class_exists('Imagick') && in_array('RSVG', \Imagick::queryFormats()) ? 'YES' : 'NO') . PHP_EOL;"

# Expose port 80
EXPOSE 80

# Start Apache
CMD ["apache2-foreground"]