# Stage 1 — Build client
FROM node:20-alpine AS builder
WORKDIR /app
COPY client/package*.json ./client/
RUN cd client && npm install
COPY client/ ./client/
RUN cd client && npm run build

# Stage 2 — Production
FROM node:20-alpine
RUN apk add --no-cache ffmpeg openssl python3 py3-pip intel-media-driver \
    chromium chromium-chromedriver nss freetype harfbuzz ca-certificates ttf-freefont \
    xvfb-run xorg-server xf86-video-dummy \
    && pip3 install --break-system-packages pywidevine yt-dlp gallery-dl \
       selenium undetected-chromedriver
WORKDIR /app
COPY server/package*.json ./server/
RUN cd server && npm install --production
COPY server/ ./server/
COPY --from=builder /app/client/dist ./client/dist

ENV PORT=3001
ENV DB_PATH=/data/db/ofapp.db
ENV MEDIA_PATH=/data/media
ENV DOWNLOAD_DELAY=1000

EXPOSE 3001 3443
CMD ["node", "server/index.js"]
