FROM php:8.2-apache

# Enable Apache modules and common build deps
RUN a2enmod rewrite
# RUN mv /etc/apt/preferences.d/no-debian-php /tmp/

RUN apt-get update
RUN apt-get upgrade -y
RUN apt-get install -y git
RUN apt-get install -y unzip
RUN apt-get install -y curl
RUN apt-get install -y libzip-dev
RUN apt-get install -y zip
RUN apt-get install -y libonig-dev
# RUN apt-get install -y php-yaml
RUN rm -rf /var/lib/apt/lists/*
# RUN mv /tmp/no-debian-php /etc/apt/preferences.d/

# Install common PHP extensions (useful for F3/Smarty and typical apps)
# - pdo, pdo_mysql: DB access
# - mbstring: multibyte string handling (often required by libraries)
# - zip: for Composer packages that need zip operations
RUN docker-php-ext-install pdo
RUN docker-php-ext-install pdo_mysql
RUN docker-php-ext-install mbstring
RUN docker-php-ext-install zip
RUN docker-php-ext-install curl

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

# Set Apache DocumentRoot to public_html and allow .htaccess overrides
ENV APACHE_DOCUMENT_ROOT=/var/www/html/public_html
RUN sed -ri "s#DocumentRoot /var/www/html#DocumentRoot ${APACHE_DOCUMENT_ROOT}#g" /etc/apache2/sites-available/000-default.conf \
    && printf '<Directory "%s">\n    AllowOverride All\n    Require all granted\n</Directory>\n' "${APACHE_DOCUMENT_ROOT}" \
         > /etc/apache2/conf-available/public-html.conf \
    && a2enconf public-html

WORKDIR /var/www/html
COPY . /var/www/html

# On first run with a bind mount, vendor/ (installed in image) is hidden; ensure it's present
CMD ["bash", "-lc", "if [ -f composer.json ] && [ ! -d vendor ]; then composer install --no-interaction --no-progress --prefer-dist; fi; apache2-foreground"]