# Invoice Match — PHP UI (CodeIgniter 4 + Shield).
# php-fpm behind the nginx `web` service (see ../docker-compose.yml). Only
# `web` publishes a host port; this container listens on 9000 internally.
FROM php:8.3-fpm

# --- System libraries for the required PHP extensions --------------------------
# PhpSpreadsheet needs zip + gd + (ext-)xml + mbstring; CI4 likes intl.
RUN apt-get update && apt-get install -y --no-install-recommends \
        git \
        unzip \
        libzip-dev \
        libpng-dev \
        libjpeg62-turbo-dev \
        libfreetype6-dev \
        libicu-dev \
        libonig-dev \
        libxml2-dev \
        default-mysql-client \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j"$(nproc)" pdo_mysql mysqli zip gd intl \
    && rm -rf /var/lib/apt/lists/*

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

WORKDIR /var/www/html

# Install PHP dependencies first for better layer caching. composer.lock is
# copied when present (the trailing * keeps the build working before a lock
# file exists).
COPY composer.json composer.lock* ./
RUN composer install --no-interaction --no-progress --prefer-dist --no-scripts --no-autoloader || true

# --- Application code ----------------------------------------------------------
COPY . /var/www/html

RUN composer install --no-interaction --no-progress --prefer-dist \
    && mkdir -p writable/cache writable/logs writable/session writable/uploads writable/temp writable/debugbar \
    && chown -R www-data:www-data writable \
    && chmod -R ug+rwx writable

# Entrypoint: wait for MySQL, migrate, then start php-fpm.
COPY docker/entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh

EXPOSE 9000

ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
CMD ["php-fpm"]
