# syntax=docker/dockerfile:1

ARG REGISTRY=docker.arvancloud.ir

# ----------------------------
# Stage 1: deps
# ----------------------------
FROM ${REGISTRY}/node:20-alpine AS deps
WORKDIR /app

# نصب وابستگی‌های sharp (بدون mirror)
RUN apk add --no-cache \
    vips-dev \
    build-base \
    python3 \
    gcc \
    g++ \
    make

COPY package.json package-lock.json ./
RUN npm ci --no-audit


# ----------------------------
# Stage 2: builder
# ----------------------------
FROM ${REGISTRY}/node:20-alpine AS builder
WORKDIR /app

# sharp build deps
RUN apk add --no-cache \
    vips-dev \
    build-base \
    python3 \
    gcc \
    g++ \
    make

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

ENV NEXT_TELEMETRY_DISABLED=1

RUN npm run build


# ----------------------------
# Stage 3: runner
# ----------------------------
FROM ${REGISTRY}/node:20-alpine AS runner
WORKDIR /app

ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1

# در مرحله اجرا فقط libvips runtime لازم داریم
RUN apk add --no-cache vips

RUN addgroup -S nodejs -g 1001
RUN adduser -S nextjs -u 1001

COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

USER nextjs

EXPOSE 3000
ENV PORT=3000
ENV HOSTNAME=0.0.0.0

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