# Build stage
FROM --platform=$TARGETPLATFORM node:20-slim AS builder

WORKDIR /app

# Copy package files
COPY backend/package*.json  ./backend/
COPY frontend/package*.json ./frontend/

# Copy source files
COPY . .

# Build frontend and backend
RUN npm install
RUN npm run build --workspaces

# Production stage
FROM --platform=$TARGETPLATFORM debian:testing-20250203

RUN apt-get update && apt-get install -y \
    mpv npm yt-dlp pulseaudio pulseaudio-utils ffmpeg \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Install only production dependencies
COPY backend/package*.json ./
COPY package-lock.json ./

RUN rm -rf node_modules/ # need to do a clean build
RUN npm ci --production

# Copy built files
COPY --from=builder /app/backend/build ./build
COPY --from=builder /app/frontend/dist ./dist/frontend

# Copy entrypoint script
COPY entrypoint.sh ./
RUN chmod +x entrypoint.sh

EXPOSE 3000
CMD ["./entrypoint.sh"]
