# syntax=docker/dockerfile:1

# ------------------------
# Dependencies
# ------------------------
FROM node:22-alpine AS deps

RUN sed -i 's|https://mirror.de.velop.ir/alpine|g' /etc/apk/repositories \
    && npm config set registry https://package-mirror.liara.ir/repository/npm

RUN apk add --no-cache libc6-compat git

WORKDIR /app

COPY package.json package-lock.json ./

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

# ------------------------
# Builder
# ------------------------
FROM node:22-alpine AS builder

RUN sed -i 's|https://mirror.de.velop.ir/alpine|g' /etc/apk/repositories \
    && npm config set registry https://package-mirror.liara.ir/repository/npm

WORKDIR /build

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

ENV NEXT_TELEMETRY_DISABLED=1

RUN npm run build

# ------------------------
# Runner (production)
# ------------------------
FROM node:22-alpine AS runner

RUN sed -i 's|https://mirror.de.velop.ir/alpine|g' /etc/apk/repositories \
    && npm config set registry https://package-mirror.liara.ir/repository/npm

WORKDIR /app

ENV NODE_ENV=production \
    NEXT_TELEMETRY_DISABLED=1 \
    PORT=3000 \
    HOSTNAME=0.0.0.0 \
    TZ=Asia/Tehran

RUN addgroup -S appgroup \
    && adduser -S appuser -G appgroup \
    && mkdir .next \
    && chown appuser:appgroup .next

COPY --from=builder --chown=appuser:appgroup /build/.next/standalone ./
COPY --from=builder --chown=appuser:appgroup /build/.next/static ./.next/static
COPY --from=builder --chown=appuser:appgroup /build/public ./public

USER appuser

EXPOSE 3000

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