# Single all-in-one image: Apache2 + PHP (mod_php, matching how this app
# actually runs in production - see pol-marketplace.printsoflove.com.conf),
# MySQL, Redis, Node/npm (Vite build + the auth-sidecar service), and PM2
# supervising every long-running process. Intentionally not split into
# multiple containers - see docker-compose.yml, which now just runs this one.
FROM ubuntu:22.04

ENV DEBIAN_FRONTEND=noninteractive

# --- Base OS packages, PHP 8.2 (via ondrej/php - Ubuntu 22.04 ships 8.1 by
# default and composer.json requires ^8.2), Node 20 LTS, MySQL, Redis ---
RUN apt-get update && apt-get install -y --no-install-recommends \
        software-properties-common ca-certificates curl gnupg lsb-release \
    && add-apt-repository -y ppa:ondrej/php \
    && curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
    && apt-get update && apt-get install -y --no-install-recommends \
        apache2 libapache2-mod-php8.2 \
        php8.2 php8.2-cli php8.2-common php8.2-mysql php8.2-redis \
        php8.2-mbstring php8.2-xml php8.2-curl php8.2-zip php8.2-gd \
        php8.2-bcmath php8.2-intl php8.2-soap php8.2-imagick \
        mysql-server redis-server \
        nodejs git unzip zip \
    && rm -rf /var/lib/apt/lists/*

COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
RUN npm install -g pm2

# --- Apache config: mod_php, DocumentRoot at public/, matching prod ---
COPY docker/apache-vhost.conf /etc/apache2/sites-available/000-default.conf
RUN a2enmod rewrite

COPY docker/uploads.ini /etc/php/8.2/apache2/conf.d/99-uploads.ini
COPY docker/uploads.ini /etc/php/8.2/cli/conf.d/99-uploads.ini

WORKDIR /var/www/html

# --- Dependency layers, cached across builds unless these manifest files
# change. Split from the app source COPY below so that a source-only edit
# (the common case) doesn't force a full composer/npm reinstall + Playwright
# browser re-download - only the app-source layer and the build step re-run. ---
COPY composer.json composer.lock ./
RUN composer install --no-dev --no-scripts --no-autoloader

COPY package.json package-lock.json ./
RUN npm ci

COPY auth-sidecar/package.json auth-sidecar/package-lock.json ./auth-sidecar/
RUN cd auth-sidecar && npm install

COPY render-sidecar/package.json ./render-sidecar/
RUN cd render-sidecar && npm install

# --- App source: only this layer and below re-run on a source-only change.
# composer install runs again here (fast - packages are already in vendor/
# from the layer above) to regenerate the autoloader and run artisan-
# dependent scripts (e.g. package:discover) that need the app's files. ---
COPY . .

RUN composer install --no-dev --optimize-autoloader \
    && npm run build

# These must exist regardless of what the build context happened to carry
# over (an empty dir with only a tracked .gitignore doesn't survive every
# copy path) - Laravel throws at runtime if they're missing, not just slow.
RUN mkdir -p storage/framework/cache/data storage/framework/sessions storage/framework/views storage/logs \
    && chown -R www-data:www-data storage bootstrap/cache \
    && chmod +x docker/entrypoint.sh docker/scheduler-loop.sh docker/wait-for-mysql.sh

EXPOSE 80

ENTRYPOINT ["docker/entrypoint.sh"]
