FROM php:8.3-fpm-alpine

# System dependencies + supervisor (used by the queue worker service).
RUN apk add --no-cache \
        bash \
        git \
        curl \
        unzip \
        supervisor \
        icu-dev \
        libzip-dev \
        oniguruma-dev \
        freetype-dev \
        libjpeg-turbo-dev \
        libpng-dev \
        $PHPIZE_DEPS \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j"$(nproc)" \
        bcmath \
        exif \
        gd \
        intl \
        mbstring \
        opcache \
        pcntl \
        pdo_mysql \
        zip \
    # phpredis (REDIS_CLIENT=phpredis). The app also ships predis via composer,
    # so either client works — see config/database.php.
    && pecl install redis \
    && docker-php-ext-enable redis \
    && apk del $PHPIZE_DEPS

# OPcache defaults tuned for containerized development.
RUN { \
        echo 'opcache.enable=1'; \
        echo 'opcache.enable_cli=0'; \
        echo 'opcache.validate_timestamps=1'; \
        echo 'opcache.revalidate_freq=0'; \
        echo 'memory_limit=512M'; \
        echo 'upload_max_filesize=64M'; \
        echo 'post_max_size=64M'; \
    } > /usr/local/etc/php/conf.d/zz-notetracker.ini

# Composer (pinned to the official image's installer).
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer

# Supervisord main config: includes /etc/supervisor/conf.d/*.conf
# (the queue service mounts docker/supervisor/queue-worker.conf there).
RUN mkdir -p /etc/supervisor/conf.d /var/log/supervisor \
    && { \
        echo '[supervisord]'; \
        echo 'nodaemon=true'; \
        echo 'logfile=/var/log/supervisor/supervisord.log'; \
        echo 'pidfile=/var/run/supervisord.pid'; \
        echo ''; \
        echo '[include]'; \
        echo 'files = /etc/supervisor/conf.d/*.conf'; \
    } > /etc/supervisor/supervisord.conf

WORKDIR /var/www/html

# Laravel runtime directories (log channel, Blade compilation, file
# cache/sessions, public uploads) — guaranteed to exist even when the
# source tree is copied into the image instead of bind-mounted.
RUN mkdir -p /var/www/html/storage/logs \
        /var/www/html/storage/framework/views \
        /var/www/html/storage/framework/cache \
        /var/www/html/storage/framework/sessions \
        /var/www/html/storage/app/public \
    && chown -R www-data:www-data /var/www/html/storage

EXPOSE 9000
CMD ["php-fpm"]
