FROM php:8.2-fpm

# Install system dependencies
RUN apt-get update && apt-get install -y \
    git \
    curl \
    libpng-dev \
    libonig-dev \
    libxml2-dev \
    zip \
    unzip \
    libzip-dev \
    libicu-dev

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

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

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

# Set working directory
WORKDIR /var/www

# Set composer to run as root
ENV COMPOSER_ALLOW_SUPERUSER=1

# Copy composer files
COPY composer.json composer.lock ./

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

# Copy the rest of the application code
COPY . .

# Generate optimized autoload files
RUN composer dump-autoload --optimize


# Create storage directory structure and set permissions
RUN mkdir -p storage/framework/{sessions,views,cache} \
&& mkdir -p storage/logs \
&& chown -R www-data:www-data storage \
&& chmod -R 775 storage \
&& chown -R www-data:www-data bootstrap/cache \
&& chmod -R 775 bootstrap/cache

# Set permissions for the entire application
# RUN chown -R www-data:www-data /var/www \
# && find /var/www -type f -exec chmod 644 {} \; \
# && find /var/www -type d -exec chmod 755 {} \; \
# && chmod -R 775 /var/www/storage /var/www/bootstrap/cache

RUN php artisan optimize