# syntax=docker/dockerfile:1

ARG TURBO_FILTER=@edr/freight-portal
ARG APP_PATH=apps/edr-freight-web/portal

FROM node:24.15.0-alpine AS base
RUN apk add --no-cache libc6-compat
RUN corepack enable
WORKDIR /app

FROM base AS pruner
ARG TURBO_FILTER
COPY . .
RUN pnpm dlx turbo prune "${TURBO_FILTER}" --docker

FROM base AS installer
COPY --from=pruner /app/out/json/ .
COPY --from=pruner /app/out/pnpm-lock.yaml ./pnpm-lock.yaml
RUN --mount=type=secret,id=npmrc,target=./.npmrc,required=false \
    --mount=type=cache,id=pnpm,target=/pnpm/store \
    pnpm install --frozen-lockfile

FROM base AS builder
ARG TURBO_FILTER
ARG APP_PATH
ARG VITE_API_URL
ARG VITE_BASE_API_URL
ARG VITE_USER_MANAGEMENT_BASE
ARG NEXT_PUBLIC_API_URL
ARG VITE_GOOGLE_MAPS_API_KEY
ARG VITE_POSTHOG_KEY
ARG VITE_POSTHOG_HOST
ENV VITE_API_URL=${VITE_API_URL}
ENV VITE_BASE_API_URL=${VITE_BASE_API_URL}
ENV VITE_USER_MANAGEMENT_BASE=${VITE_USER_MANAGEMENT_BASE}
ENV NEXT_PUBLIC_API_URL=${NEXT_PUBLIC_API_URL}
ENV VITE_GOOGLE_MAPS_API_KEY=${VITE_GOOGLE_MAPS_API_KEY}
ENV VITE_POSTHOG_KEY=${VITE_POSTHOG_KEY}
ENV VITE_POSTHOG_HOST=${VITE_POSTHOG_HOST}

RUN if [ -z "$VITE_API_URL" ] || [ -z "$VITE_BASE_API_URL" ] || [ -z "$VITE_USER_MANAGEMENT_BASE" ]; then \
      echo "ERROR: VITE_API_URL, VITE_BASE_API_URL, and VITE_USER_MANAGEMENT_BASE must all be set" && \
      exit 1; \
    fi

COPY --from=installer /app/ .
COPY --from=pruner /app/out/full/ .

# Configure npm/pnpm with extended timeouts and retries for network requests
RUN npm config set fetch-timeout 120000 && \
    npm config set fetch-retry-mintimeout 20000 && \
    npm config set fetch-retry-maxtimeout 120000 && \
    npm config set fetch-retries 5

RUN NODE_OPTIONS="--max-old-space-size=4096" pnpm turbo build --filter="${TURBO_FILTER}..." || \
    (echo "Build failed for ${TURBO_FILTER}" && \
     ls -la /app/${APP_PATH}/ 2>/dev/null || echo "App path does not exist" && \
     exit 1)

# Verify build output exists before proceeding
RUN if [ ! -d "/app/${APP_PATH}/dist" ] && [ ! -d "/app/${APP_PATH}/out" ]; then \
      echo "ERROR: Build output directory not found at /app/${APP_PATH}/dist or /app/${APP_PATH}/out"; \
      echo "Contents of /app/${APP_PATH}:"; \
      ls -la /app/${APP_PATH}/ 2>/dev/null || echo "Directory does not exist"; \
      exit 1; \
    fi

FROM nginx:alpine AS runner
ARG APP_PATH

# Copy nginx configuration
COPY infrastructure/nginx/spa.conf /etc/nginx/conf.d/default.conf

# Remove default nginx files
RUN rm -rf /usr/share/nginx/html/*

# Copy build output - check for Next.js 'out' or Vite 'dist' directory
# Use shell to handle conditional copy
RUN --mount=type=bind,from=builder,source=/app,target=/build \
    if [ -d "/build/${APP_PATH}/out" ]; then \
      cp -r /build/${APP_PATH}/out/* /usr/share/nginx/html/; \
      echo "Copied from Next.js 'out' directory"; \
    elif [ -d "/build/${APP_PATH}/dist" ]; then \
      cp -r /build/${APP_PATH}/dist/* /usr/share/nginx/html/; \
      echo "Copied from Vite 'dist' directory"; \
    else \
      echo "Error: No build output found in 'out' or 'dist' directory"; \
      exit 1; \
    fi

# Copy public directory if it exists (for static assets)
RUN --mount=type=bind,from=builder,source=/app,target=/build \
    if [ -d "/build/${APP_PATH}/public" ]; then \
      mkdir -p /usr/share/nginx/html/public && \
      cp -r /build/${APP_PATH}/public/* /usr/share/nginx/html/public/ && \
      echo "Copied public directory"; \
    else \
      echo "No public directory found, skipping"; \
    fi

EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
