FROM php:8.2.15-fpm

WORKDIR /var/www/html

COPY --from=composer:2.6 /usr/bin/composer /usr/bin/composer

RUN set -eux; \
    apt-get update; \
    apt-get install -y --no-install-recommends \
        $PHPIZE_DEPS \
        ca-certificates \
        curl \
        default-mysql-client \
        git \
        imagemagick \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libmagickcore-dev \
        libmagickwand-dev \
        libonig-dev \
        libpng-dev \
        libssl-dev \
        libxml2-dev \
        libzip-dev \
        mariadb-client \
        nginx \
        pkg-config \
        redis-tools \
        unzip \
        zip; \
    update-ca-certificates; \
    docker-php-ext-configure gd --with-freetype --with-jpeg; \
    docker-php-ext-install -j"$(nproc)" \
        bcmath \
        exif \
        gd \
        mbstring \
        mysqli \
        pcntl \
        pdo_mysql \
        sockets \
        zip; \
    pecl install imagick-3.7.0 redis; \
    docker-php-ext-enable imagick redis; \
    apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false $PHPIZE_DEPS; \
    rm -rf /var/lib/apt/lists/*

COPY services/nginx/app/ /var/www/html/
COPY services/php/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
COPY services/php/php-fpm-pool.conf /usr/local/etc/php-fpm.d/zz-pleno-workers.conf
COPY services/coolify/api/nginx.conf /etc/nginx/nginx.conf
COPY services/coolify/api/start.sh /usr/local/bin/coolify-api-start

RUN set -eux; \
    rm -f /var/www/html/storage/replication-bootstrap.json /var/www/html/storage/replication-bootstrap-*.json; \
    sed -i 's/\r$//' /usr/local/bin/docker-entrypoint.sh /usr/local/bin/coolify-api-start; \
    chmod +x /usr/local/bin/docker-entrypoint.sh /usr/local/bin/coolify-api-start; \
    COMPOSER_ALLOW_SUPERUSER=1 composer install --no-dev --prefer-dist --optimize-autoloader --no-interaction -d /var/www/html; \
    if [ -f /var/www/html/modules/washcertificates/composer.json ]; then \
        COMPOSER_ALLOW_SUPERUSER=1 composer install --no-dev --prefer-dist --optimize-autoloader --no-interaction -d /var/www/html/modules/washcertificates; \
    fi; \
    COMPOSER_ALLOW_SUPERUSER=1 composer dump-autoload --no-dev --optimize --no-interaction -d /var/www/html; \
    php -d display_errors=1 -r 'require "/var/www/html/vendor/autoload.php"; exit(interface_exists("Psr\\Http\\Message\\UriInterface") && interface_exists("Psr\\Http\\Message\\StreamInterface") ? 0 : 1);'; \
    chown -R www-data:www-data /var/www/html; \
    chmod -R 755 /var/www/html

ENV APP_DIR=/var/www/html \
    MODULE_DIR=/var/www/html/modules/washcertificates \
    AUTO_COMPOSER_INSTALL=false \
    COMPOSER_ALLOW_SUPERUSER=1

EXPOSE 80

ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
CMD ["coolify-api-start"]
