FROM php:8.3-cli

# Install system dependencies
RUN apt-get update && apt-get install -y \
    curl \
    git \
    unzip \
    libldap2-dev \
    libzip-dev \
    ca-certificates \
    gnupg \
    && rm -rf /var/lib/apt/lists/*

# Install Node.js (official repo)
RUN curl -fsSL https://deb.nodesource.com/setup_22.x | bash - \
    && apt-get update \
    && apt-get install -y nodejs \
    && rm -rf /var/lib/apt/lists/*

# PHP extensions
RUN docker-php-ext-configure ldap --with-libdir=lib/x86_64-linux-gnu/ \
    && docker-php-ext-install zip ldap mysqli

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

# Create unprivileged user
RUN groupadd -g 1000 app \
    && useradd -m -u 1000 -g app -s /bin/bash app

WORKDIR /var/www/filegator

RUN chown -R app:app /var/www/filegator

# Copy application
COPY --chown=app:app . .

# Switch to non-root BEFORE install
USER app

# Install dependencies as non-root
RUN composer install
RUN npm ci

# Dev config
RUN cp -n configuration_sample.php configuration.php || true

CMD ["npm", "run", "serve"]