# Build context is the pbx-app project root (see docker-compose.yml: context: ..)
FROM php:8.3-apache

RUN docker-php-ext-install pdo_mysql \
    && a2enmod rewrite

# The app's webroot is public/, not the image's default /var/www/html —
# repoint Apache's DocumentRoot (standard recipe for php-apache images).
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 \
        /etc/apache2/apache2.conf \
        /etc/apache2/conf-available/*.conf

# Copy the whole app (src/, public/, views/, config/, agi/, scripts/, migrations/)
COPY . /var/www/html/

# storage/ needs to be writable by the web server user for recordings/uploads
RUN chown -R www-data:www-data /var/www/html/storage \
    && chmod -R 775 /var/www/html/storage

# docker-compose supplies DB_*, SIGNALWIRE_*, SMTP_*, etc. as real environment
# variables — generate .env from them at container start rather than baking
# secrets into the image.
COPY docker/app/entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh

ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
CMD ["apache2-foreground"]
