FROM php:8.1-apache

# Install system dependencies including ping utilities
RUN apt-get update && apt-get install -y \
    git \
    curl \
    libpng-dev \
    libonig-dev \
    libxml2-dev \
    zip \
    unzip \
    libzip-dev \
    libfreetype6-dev \
    libjpeg62-turbo-dev \
    libpng-dev \
    libwebp-dev \
    iputils-ping \
    net-tools \
    && rm -rf /var/lib/apt/lists/*

# Install PHP extensions yang diperlukan
RUN docker-php-ext-configure gd --with-freetype --with-jpeg --with-webp
RUN docker-php-ext-install \
    pdo \
    pdo_mysql \
    mysqli \
    mbstring \
    exif \
    pcntl \
    bcmath \
    gd \
    zip

# Enable Apache modules
RUN a2enmod rewrite
RUN a2enmod headers

# Set working directory
WORKDIR /var/www/html

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

# Set permissions
RUN chown -R www-data:www-data /var/www/html \
    && chmod -R 755 /var/www/html

# Copy fixed PHP configuration
COPY docker/php-fixed.ini /usr/local/etc/php/php.ini

# Create directories for logs
RUN mkdir -p /var/log/php \
    && chown www-data:www-data /var/log/php

# Create .htaccess if not exists
RUN if [ ! -f .htaccess ]; then \
    echo "RewriteEngine On" > .htaccess && \
    echo "RewriteCond %{REQUEST_FILENAME} !-f" >> .htaccess && \
    echo "RewriteCond %{REQUEST_FILENAME} !-d" >> .htaccess && \
    echo "RewriteRule ^(.*)$ index.php [QSA,L]" >> .htaccess; \
    fi

# Expose port 80
EXPOSE 80

# Start Apache
CMD ["apache2-foreground"]
