# syntax=docker/dockerfile:1

# ---------------------------------------------------------------------------
# Stage 1 – Build TypeScript
# ---------------------------------------------------------------------------
FROM node:20-alpine AS builder

WORKDIR /app

COPY package*.json ./
RUN npm install

COPY tsconfig.json ./
COPY src/ ./src/

RUN npm run build

# ---------------------------------------------------------------------------
# Stage 2 – Runtime image
# ---------------------------------------------------------------------------
FROM node:20-alpine AS runner

# Install FFmpeg (includes ffprobe).
RUN apk add --no-cache ffmpeg

WORKDIR /app

# Production dependencies only.
COPY package*.json ./
RUN npm install --omit=dev

# Compiled output from builder stage.
COPY --from=builder /app/dist ./dist

# Non-root user for defence-in-depth.
RUN addgroup -g 1001 -S transcoder \
 && adduser  -u 1001 -S transcoder -G transcoder

# Work directory the worker uses for temp files.
RUN mkdir -p /work && chown transcoder:transcoder /work

USER transcoder

CMD ["npm", "run", "start"]
