# Stage 1: Build the application
FROM node:24-alpine AS build

# Set working directory
WORKDIR /app

# Copy package.json and yarn.lock
COPY package.json yarn.lock ./

# Vite embeds these values into the static bundle at build time. When an
# argument is omitted, the checked-in .env remains the fallback.
ARG VITE_API_URL
ARG VITE_GEO_API_URL
ARG VITE_WEATHER_API_URL
ARG VITE_KRBL_MEDIA

# Install dependencies
RUN yarn install --frozen-lockfile

# Copy the rest of the application code
COPY . .

# Build the application
RUN if [ -n "${VITE_API_URL:-}" ]; then export VITE_API_URL; fi && \
    if [ -n "${VITE_GEO_API_URL:-}" ]; then export VITE_GEO_API_URL; fi && \
    if [ -n "${VITE_WEATHER_API_URL:-}" ]; then export VITE_WEATHER_API_URL; fi && \
    if [ -n "${VITE_KRBL_MEDIA:-}" ]; then export VITE_KRBL_MEDIA; fi && \
    yarn build

# Stage 2: Serve the application with Nginx
FROM nginxinc/nginx-unprivileged:alpine

# Copy the built application from the build stage
COPY --from=build /app/dist /usr/share/nginx/html

# Keep client-side routes working on direct navigation and page refresh.
COPY nginx.conf /etc/nginx/conf.d/default.conf

# The unprivileged image cannot bind to port 80.
EXPOSE 8080

# Start Nginx server
CMD ["nginx", "-g", "daemon off;"]
