# Build arguments
ARG NODE_VERSION=20
ARG NODE_ALPINE_SHA256=sha256:6a91081a440be0b57336fbc4ee87f3dab1a2fd6f80cdb355dcf960e13bda3b59

# Build the frontend
FROM node:${NODE_VERSION}-alpine@${NODE_ALPINE_SHA256} AS build-frontend

WORKDIR /src

COPY package.json vite.config.js pnpm-lock.yaml tailwind.config.js postcss.config.js ./
# Copy only the frontend source and version info to speed up builds and reduce image size
COPY meshchatx/src/frontend ./meshchatx/src/frontend
COPY meshchatx/src/version.py ./meshchatx/src/version.py

RUN corepack enable && corepack prepare pnpm@latest --activate

RUN pnpm install

RUN pnpm run build-frontend

RUN find /src/meshchatx/public -type d -exec chmod 755 {} + && \
    find /src/meshchatx/public -type f -exec chmod 644 {} +

# Runtime stage
FROM nginxinc/nginx-unprivileged:alpine

COPY --from=build-frontend --chown=101:101 /src/meshchatx/public /usr/share/nginx/html
COPY nginx.demo.conf /etc/nginx/conf.d/default.conf

EXPOSE 8080

CMD ["nginx", "-g", "daemon off;"]
