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


WORKDIR /app

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

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

COPY package*.json ./

RUN npm ci

# ------------------------
# Builder stage
# ------------------------
FROM base AS builder

WORKDIR /build

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

COPY . .

RUN NEXT_TELEMETRY_DISABLED=1 npm run build -- --no-lint || true

# ------------------------
# Runner stage (production)
# ------------------------
FROM base AS runner

WORKDIR /app

ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"

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 port
EXPOSE 3000

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