FROM node:22-bookworm-slim

# Install Chromium and dependencies as root
RUN apt-get update && apt-get install -y --no-install-recommends \
  chromium fonts-liberation \
  && rm -rf /var/lib/apt/lists/*

# Create non-root user
RUN groupadd --gid 1001 docfast \
  && useradd --uid 1001 --gid docfast --shell /bin/bash --create-home docfast

# Set environment variables
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true
ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium

WORKDIR /app
COPY package*.json ./
RUN npm install --omit=dev

COPY dist/ dist/
COPY scripts/ scripts/
COPY public/ public/
RUN node scripts/build-html.cjs
RUN rm -f public/swagger-ui && ln -s /app/node_modules/swagger-ui-dist public/swagger-ui

# Create data directory and set ownership to docfast user
RUN mkdir -p /app/data && chown -R docfast:docfast /app

# Switch to non-root user
USER docfast

ENV PORT=3100
EXPOSE 3100
CMD ["node", "dist/index.js"]
