# modem-bridge: Headless Chromium for TP-Link ONU web GUI access
# ~450MB total (node:20-slim + Chromium deps)
# Lighter than node:20 + full playwright install (~800MB)

FROM node:20-slim

# Playwright needs these system deps for Chromium
# Install ALL Chromium dependencies in one shot via playwright's own installer
RUN apt-get update && apt-get install -y --no-install-recommends \
    libnss3 libatk1.0-0 libatk-bridge2.0-0 libcups2 libdrm2 \
    libxkbcommon0 libxcomposite1 libxdamage1 libxrandr2 libgbm1 \
    libpango-1.0-0 libcairo2 libasound2 libxshmfence1 \
    libxfixes3 libx11-6 libx11-xcb1 libxcb1 libxext6 \
    libxrender1 libxi6 libxtst6 libglib2.0-0 libdbus-1-3 \
    fonts-liberation \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Create non-root user first so playwright installs in their home
RUN groupadd -r bridge && useradd -r -g bridge -G audio,video -m bridge

COPY package.json .
RUN npm install --production

# Install Chromium as the bridge user (so it goes to /home/bridge/.cache)
USER bridge
RUN npx playwright install chromium 2>&1 | tail -3
USER root

COPY server.js .
COPY lib/ lib/
RUN chown -R bridge:bridge /app

EXPOSE 3301

USER bridge

CMD ["node", "server.js"]
