# Office Translator - Frontend Dockerfile
# Multi-stage build for optimized production

# ---- Build stage ----
FROM node:20-alpine AS builder

WORKDIR /app

# Copy package files first (better layer caching)
COPY frontend/package*.json ./

# Install ALL dependencies (dev needed for build)
RUN npm install

# Copy source code
COPY frontend/ .

# Build arguments
# NEXT_PUBLIC_API_URL: client-side API base (empty = relative URLs via proxy)
# BACKEND_URL: server-side proxy target (must be resolvable from container)
ARG NEXT_PUBLIC_API_URL=
ARG NEXT_PUBLIC_GOOGLE_CLIENT_ID=
ARG BACKEND_URL=http://backend:8000
ENV NEXT_PUBLIC_API_URL=${NEXT_PUBLIC_API_URL}
ENV NEXT_PUBLIC_GOOGLE_CLIENT_ID=${NEXT_PUBLIC_GOOGLE_CLIENT_ID}
ENV BACKEND_URL=${BACKEND_URL}

# Build the application (with standalone output)
RUN npm run build

# ---- Production stage ----
FROM node:20-alpine AS production

WORKDIR /app

# Set NODE_ENV before copying
ENV NODE_ENV=production

# Create non-root user
RUN addgroup -g 1001 -S nodejs && \
    adduser -S nextjs -u 1001

# Copy standalone output from builder
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static

# Set correct ownership
RUN chown -R nextjs:nodejs /app

USER nextjs

# Runtime environment
ENV PORT=3000 \
    HOSTNAME="0.0.0.0"

EXPOSE 3000

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