# syntax=docker/dockerfile:1
#
# Target: linux/arm64 (ARMv8 / aarch64). Build from the repository root:
#   docker build -f nextjs/Dockerfile -t catherine-league-web .
# Cross-build from amd64 (optional, requires QEMU/binfmt, e.g. Docker Desktop / buildx):
#   docker buildx build --platform linux/arm64 -f nextjs/Dockerfile -t catherine-league-web .
#
# Run (set DB_* env vars as needed):
#   docker run --rm -p 3000:3000 \
#     -e DB_ENDPOINT=host.docker.internal \
#     -e DB_PORT=3306 \
#     -e DB_USER=... \
#     -e DB_PASS=... \
#     catherine-league-web

FROM arm64v8/node:24-alpine AS deps
RUN apk add --no-cache libc6-compat

WORKDIR /app

COPY package.json package-lock.json ./
COPY nextjs/package.json ./nextjs/

RUN npm ci

FROM arm64v8/node:24-alpine AS builder
RUN apk add --no-cache libc6-compat

WORKDIR /app

COPY --from=deps /app/node_modules ./node_modules
COPY --from=deps /app/package.json ./package.json
COPY --from=deps /app/package-lock.json ./package-lock.json
COPY nextjs ./nextjs

ENV NEXT_TELEMETRY_DISABLED=1

RUN npm run build -w catherine-league-nextjs

FROM arm64v8/node:24-alpine AS runner
RUN apk add --no-cache libc6-compat

WORKDIR /app

ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1

RUN addgroup --system --gid 1001 nodejs \
  && adduser --system --uid 1001 nextjs

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

RUN chown -R nextjs:nodejs /app

USER nextjs

EXPOSE 3000

ENV PORT=3000
ENV HOSTNAME=0.0.0.0

WORKDIR /app/nextjs

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