FROM php:8.4-apache

# Install system dependencies + PHP extensions
RUN apt-get update && apt-get install -y \
        libpng-dev \
        libjpeg-dev \
        libfreetype6-dev \
        libzip-dev \
        libonig-dev \
        libxml2-dev \
        libcurl4-openssl-dev \
        zip \
        unzip \
        git \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install \
        pdo \
        pdo_mysql \
        gd \
        zip \
        mbstring \
        xml \
        dom \
        simplexml \
        curl \
        fileinfo \
    && pecl install redis \
    && docker-php-ext-enable redis \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# Composer (used for the Laravel app under laravel/ — the legacy app still has
# no runtime Composer dependencies of its own, see CLAUDE.md).
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer

# Enable Apache mod_rewrite
RUN a2enmod rewrite

# Set working directory
WORKDIR /var/www/html

# Copy application files
COPY . .

# Make sure config/database.php is not copied (use env vars instead)
# It's in .gitignore and .dockerignore so this is safe

# Install the Laravel app's own dependencies (legacy app has none of its own).
RUN composer install --working-dir=laravel --no-interaction --optimize-autoloader

# Apache config — allow .htaccess overrides
RUN echo '<Directory /var/www/html>\n\
    AllowOverride All\n\
    Require all granted\n\
</Directory>' > /etc/apache2/conf-available/app.conf \
    && a2enconf app

# Fix file permissions
RUN chown -R www-data:www-data /var/www/html \
    && chmod -R 755 /var/www/html

# Local dev bind-mounts the repo over /var/www/html (see docker-compose.yml),
# which overrides the chown above for laravel/storage + bootstrap/cache —
# those need to be www-data-writable at runtime for Blade's view cache etc.
# See docker/entrypoint.sh for why this has to happen at container start,
# not build time.
COPY docker/entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
CMD ["apache2-foreground"]

EXPOSE 80
