# Use PHP 8.2 Apache for web backend
FROM php:8.2-apache

# Install required PHP extensions
RUN apt-get update && apt-get install -y \
    libcurl4-openssl-dev \
    pkg-config \
    libssl-dev \
    && docker-php-ext-install curl \
    && rm -rf /var/lib/apt/lists/*

# Enable Apache mod_rewrite
RUN a2enmod rewrite

# Set working directory
WORKDIR /var/www/html

# Copy PHP application
COPY back-end.php .

# Create non-root user for security
RUN groupadd -g 1001 phpapp && \
    useradd -r -u 1001 -g phpapp phpapp

# Change ownership
RUN chown -R phpapp:phpapp /var/www/html

# Switch to non-root user
USER phpapp

# Expose port 80
EXPOSE 80

# Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
  CMD curl -f http://localhost/back-end.php || exit 1

# Start Apache
CMD ["apache2-foreground"]
