# Base image with PHP 7.2-FPM and Alpine
FROM 517208865016.dkr.ecr.ap-south-1.amazonaws.com/base:php-7.2-fpm-alpine

# Set working directory
WORKDIR /var/www/html/wordpress

# Install necessary packages and PHP extensions in one layer
RUN apk add --no-cache nginx bash curl tar libpng-dev libjpeg-turbo-dev freetype-dev oniguruma-dev libzip-dev zip unzip \
    && docker-php-ext-install mysqli pdo pdo_mysql mbstring exif pcntl bcmath gd zip \
    && rm -rf /var/cache/apk/*

# Download and setup WordPress in one command to reduce layers
RUN curl -o /tmp/wordpress.tar.gz https://wordpress.org/wordpress-6.5.4.tar.gz \
    && tar -xzf /tmp/wordpress.tar.gz -C /var/www/html/wordpress --strip-components=1 \
    && chown -R www-data:www-data /var/www/html/wordpress \
    && chmod -R 755 /var/www/html/wordpress \
    && rm /tmp/wordpress.tar.gz

# Copy plugin and assets files (combine COPY statements to reduce layers)
COPY assets /var/www/html/wordpress/wp-content/plugins/nimbbl-woocommerce/assets
COPY nimbbl-woocommerce.php /var/www/html/wordpress/wp-content/plugins/nimbbl-woocommerce/
# Exclude unnecessary files like `.DS_Store` from COPY operations
COPY .DS_Store /dev/null

# Create the Nginx run directory
RUN mkdir -p /run/nginx

# Copy custom Nginx configuration
COPY nginx.conf /etc/nginx/nginx.conf

# Expose the port Nginx will listen on
EXPOSE 80

# Start both Nginx and PHP-FPM services
CMD ["sh", "-c", "php-fpm & nginx -g 'daemon off;'"]
