FROM php:8.2-apache

# Enable required Apache modules
RUN a2enmod rewrite headers remoteip

# Install PHP extensions (gd needs libpng/zlib dev headers at build time;
# libpng16-16 is the runtime shared library and must NOT be purged after).
RUN apt-get update && \
    apt-get install -y --no-install-recommends libpng-dev zlib1g-dev libpng16-16 && \
    docker-php-ext-configure gd && \
    docker-php-ext-install opcache gd && \
    apt-get purge -y --auto-remove libpng-dev zlib1g-dev && \
    rm -rf /var/lib/apt/lists/*

# Copy custom PHP configuration
COPY docker/php.ini /usr/local/etc/php/conf.d/panel.ini

# Copy Apache configuration
COPY docker/apache.conf /etc/apache2/sites-available/000-default.conf

# Set working directory
WORKDIR /var/www/html

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

# Create data directory and set permissions
RUN mkdir -p /var/www/html/api && \
    chown -R www-data:www-data /var/www/html && \
    chmod -R 755 /var/www/html && \
    chmod 750 /var/www/html/api

# Persist only the SQLite database file across container rebuilds.
# (Do not declare the whole /api directory as a volume -- that would
# hide the PHP endpoint scripts that ship inside this image.)
VOLUME /var/www/html/api/.db.db

# Expose port
EXPOSE 80

CMD ["apache2-foreground"]
