# ============================================================
# 1. Composer dependencies
# ============================================================
FROM composer:2 AS composer

WORKDIR /app

COPY composer.json composer.lock ./

RUN composer install \
    --no-dev \
    --no-interaction \
    --prefer-dist \
    --optimize-autoloader \
    --no-scripts

COPY . .

RUN composer dump-autoload \
    --optimize \
    --no-dev


# ============================================================
# 2. Frontend build
# ============================================================
FROM node:22-bookworm AS frontend

WORKDIR /app

COPY package*.json ./

RUN npm ci

# Copy the Laravel application
COPY . .

# Copy Composer vendor directory because Flux CSS is imported
# from vendor/livewire/flux
COPY --from=composer /app/vendor ./vendor

RUN npm run build


# ============================================================
# 3. Production Laravel application
# ============================================================
FROM php:8.4-apache

WORKDIR /var/www/html

# ------------------------------------------------------------
# System dependencies
# ------------------------------------------------------------
RUN apt-get update && apt-get install -y \
    libpng-dev \
    libjpeg62-turbo-dev \
    libfreetype6-dev \
    libzip-dev \
    libonig-dev \
    libxml2-dev \
    libsqlite3-dev \
    sqlite3 \
    unzip \
    git \
    curl \
    && rm -rf /var/lib/apt/lists/*


# ------------------------------------------------------------
# PHP extensions
# ------------------------------------------------------------
RUN docker-php-ext-configure gd \
    --with-freetype \
    --with-jpeg

RUN docker-php-ext-install \
    pdo_sqlite \
    mbstring \
    exif \
    pcntl \
    bcmath \
    gd \
    zip


# ------------------------------------------------------------
# Apache
# ------------------------------------------------------------
RUN a2enmod rewrite

ENV APACHE_DOCUMENT_ROOT=/var/www/html/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


# ------------------------------------------------------------
# Laravel application
# ------------------------------------------------------------
COPY . .

COPY --from=composer /app/vendor ./vendor

COPY --from=frontend /app/public/build ./public/build


# ------------------------------------------------------------
# Laravel directories
# ------------------------------------------------------------
RUN mkdir -p \
    storage/framework/cache \
    storage/framework/sessions \
    storage/framework/views \
    storage/logs \
    bootstrap/cache

RUN chown -R www-data:www-data \
    storage \
    bootstrap/cache


# ------------------------------------------------------------
# Entrypoint
# ------------------------------------------------------------
COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh

RUN chmod +x /usr/local/bin/docker-entrypoint.sh

EXPOSE 80

ENTRYPOINT ["docker-entrypoint.sh"]

CMD ["apache2-foreground"]
