# Set the base image
FROM php:8.1-fpm

# Install dependencies
RUN apt-get update && apt-get install -y \
    libonig-dev \
    libzip-dev \
    zip \
    unzip \
    nginx \
    curl

# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl bcmath

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

# Copy nginx configuration
COPY ./.docker/nginx/nginx.conf /etc/nginx/conf.d/default.conf

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

# Copy composer.json
COPY composer.json .

# Install Composer dependencies 
RUN composer install --no-scripts --no-autoloader

# Copy code
COPY . .

# changing the storage dir permission (777 is temp for now)
RUN chmod -R 777 storage

# Generate optimized autoloader and clear cache
RUN composer dump-autoload --optimize && php artisan cache:clear

# Expose port 80
EXPOSE 80

# Start Nginx and PHP-FPM
CMD /usr/sbin/nginx && php-fpm && php artisan system:setup