FROM php:8.3-fpm-bookworm

RUN apt-get update && apt-get install -y --no-install-recommends \
    git unzip zip libicu-dev libzip-dev libpng-dev libjpeg62-turbo-dev libfreetype6-dev \
    libonig-dev libxml2-dev libsqlite3-dev default-mysql-client \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j"$(nproc)" bcmath gd intl mbstring opcache pcntl pdo_mysql pdo_sqlite sqlite3 zip \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
WORKDIR /var/www/html

ARG INSTALL_DEV=false
COPY composer.json composer.lock ./
RUN if [ "$INSTALL_DEV" = "true" ]; then \
      composer install --prefer-dist --no-interaction --no-progress --no-scripts; \
    else \
      composer install --no-dev --prefer-dist --no-interaction --no-progress --no-scripts; \
    fi

COPY . .
RUN if [ "$INSTALL_DEV" = "true" ]; then \
      composer dump-autoload --optimize --no-interaction --no-scripts; \
    else \
      composer dump-autoload --no-dev --optimize --no-interaction --no-scripts; \
    fi \
    && php artisan package:discover --ansi \
    && mkdir -p storage/framework/cache/data storage/framework/sessions storage/framework/views storage/logs bootstrap/cache \
    && chown -R www-data:www-data storage bootstrap/cache \
    && chmod +x docker/entrypoint.sh

COPY docker/php-production.ini /usr/local/etc/php/conf.d/99-hmis-production.ini
EXPOSE 9000
ENTRYPOINT ["/var/www/html/docker/entrypoint.sh"]
CMD ["php-fpm", "-F"]
