
FROM node:18-bullseye-slim AS build

WORKDIR /app

RUN apt-get update \
    && apt-get install -y --no-install-recommends python3 make g++ \
    && rm -rf /var/lib/apt/lists/*

COPY front-end/package.json front-end/package-lock.json ./
RUN npm ci

COPY front-end/ ./


ARG NEXT_PUBLIC_CRYPTOPASS
ARG NEXT_PUBLIC_WEBHOOK_LEADS_URL
ARG NEXT_PUBLIC_NODE_API_URL
ARG NEXT_APP_API_URL
ENV NEXT_PUBLIC_CRYPTOPASS=$NEXT_PUBLIC_CRYPTOPASS \
    NEXT_PUBLIC_WEBHOOK_LEADS_URL=$NEXT_PUBLIC_WEBHOOK_LEADS_URL \
    NEXT_PUBLIC_NODE_API_URL=$NEXT_PUBLIC_NODE_API_URL \
    NEXT_APP_API_URL=$NEXT_APP_API_URL \
    NODE_ENV=production

RUN npm run build

FROM node:18-bullseye-slim

WORKDIR /app
ENV NODE_ENV=production \
    PORT=3000 \
    TZ=America/Sao_Paulo

COPY --from=build /app/node_modules ./node_modules
COPY --from=build /app/.next ./.next
COPY --from=build /app/public ./public
COPY --from=build /app/next.config.js /app/package.json /app/server.js ./

RUN mkdir -p /app/.next/cache && chown -R node:node /app/.next
USER node

EXPOSE 3000
CMD ["node", "server.js"]
