FROM php:8.3-fpm

# ---------- ARG für UID/GID ----------
ARG UID=1000
ARG GID=1000

# ---------- Systempakete ----------
RUN apt-get update && apt-get install -y \
    git unzip libzip-dev libicu-dev libpng-dev libonig-dev libxml2-dev \
    libjpeg62-turbo-dev libfreetype6-dev libwebp-dev libssl-dev locales rsync ffmpeg \
    nmap iproute2 net-tools netcat-openbsd curl \
 && docker-php-ext-configure gd --with-freetype --with-jpeg --with-webp \
 && docker-php-ext-install -j"$(nproc)" intl zip pdo_mysql gd bcmath opcache pcntl exif \
 && rm -rf /var/lib/apt/lists/*

# ---------- Node.js ----------
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
 && apt-get install -y nodejs

# ---------- Redis Extension ----------
RUN pecl install redis && docker-php-ext-enable redis

# ---------- Composer ----------
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer

# ---------- PHP Config ----------
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" \
 && echo "memory_limit=512M" >> /usr/local/etc/php/conf.d/memory.ini \
 && echo "upload_max_filesize=20M" >> /usr/local/etc/php/conf.d/uploads.ini \
 && echo "post_max_size=20M" >> /usr/local/etc/php/conf.d/uploads.ini

# ---------- PHP-FPM ----------
RUN mkdir -p /usr/local/etc/php-fpm.d \
 && echo "[www]" > /usr/local/etc/php-fpm.d/www.conf \
 && echo "user = www-data" >> /usr/local/etc/php-fpm.d/www.conf \
 && echo "group = www-data" >> /usr/local/etc/php-fpm.d/www.conf \
 && echo "listen = 9000" >> /usr/local/etc/php-fpm.d/www.conf \
 && echo "pm = dynamic" >> /usr/local/etc/php-fpm.d/www.conf \
 && echo "pm.max_children = 16" >> /usr/local/etc/php-fpm.d/www.conf \
 && echo "pm.start_servers = 4" >> /usr/local/etc/php-fpm.d/www.conf \
 && echo "pm.min_spare_servers = 4" >> /usr/local/etc/php-fpm.d/www.conf \
 && echo "pm.max_spare_servers = 8" >> /usr/local/etc/php-fpm.d/www.conf \
 && echo "pm.max_requests = 500" >> /usr/local/etc/php-fpm.d/www.conf

# ---------- UID/GID Fix ----------
RUN usermod -u ${UID} www-data \
 && groupmod -g ${GID} www-data

# ---------- Working Dir ----------
WORKDIR /var/www/html

# ---------- Cache Ordner ----------
RUN mkdir -p /var/www/.cache \
    /var/www/.npm

# ---------- Rechte (nur Cache!) ----------
RUN chown -R www-data:www-data /var/www/.cache /var/www/.npm

# ---------- User ----------
USER www-data