FROM php:8.2-apache

# Install PHP extensions + MySQL client for entrypoint
RUN apt-get update && apt-get install -y \
    libpng-dev \
    libjpeg-dev \
    libfreetype6-dev \
    libzip-dev \
    libicu-dev \
    libonig-dev \
    libcurl4-openssl-dev \
    zip unzip curl \
    default-mysql-client \
    cron \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install \
        pdo_mysql \
        mbstring \
        gd \
        zip \
        intl \
        opcache \
        bcmath \
        fileinfo \
        pcntl \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# Apache configuration
RUN a2enmod rewrite headers

# Suppress ServerName warning globally
RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf

# PHP configuration
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
COPY docker/php-custom.ini /usr/local/etc/php/conf.d/99-custom.ini

# Set working directory
WORKDIR /var/www/html

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

# Create storage directories
RUN mkdir -p \
    /var/www/html/storage/uploads \
    /var/www/html/storage/exports \
    /var/www/html/storage/logs \
    /var/www/html/storage/cache/templates \
    && chown -R www-data:www-data /var/www/html/storage \
    && chmod -R 775 /var/www/html/storage

# Apache vhost
COPY docker/000-default.conf /etc/apache2/sites-available/000-default.conf

# Entrypoint
COPY docker/entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh

EXPOSE 80

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