FROM php:8.3.36-apache

# Install Node.js
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && apt-get install -y nodejs

RUN apt-get update && apt-get install -y \
    git \
    curl \
    libicu-dev \
    libzip-dev \
    libonig-dev \
    libssh2-1 \
    libssh2-1-dev \
 && rm -rf /var/lib/apt/lists/*

RUN pecl install ssh2 \
    && docker-php-ext-enable ssh2 \
    && docker-php-ext-configure intl \
    && docker-php-ext-install \
    pdo \
        pdo_mysql \
        mysqli \
        intl \
        ftp \
        mbstring \
        bcmath \
        zip

RUN a2enmod rewrite

COPY --from=composer:2 /usr/bin/composer /usr/local/bin/composer

WORKDIR /var/www/app

ENV APACHE_DOCUMENT_ROOT=/var/www/app/public

RUN sed -ri \
    -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' \
    /etc/apache2/sites-available/*.conf \
    /etc/apache2/apache2.conf \
    /etc/apache2/conf-available/*.conf

RUN echo '<Directory /var/www/app/public>\n\
    AllowOverride All\n\
    Require all granted\n\
</Directory>' > /etc/apache2/conf-available/laravel.conf \
    && a2enconf laravel 

# Copy app files (composer/npm setup happens at runtime via entrypoint)
COPY composer.json composer.lock ./
COPY package.json package-lock.json* ./
COPY scripts/ ./scripts/ 2>/dev/null

# Note: for Laravel, the COPY . . needs to be before the composer install
COPY . .

# Create storage directories
RUN mkdir -p \
    storage/app \
    storage/framework/cache/data \
    storage/framework/sessions \
    storage/framework/views \
    storage/logs \
    bootstrap/cache \
    public/assets/vendor

# Copy and set up entrypoint script
COPY docker-entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/docker-entrypoint.sh

ENTRYPOINT ["docker-entrypoint.sh"]

EXPOSE 80
