# syntax=docker/dockerfile:1

FROM node:20-alpine AS base

RUN apk add --no-cache tzdata \
  && cp /usr/share/zoneinfo/Asia/Tehran /etc/localtime \
  && echo "Asia/Tehran" > /etc/timezone

FROM base AS deps

WORKDIR /deps

RUN apk add --no-cache python3 make g++ vips-dev

COPY package.json package-lock.json ./

RUN --mount=type=cache,target=/root/.npm \
  npm ci

FROM base AS prod-deps

WORKDIR /deps

RUN apk add --no-cache python3 make g++ vips-dev

COPY package.json package-lock.json ./

RUN --mount=type=cache,target=/root/.npm \
  npm ci --omit=dev

FROM base AS builder

WORKDIR /build

RUN apk add --no-cache python3 make g++ vips-dev

COPY package.json package-lock.json nuxt.config.js ./
COPY assets components layouts middleware mixins modules pages plugins server static store ./

COPY --from=deps /deps/node_modules ./node_modules

RUN npm run build

FROM base AS runner

WORKDIR /app

RUN apk add --no-cache vips \
  && addgroup -S appgroup \
  && adduser -S appuser -G appgroup

ENV NODE_ENV=production \
  NUXT_TELEMETRY_DISABLED=1 \
  PORT=3000 \
  HOSTNAME=0.0.0.0

COPY --from=prod-deps --chown=appuser:appgroup /deps/node_modules ./node_modules
COPY --from=builder --chown=appuser:appgroup /build/.nuxt ./.nuxt
COPY --from=builder --chown=appuser:appgroup /build/static ./static
COPY --from=builder --chown=appuser:appgroup /build/server ./server
COPY --from=builder --chown=appuser:appgroup /build/modules ./modules
COPY --from=builder --chown=appuser:appgroup /build/nuxt.config.js /build/package.json ./

USER appuser

EXPOSE 3000

CMD ["node", "node_modules/nuxt/bin/nuxt.js", "start"]
