# ==============================
# Build stage
# ==============================
FROM node:20-alpine AS builder

WORKDIR /app

# Copy package files and Prisma schema FIRST
COPY transcoder/package.json transcoder/tsconfig.json ./
COPY prisma ./prisma

# Set a temporary DATABASE_URL for Prisma schema generation during build
ENV DATABASE_URL="postgresql://user:password@localhost:5432/dummy"

# Install all deps (including dev)
RUN npm install

# Copy source
COPY transcoder ./ 

# Build TypeScript
RUN npm run build


# ==============================
# Runtime stage
# ==============================
FROM node:20-alpine

# Install FFmpeg
RUN apk add --no-cache ffmpeg

WORKDIR /app

# Copy package files
COPY transcoder/package.json ./

# Copy node_modules from builder (includes all dependencies)
COPY --from=builder /app/node_modules ./node_modules

# Copy compiled output
COPY --from=builder /app/dist ./dist

# Copy Prisma schema
COPY prisma ./prisma

# Environment defaults (can be overridden)
ENV NODE_ENV=production
ENV UPLOADS_DIR=/uploads

# The container runs once and exits
CMD ["node", "dist/index.js"]
