FROM php:8.2-fpm

# Set timezone
ENV TZ=Europe/London
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# Install system packages and PHP extensions
RUN apt-get update && apt-get install -y \
    git unzip curl zip vim libzip-dev libpng-dev libonig-dev libxml2-dev \
    libpq-dev libjpeg-dev libfreetype6-dev locales gnupg libldap2-dev gosu \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-configure ldap --with-libdir=lib/x86_64-linux-gnu/ \
    && docker-php-ext-install pdo pdo_mysql zip gd mbstring xml soap ftp ldap \
    && pecl install redis \
    && docker-php-ext-enable redis \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

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

# Copy custom PHP-FPM configuration
COPY docker/php-fpm.conf /usr/local/etc/php-fpm.conf

# Create necessary directories for PHP-FPM
RUN mkdir -p /var/lib/php/sessions /var/lib/php/wsdlcache /var/log \
    && chown -R www-data:www-data /var/lib/php /var/log \
    && chmod -R 755 /var/lib/php

# Install Node.js 18 (for asset compilation)
RUN curl -fsSL https://deb.nodesource.com/setup_18.x | bash - \
    && apt-get install -y nodejs

# Set working directory
WORKDIR /var/www/html

# Copy composer files first for better caching
COPY composer.json composer.lock ./

# Install PHP dependencies without dev dependencies
RUN COMPOSER_PROCESS_TIMEOUT=600 COMPOSER_MEMORY_LIMIT=-1 \
    composer install --no-dev --no-scripts --no-autoloader --no-interaction --prefer-dist

# Copy the rest of the application
COPY . .

# Set APP_ENV to production
ENV APP_ENV=production

# Generate optimized autoload files
RUN composer dump-autoload --no-dev --optimize --classmap-authoritative

# Create necessary directories for Laravel
RUN mkdir -p storage/logs storage/framework/{cache,sessions,views} bootstrap/cache storage/app/public

# Create storage symlink
RUN ln -sf /var/www/html/storage/app/public /var/www/html/public/storage

# Create shared group and set basic permissions for Docker compatibility
RUN groupadd -g 1000 -f seraph-app && \
    usermod -a -G seraph-app www-data && \
    usermod -u 33 www-data && \
    groupmod -g 33 www-data && \
    chown -R www-data:www-data /var/www/html && \
    # Set basic permissions - let entrypoint handle detailed permissions
    find /var/www/html -type d -exec chmod 755 {} \; && \
    find /var/www/html -type f -exec chmod 644 {} \; && \
    # Ensure writable directories exist with proper permissions
    chmod -R 775 /var/www/html/storage && \
    chmod -R 775 /var/www/html/bootstrap/cache

# Copy entrypoint script
COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
# Fix Windows line endings (CRLF -> LF) for proper Linux execution
RUN sed -i 's/\r$//' /usr/local/bin/docker-entrypoint.sh
RUN chmod +x /usr/local/bin/docker-entrypoint.sh

# DON'T switch to www-data user yet - let entrypoint handle it
# USER www-data

# Expose PHP-FPM port
EXPOSE 9000

# Set entrypoint
ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
CMD ["php-fpm", "--nodaemonize", "--fpm-config", "/usr/local/etc/php-fpm.conf"]
