FROM php:8.4-apache

# Deploy v2: Fixed Docker build configuration
# Install sistem dependencies & PHP extensions untuk Laravel + PostgreSQL + Node.js
RUN apt-get update && apt-get install -y \
    libpng-dev \
    libjpeg-dev \
    libfreetype6-dev \
    libpq-dev \
    zip \
    unzip \
    git \
    curl \
    gnupg2 \
    lsb-release \
    ca-certificates \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install pdo pdo_pgsql pdo_mysql gd bcmath

# Install Node.js untuk Vite build
RUN curl -fsSL https://deb.nodesource.com/setup_22.x | bash - \
    && apt-get install -y nodejs

# Aktifkan Apache modules yang diperlukan
RUN a2enmod rewrite \
    && a2enmod headers

# Setup Apache configuration untuk Laravel public folder
RUN echo '<Directory /var/www/html/public>' > /etc/apache2/conf-available/laravel.conf \
    && echo '    Options -Indexes +FollowSymLinks' >> /etc/apache2/conf-available/laravel.conf \
    && echo '    AllowOverride All' >> /etc/apache2/conf-available/laravel.conf \
    && echo '    Allow from all' >> /etc/apache2/conf-available/laravel.conf \
    && echo '</Directory>' >> /etc/apache2/conf-available/laravel.conf \
    && a2enconf laravel

# Tukar Apache document root ke folder /public Laravel
ENV APACHE_DOCUMENT_ROOT /var/www/html/public

RUN sed -ri -e 's!/var/www/html!/var/www/html/public!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!/var/www/html/public/!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf

# Set Apache untuk handle port dinamis dari Render
ENV APACHE_PORT=10000
RUN sed -i 's/Listen 80/Listen 10000/' /etc/apache2/ports.conf

# Salin semua fail projek ke dalam container
COPY . /var/www/html

WORKDIR /var/www/html

# Install Composer terbaru
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Install PHP dependencies
RUN composer install --no-interaction --optimize-autoloader --no-dev

# Install Node dependencies dan build assets
RUN npm ci --ignore-scripts
RUN npm run build

# Set proper permissions untuk Laravel
RUN mkdir -p /var/www/html/storage /var/www/html/bootstrap/cache \
    && chown -R www-data:www-data /var/www/html \
    && chmod -R 755 /var/www/html \
    && chmod -R 775 /var/www/html/storage \
    && chmod -R 775 /var/www/html/bootstrap/cache \
    && find /var/www/html -type f -exec chmod 644 {} \; \
    && find /var/www/html/storage -type f -exec chmod 664 {} \; \
    && find /var/www/html/bootstrap/cache -type f -exec chmod 664 {} \;

# Set permissions untuk folder storage dan cache
RUN chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache

# Clear dan cache config untuk production
# RUN php artisan config:cache \
#     && php artisan route:cache \
#     && php artisan view:cache
RUN chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache

# Buka port 10000 untuk Render
EXPOSE 15000php artisan key:generate --show

# CMD ["apache2-foreground"]
# KITA JALANKAN CACHE MASA CONTAINER BARU NAK START (RUN TIME)
CMD php artisan config:cache && php artisan route:cache && php artisan view:cache && apache2-foreground
