
FROM php:8.2-apache

LABEL maintainer="CapitalExpress Insurance"
LABEL description="Marine Cargo Insurance Platform — Web + Background Workers"


RUN apt-get update && apt-get install -y --no-install-recommends \
        git \
        unzip \
        libzip-dev \
        libpng-dev \
        libjpeg62-turbo-dev \
        libfreetype6-dev \
        libxml2-dev \
        default-mysql-client \
        cron \
    && rm -rf /var/lib/apt/lists/*

RUN docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j$(nproc) \
        pdo_mysql \
        mysqli \
        gd \
        zip \
        soap \
        opcache

# ── Apache modules ───────────────────────────────────────────────────────────
RUN a2enmod rewrite headers expires

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

# ── PHP configuration ───────────────────────────────────────────────────────
COPY docker/php.ini /usr/local/etc/php/conf.d/zz-custom.ini
COPY docker/opcache.ini /usr/local/etc/php/conf.d/zz-opcache.ini

# ── Apache configuration ─────────────────────────────────────────────────────
COPY docker/apache-vhost.conf /etc/apache2/sites-available/000-default.conf

# ── App source ────────────────────────────────────────────────────────────────
WORKDIR /var/www/html

# Copy composer files first for better layer caching
COPY composer.json composer.lock* ./
RUN if [ -f composer.json ]; then composer install --no-dev --no-interaction --prefer-dist --no-progress; fi

# Copy the rest of the application
COPY . .

# ── Permissions ──────────────────────────────────────────────────────────────
# Apache (www-data) needs write access to logs/ and any upload dirs.
RUN mkdir -p /var/www/html/logs \
    && chown -R www-data:www-data /var/www/html \
    && chmod -R 755 /var/www/html \
    && chmod -R 775 /var/www/html/logs

# ── Entrypoint ────────────────────────────────────────────────────────────────
COPY docker/entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh

EXPOSE 80

ENTRYPOINT ["entrypoint.sh"]

# Default command — overridden in docker-compose.yml for worker containers
CMD ["apache2-foreground"]