FROM ubuntu:24.04

LABEL maintainer="Mohamed Fathey @ axon-eg.com <fathey@axon-eg.com>"

# Build arguments
ARG BUILD_ENV=production
ARG INCLUDE_TESTS=false

# Set environment variables
ENV TZ=Africa/Cairo \
    DEBIAN_FRONTEND=noninteractive \
    COMPOSER_ALLOW_SUPERUSER=1 \
    COMPOSER_NO_INTERACTION=1 \
    # Set Laravel to log to stderr instead of a file
    LOG_CHANNEL=stderr

# Set timezone and install packages
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone && \
    apt-get update && \
    apt-get install -y --no-install-recommends software-properties-common && \
    LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php -y && \
    apt-get update && \
    apt-get install -y --no-install-recommends \
    php8.2 php8.2-fpm php8.2-curl php8.2-zip php8.2-xml \
    php8.2-opcache php8.2-mysql php8.2-mbstring php8.2-bcmath \
    php8.2-common php8.2-cli php8.2-cgi php8.2-gd php8.2-intl \
    php8.2-apcu nginx curl git supervisor acl vim zsh \
    libxrender1 && \
    if [ "$BUILD_ENV" = "testing" ] || [ "$BUILD_ENV" = "development" ]; then \
      apt-get install -y --no-install-recommends php8.2-xdebug; \
    fi && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# Create necessary directories
RUN mkdir -p /run/php \
    /var/log/supervisor \
    /var/log/nginx \
    /var/cache/nginx && \
    rm -f /etc/nginx/sites-enabled/default

# Copy configuration files
COPY devops/nginx/default.production.conf /etc/nginx/sites-enabled/default
COPY devops/nginx/nginx.conf /etc/nginx/nginx.conf
COPY devops/php/php.production.ini /etc/php/8.2/fpm/php.ini
COPY devops/php/php-fpm.conf /etc/php/8.2/fpm/php-fpm.conf
COPY devops/php/www.conf /etc/php/8.2/fpm/pool.d/www.conf
COPY devops/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY devops/start.sh /start.sh

# Set proper permissions
RUN chmod +x /start.sh && \
    chown -R www-data:www-data /var/log/nginx /var/cache/nginx

# Set up application directory
WORKDIR /usr/share/nginx/html

# Copy application files
COPY src/backend .

# Install Composer and dependencies
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer && \
    if [ "$BUILD_ENV" = "production" ] && [ "$INCLUDE_TESTS" = "false" ]; then \
      composer install --no-dev --optimize-autoloader --prefer-dist; \
    else \
      composer install --optimize-autoloader; \
    fi

# Setup Laravel storage and logging with proper permissions
RUN mkdir -p storage/framework/sessions \
    storage/framework/views \
    storage/framework/cache \
    storage/logs \
    bootstrap/cache \
    public/assets/images/qrcodes && \
    chown -R www-data:www-data . && \
    # More permissive permissions for storage and cache directories
    chmod -R 777 storage && \
    chmod -R 777 bootstrap/cache && \
    chmod -R 775 public/assets && \
    # Setup logging to stdout/stderr
    ln -sf /dev/stdout /var/log/nginx/access.log && \
    ln -sf /dev/stderr /var/log/nginx/error.log

# Create and make laravel.log writable
RUN touch storage/logs/laravel.log && \
    chmod 666 storage/logs/laravel.log && \
    chown www-data:www-data storage/logs/laravel.log && \
    # Configure Laravel to log to stderr
    echo "LOG_CHANNEL=stderr" >> .env

# Remove unnecessary files and set permissions
RUN if [ "$BUILD_ENV" = "production" ] && [ "$INCLUDE_TESTS" = "false" ]; then \
      rm -rf .git .github tests; \
    fi && \
    # Set proper group permissions for www-data
    usermod -a -G www-data root && \
    chmod -R g+w storage bootstrap/cache

# Add PHP OPcache configuration
RUN echo "opcache.enable=1" >> /etc/php/8.2/fpm/conf.d/10-opcache.ini && \
    echo "opcache.memory_consumption=128" >> /etc/php/8.2/fpm/conf.d/10-opcache.ini && \
    echo "opcache.interned_strings_buffer=8" >> /etc/php/8.2/fpm/conf.d/10-opcache.ini && \
    echo "opcache.max_accelerated_files=4000" >> /etc/php/8.2/fpm/conf.d/10-opcache.ini && \
    echo "opcache.revalidate_freq=60" >> /etc/php/8.2/fpm/conf.d/10-opcache.ini && \
    echo "opcache.fast_shutdown=1" >> /etc/php/8.2/fpm/conf.d/10-opcache.ini

EXPOSE 80

CMD ["/start.sh"]