FROM node:lts-alpine AS base

# Dependencies stage
# This stage is used to install dependencies and prepare the environment
FROM base AS deps
RUN apk add --no-cache libc6-compat openssl
WORKDIR /app
COPY package*.json package-lock.json* ./
RUN npm ci

# Builder stage
# This stage is used to build the application
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .

ARG BASE_URL
ENV NEXT_PUBLIC_BASE_URL=$BASE_URL
ARG INTERNAL_BASE_URL
ENV INTERNAL_BASE_URL=$INTERNAL_BASE_URL
ARG POSTGRES_USER
ARG POSTGRES_PASSWORD
ARG POSTGRES_DB
ARG PGHOST
ARG PGPORT
ENV PGHOST=$PGHOST
ENV POSTGRES_DB=$POSTGRES_DB
ENV PGPORT=$PGPORT
ENV POSTGRES_USER=$POSTGRES_USER
ENV POSTGRES_PASSWORD=$POSTGRES_PASSWORD
ARG JWT_SECRET
ENV JWT_SECRET=$JWT_SECRET
ARG BACKENDSERVER_URL
ENV BACKENDSERVER_URL=$BACKENDSERVER_URL
ARG SENDGRID_API_KEY
ENV SENDGRID_API_KEY=$SENDGRID_API_KEY
ARG STRIPE_SECRET_KEY
ENV STRIPE_SECRET_KEY=$STRIPE_SECRET_KEY
ARG NEXT_PUBLIC_STRIPE_PUBLIC_KEY
ENV NEXT_PUBLIC_STRIPE_PUBLIC_KEY=$NEXT_PUBLIC_STRIPE_PUBLIC_KEY
ARG STRIPE_LIMITED_KEY
ENV STRIPE_LIMITED_KEY=$STRIPE_LIMITED_KEY
ARG STRIPE_WEBHOOK_SECRET
ENV STRIPE_WEBHOOK_SECRET=$STRIPE_WEBHOOK_SECRET
ARG FIC_CLIENT_ID
ENV FIC_CLIENT_ID=$FIC_CLIENT_ID
ARG FIC_ACCESS_TOKEN
ENV FIC_ACCESS_TOKEN=$FIC_ACCESS_TOKEN
ARG FIC_COMPANY_ID
ENV FIC_COMPANY_ID=$FIC_COMPANY_ID
ARG ARUBA_USER
ENV ARUBA_USER=$ARUBA_USER
ARG ARUBA_PASS
ENV ARUBA_PASS=$ARUBA_PASS
ARG EXP_API_USER
ENV EXP_API_USER=$EXP_API_USER
ARG EXP_API_PASS
ENV EXP_API_PASS=$EXP_API_PASS
ARG KEYDB_URL
ENV KEYDB_URL=$KEYDB_URL
ARG TILES_URL
ENV TILES_URL=$TILES_URL
ENV NEXT_TELEMETRY_DISABLED=1
ENV SKIP_REDIS="true"

RUN SKIP_ENV_VALIDATION=1 npm run build

# Runner stage
# This stage is used to run the built application
FROM base AS runner
WORKDIR /app
ENV NODE_ENV=production
ARG BASE_URL
ENV NEXT_PUBLIC_BASE_URL=$BASE_URL
ARG INTERNAL_BASE_URL
ENV INTERNAL_BASE_URL=$INTERNAL_BASE_URL
ARG POSTGRES_USER
ARG POSTGRES_PASSWORD
ARG POSTGRES_DB
ARG PGHOST
ARG PGPORT
ENV PGHOST=$PGHOST
ENV POSTGRES_DB=$POSTGRES_DB
ENV PGPORT=$PGPORT
ENV POSTGRES_USER=$POSTGRES_USER
ENV POSTGRES_PASSWORD=$POSTGRES_PASSWORD
ARG JWT_SECRET
ENV JWT_SECRET=$JWT_SECRET
ARG BACKENDSERVER_URL
ENV BACKENDSERVER_URL=$BACKENDSERVER_URL
ARG SENDGRID_API_KEY
ENV SENDGRID_API_KEY=$SENDGRID_API_KEY
ARG STRIPE_SECRET_KEY
ENV STRIPE_SECRET_KEY=$STRIPE_SECRET_KEY
ARG NEXT_PUBLIC_STRIPE_PUBLIC_KEY
ENV NEXT_PUBLIC_STRIPE_PUBLIC_KEY=$NEXT_PUBLIC_STRIPE_PUBLIC_KEY
ARG STRIPE_LIMITED_KEY
ENV STRIPE_LIMITED_KEY=$STRIPE_LIMITED_KEY
ARG STRIPE_WEBHOOK_SECRET
ENV STRIPE_WEBHOOK_SECRET=$STRIPE_WEBHOOK_SECRET
ARG FIC_CLIENT_ID
ENV FIC_CLIENT_ID=$FIC_CLIENT_ID
ARG FIC_ACCESS_TOKEN
ENV FIC_ACCESS_TOKEN=$FIC_ACCESS_TOKEN
ARG FIC_COMPANY_ID
ENV FIC_COMPANY_ID=$FIC_COMPANY_ID
ARG ARUBA_USER
ENV ARUBA_USER=$ARUBA_USER
ARG ARUBA_PASS
ENV ARUBA_PASS=$ARUBA_PASS
ARG EXP_API_USER
ENV EXP_API_USER=$EXP_API_USER
ARG EXP_API_PASS
ENV EXP_API_PASS=$EXP_API_PASS
ARG KEYDB_URL
ENV KEYDB_URL=$KEYDB_URL
ARG TILES_URL
ENV TILES_URL=$TILES_URL
ENV NEXT_TELEMETRY_DISABLED=1
ENV SKIP_REDIS="false"

RUN addgroup -g 1001 -S nodejs
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

CMD HOSTNAME="0.0.0.0" node server.js

