# Devwebs web app (Next.js standalone server).
# API_URL and PUBLIC_URL are read when the app is built (rewrites and security headers are compiled in):
#   docker build -t devwebs-web --build-arg API_URL=http://api:4000 --build-arg PUBLIC_URL=https://crm.example.com ./frontend
FROM node:22-bookworm-slim AS build
WORKDIR /app
ARG API_URL=http://api:4000
ARG PUBLIC_URL=
ENV API_URL=$API_URL PUBLIC_URL=$PUBLIC_URL NEXT_TELEMETRY_DISABLED=1
COPY package.json package-lock.json ./
RUN npm ci --no-audit --no-fund
COPY . .
RUN npm run build

FROM node:22-bookworm-slim
ENV NODE_ENV=production PORT=3000 HOSTNAME=0.0.0.0 NEXT_TELEMETRY_DISABLED=1
WORKDIR /app
COPY --from=build --chown=node:node /app/.next/standalone ./
COPY --from=build --chown=node:node /app/.next/static ./.next/static
COPY --from=build --chown=node:node /app/public ./public
USER node
EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
  CMD node -e "fetch('http://127.0.0.1:3000/login').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))"
CMD ["node", "server.js"]
