ARG AWS_ACCOUNT=086679231553

# Build stage
FROM --platform=linux/amd64 ${AWS_ACCOUNT}.dkr.ecr.us-east-1.amazonaws.com/docker-parent-images:node20 AS builder

WORKDIR /build

# Copy package files and local tarball dependency
COPY package.json pnpm-lock.yaml console-ui-kit-0.1.15.tgz ./

# Install pnpm and dependencies
USER root
RUN npm install -g pnpm && \
    pnpm install

# Switch back to node user for build
USER node

# Copy source code
COPY . .

# Build the Angular application
RUN pnpm run build

# Production stage - use nginx to serve the built app
FROM --platform=linux/amd64 nginx:1.29.4

LABEL service=dx-ui

# Copy nginx configuration
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/nginx.conf

# Copy built application from builder
COPY --from=builder /build/dist/dx-angular/browser /usr/share/nginx/html

# Copy runtime config template and startup renderer
COPY public/runtime-config.template.js /usr/share/nginx/html/runtime-config.template.js
COPY docker/40-runtime-config.sh /docker-entrypoint.d/40-runtime-config.sh

# Set permissions for non-root nginx runtime
RUN mkdir -p /tmp/client_temp /tmp/proxy_temp /tmp/fastcgi_temp /tmp/uwsgi_temp /tmp/scgi_temp && \
    chown -R nginx:nginx /usr/share/nginx/html /tmp/client_temp /tmp/proxy_temp /tmp/fastcgi_temp /tmp/uwsgi_temp /tmp/scgi_temp && \
    chmod +x /docker-entrypoint.d/40-runtime-config.sh && \
    chmod -R 755 /usr/share/nginx/html

USER nginx

EXPOSE 8080

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