FROM php:8.5-fpm-alpine

# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/

# Set working directory
WORKDIR /var/www

# # Install dependencies
RUN apk update && apk add --no-cache \
    build-base \
    libpng-dev \
    libjpeg-turbo-dev \
    freetype-dev \
    zip \
    jpegoptim optipng pngquant gifsicle \
    vim \
    unzip \
    git \
    curl \
    libzip-dev \
    oniguruma-dev \
    icu-dev \
    sqlite \
    sqlite-dev \
    nodejs \
    npm


# # Clear cache
RUN rm -rf /var/cache/apk/*

# # Install extensions
RUN docker-php-ext-install mbstring zip exif pcntl intl pdo_sqlite

RUN docker-php-ext-install gd

# # Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Add user for laravel application
RUN addgroup -g 1000 www
RUN adduser -u 1000 -G www -s /bin/sh -D www

# # Copy existing application directory contents
COPY . /var/www

# # Copy existing application directory permissions
COPY --chown=www-data:www-data . /var/www

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

# Install Node.js dependencies
RUN npm install

# Build assets
RUN npm run build

# Change ownership of /var/www to www-data
RUN chmod -R u+rw /var/www && chown -R www:www /var/www

# # RUN php artisan optimize
# # removed as it calls all 4 commands below and fails du to 
# # multi-site configuration

# # Clear any cached configurations
RUN php artisan config:clear
# RUN php artisan cache:clear
RUN php artisan route:clear
RUN php artisan view:clear

# # Build optimizations
RUN php artisan config:cache
RUN php artisan event:cache
RUN php artisan view:cache

# # RUN php artisan route:cache

# # Run Laravel artisan command
RUN php artisan storage:link

# # RUN composer install --optimize-autoloader --no-dev

# # Change current user to www
USER www

# # Expose port 9000 and start php-fpm server
# EXPOSE 9000
