# --------------------
# Base
# --------------------
FROM node:22-alpine AS base

# Change Alpine repo
RUN sed -i 's|https://dl-cdn.alpinelinux.org/alpine|https://mirror.de.velop.ir/alpine|g' /etc/apk/repositories

# Configure npm registry mirror (Liara)
ENV NPM_CONFIG_REGISTRY=https://package-mirror.liara.ir/repository/npm/
RUN npm config set registry https://package-mirror.liara.ir/repository/npm/


WORKDIR /app

# --------------------
# Dependencies
# --------------------
FROM base AS deps

# RUN apk add --no-cache libc6-compat git
COPY package.json package-lock.json ./
RUN npm ci --legacy-peer-deps --ignore-scripts --loglevel info

# --------------------
# Build
# --------------------
FROM base AS builder
WORKDIR /build

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

RUN npm run build && \
    npm ci --omit=dev --legacy-peer-deps --ignore-scripts --loglevel info

# --------------------
# Runtime
# --------------------
FROM base AS runner
WORKDIR /app

ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1

RUN addgroup -S appgroup && adduser -S appuser -G appgroup
RUN 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
COPY --from=builder --chown=appuser:appgroup /build/package.json ./package.json

USER appuser

EXPOSE 3000
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"

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


# FROM node:22-alpine AS base

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

# RUN npm install -g corepack
# RUN corepack enable && corepack prepare pnpm@latest --activate
# WORKDIR /app

# FROM base AS deps
# RUN apk add --no-cache libc6-compat git
# COPY package*.json ./
# COPY pnpm-lock.yaml ./
# RUN pnpm install --frozen-lockfile --ignore-scripts

# FROM base AS builder
# WORKDIR /build
# COPY --from=deps /app/node_modules ./node_modules
# COPY . .
# RUN pnpm build && pnpm install --prod --frozen-lockfile --ignore-scripts

# FROM base AS runner
# WORKDIR /app

# ENV NODE_ENV=production
# ENV NEXT_TELEMETRY_DISABLED=1

# RUN addgroup -S appgroup && adduser -S appuser -G appgroup
# RUN 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
# COPY --from=builder --chown=appuser:appgroup /build/package.json ./package.json

# USER appuser

# EXPOSE 3000
# ENV PORT=3000
# ENV HOSTNAME="0.0.0.0"
# ENV NEXT_PUBLIC_BASE_URL=https://api.danakcorp.com
# ENV NEXT_PUBLIC_TOKEN_NAME=dsc_refresh_token
# ENV NEXT_PUBLIC_TOKEN_NAME=dsc_token

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