FROM node:22-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 ./
ENV NODE_ENV=production

RUN echo "base variables: $NODE_ENV is set to $NODE_ENV"
RUN npm i --omit=dev

RUN echo "Dependencies installed successfully"

# 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 . .

ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1
ENV SKIP_REDIS="true"

# Only essential build-time variables
ARG BASE_URL
ENV BASE_URL=$BASE_URL
ARG NEXT_PUBLIC_BASE_URL
ENV NEXT_PUBLIC_BASE_URL=$NEXT_PUBLIC_BASE_URL

ARG NEXT_PUBLIC_STRIPE_PUBLIC_KEY
ENV NEXT_PUBLIC_STRIPE_PUBLIC_KEY=$NEXT_PUBLIC_STRIPE_PUBLIC_KEY

# 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
# ENV JWT_SECRET="build-time-mock"
# ENV BACKENDSERVER_URL="http://backend:1323"
ENV STRIPE_SECRET_KEY="mock"
ENV STRIPE_WEBHOOK_SECRET="mock"
# ENV FIC_CLIENT_ID="mock"
# ENV FIC_ACCESS_TOKEN="mock"
# ENV FIC_COMPANY_ID="mock"
# ENV ARUBA_USER="mock"
# ENV ARUBA_PASS="mock"
# ENV EXP_API_USER="mock"
# ENV EXP_API_PASS="mock"
# ENV KEYDB_URL="localhost:6380"
# ENV TILES_URL="localhost:6379"
# ENV SKEBBY_USER="mock"
# ENV SKEBBY_PASS="mock"
# ENV REVALIDATION_SECRET="mock"
# ENV STORAGE_URL="mock"
# ENV STORAGE_TOKEN="mock"

RUN SKIP_ENV_VALIDATION=1 npm run build


RUN echo "Build completed successfully"
RUN echo "Build with BASE_URL=$BASE_URL and NEXT_PUBLIC_BASE_URL=$NEXT_PUBLIC_BASE_URL"

# Runner stage
# This stage is used to run the built application
FROM base AS runner
WORKDIR /app

ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1
ENV SKIP_REDIS="false"

# Essential runtime variables
ARG BASE_URL
ENV BASE_URL=$BASE_URL
ARG NEXT_PUBLIC_BASE_URL
ENV NEXT_PUBLIC_BASE_URL=$NEXT_PUBLIC_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 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_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
ARG SKEBBY_USER
ENV SKEBBY_USER=$SKEBBY_USER
ARG SKEBBY_PASS
ENV SKEBBY_PASS=$SKEBBY_PASS
ENV REVALIDATION_SECRET="SWKpgaaLfsyeqV1eOT0WG7TUFBewir8kJXure3O37ki8Lt4Z4IiEgBW4zPHDdM5c"
ARG STORAGE_URL
ENV STORAGE_URL=$STORAGE_URL
ARG STORAGE_TOKEN
ENV STORAGE_TOKEN=$STORAGE_TOKEN


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

RUN echo "Starting application with BASE_URL=$BASE_URL and NEXT_PUBLIC_BASE_URL=$NEXT_PUBLIC_BASE_URL"

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

