# ---------- Stage 1: Build extensions  ----------
# ---------- bookworm - Debian 12 base  ----------
    FROM php:8.3-apache-bookworm AS builder

# Build dependencies for PHP extensions
RUN apt-get update && apt-get install -y --no-install-recommends \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libpng-dev \
        libxml2-dev \
        zlib1g-dev \
        libzip-dev \
        build-essential \
        git \
        unzip \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install \
        mysqli \
        pdo \
        pdo_mysql \
        bcmath \
        soap \
        gd \
        opcache \
        zip \
    && rm -rf /var/lib/apt/lists/*

# ---------- Stage 2: Runtime (Developer environment) ----------
FROM php:8.3-apache-bookworm

# Kopira PHP extensione iz build stagea
COPY --from=builder /usr/local/lib/php/extensions/ /usr/local/lib/php/extensions/
COPY --from=builder /usr/local/etc/php/conf.d/ /usr/local/etc/php/conf.d/

# Dev utils
RUN apt-get update && apt-get install -y --no-install-recommends \
        libfreetype6 \
        libjpeg62-turbo \
        libpng16-16 \
        libxml2 \
        zlib1g \
        libzip4 \
        texlive-full \
        # Namesto texlive-full lahko namestimo samo potrebne pakete za LaTeX (toplo priporocam)
        # texlive-latex-base \
        # texlive-latex-recommended \
        # texlive-latex-extra \
        latex2rtf \
        ghostscript \
        imagemagick \
        make \
        curl \
        wget \
        nano \
        vim \
        git \
        zip \
        less \
    && a2enmod rewrite \
    # Allow PNG→PDF conversion for chart PDF exports (LaTeX includegraphics)
    && sed -i 's/rights="none" pattern="PDF"/rights="read|write" pattern="PDF"/' /etc/ImageMagick-6/policy.xml \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN a2enmod rewrite

# ENV
ENV APACHE_DOCUMENT_ROOT=/var/www/html

# Set work dir
WORKDIR /var/www/html

# Port exposure for Apache
EXPOSE 80

# Default command
CMD ["apache2-foreground"]