# Build stage
FROM node:20.15.1-alpine AS builder

# Configure build dependencies
RUN apk add --no-cache --virtual .build-dependencies \
    autoconf \
    automake \
    g++ \
    gcc \
    libtool \
    make \
    nasm \
    libpng-dev \
    python3

WORKDIR /usr/src/app

# Copy only necessary files for build
COPY . .
COPY server-package.json package.json

# Build and cleanup in a single layer
RUN cp -R build/src/* src/. && \
    cp build/docker_healthcheck.js . && \
    rm -r build && \
    rm docker_healthcheck.ts && \
    npm install && \
    npm run webpack && \
    npm prune --omit=dev && \
    npm cache clean --force && \
    cp src/public/app/share.js src/public/app-dist/. && \
    cp -r src/public/app/doc_notes src/public/app-dist/. && \
    rm -rf src/public/app && \
    rm src/services/asset_path.ts

# Runtime stage
FROM node:20.15.1-alpine

# Install runtime dependencies
RUN apk add --no-cache su-exec shadow

WORKDIR /usr/src/app

# Copy only necessary files from builder
COPY --from=builder /usr/src/app/node_modules ./node_modules
COPY --from=builder /usr/src/app/src ./src
COPY --from=builder /usr/src/app/db ./db
COPY --from=builder /usr/src/app/docker_healthcheck.js .
COPY --from=builder /usr/src/app/start-docker.sh .
COPY --from=builder /usr/src/app/package.json .
COPY --from=builder /usr/src/app/config-sample.ini .
COPY --from=builder /usr/src/app/images ./images
COPY --from=builder /usr/src/app/translations ./translations
COPY --from=builder /usr/src/app/libraries ./libraries

# Add application user
RUN adduser -s /bin/false node; exit 0

# Configure container
EXPOSE 8080
CMD [ "./start-docker.sh" ]
HEALTHCHECK --start-period=10s CMD exec su-exec node node docker_healthcheck.js