# Dockerfile for DSP PHP application with R support
FROM php:8.2-apache

# Install system dependencies and PHP extensions
RUN apt-get update && apt-get install -y \
        libpng-dev \
        libonig-dev \
        libzip-dev \
        zip \
        unzip \
        git \
        default-mysql-client \
        r-base \
    && docker-php-ext-install pdo_mysql \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# Install required R packages
RUN Rscript -e "install.packages('jsonlite', repos='https://cloud.r-project.org')"

# Enable Apache modules commonly used by PHP apps
RUN a2enmod rewrite

# Set working directory
WORKDIR /var/www/html

# Copy project files into the container (can be overridden by bind mount in docker-compose)
COPY . /var/www/html

# Ensure upload directories are writable by the web server
RUN chown -R www-data:www-data /var/www/html/uploads

# Increase default PHP upload limits
COPY docker/custom.ini /usr/local/etc/php/conf.d/uploads.ini

# Copy entrypoint script
COPY docker/app-entrypoint.sh /usr/local/bin/app-entrypoint.sh
RUN chmod +x /usr/local/bin/app-entrypoint.sh

# Expose Apache port
EXPOSE 80

ENTRYPOINT ["app-entrypoint.sh"]
