# Build stage
FROM node:20-alpine AS builder
WORKDIR /app

# Define build arguments
ARG NEXT_PUBLIC_FIREBASE_AIzaSyDxMtKbCxzfUoY_Klfk06IgKeF9aLIl5EM
ARG NEXT_PUBLIC_FIREBASE_streamview-vhius.firebaseapp.com
ARG NEXT_PUBLIC_FIREBASE_streamview-vhius
ARG NEXT_PUBLIC_FIREBASE_streamview-vhius.firebasestorage.app
ARG NEXT_PUBLIC_FIREBASE_874800452209
ARG NEXT_PUBLIC_FIREBASE_1:874800452209:web:7d30ea5c8acb576a5f81d0
ARG GOOGLE_APPLICATION_CREDENTIALS


# Install dependencies first (improves caching)
COPY package*.json ./

# Install all dependencies including devDependencies
RUN npm ci --include=dev

# Copy the rest of the application
COPY . .

# Set environment to production
ENV NODE_ENV=production

# Create a directory for credentials and write the JSON credentials
RUN mkdir -p /app/credentials && \
    echo "$GOOGLE_APPLICATION_CREDENTIALS" > /app/credentials/google-credentials.json && \
    chmod 644 /app/credentials/google-credentials.json

# Set the environment variable for Google credentials
ENV GOOGLE_APPLICATION_CREDENTIALS=/app/credentials/google-credentials.json

# Build the application with production environment
RUN npm run build

# Production stage
FROM node:20-alpine
WORKDIR /app

# Set environment to production
ENV NODE_ENV=production
ENV PORT=3000

# Copy package files and built assets
COPY --from=builder /app/package*.json ./
COPY --from=builder /app/.next ./.next
# Copy Next.js configuration (TypeScript)
COPY --from=builder /app/next.config.ts ./


# Install only production dependencies
RUN npm ci --only=production

# Expose the port the app will run on
EXPOSE 3000

# Set the command to start the application
CMD ["npm", "start"]