FROM node:20-alpine AS builder

RUN apk add --no-cache libc6-compat

WORKDIR /build

# Copy shared types
COPY shared/ ./shared/

# Copy frontend
COPY frontend/package*.json ./frontend/
WORKDIR /build/frontend
RUN npm install --legacy-peer-deps

COPY frontend/ ./

# Build Next.js
ENV NEXT_TELEMETRY_DISABLED=1
RUN npm run build

# --- Production ---
FROM node:20-alpine

RUN apk add --no-cache libc6-compat

WORKDIR /app

ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1

COPY --from=builder /build/frontend/.next/standalone ./
COPY --from=builder /build/frontend/.next/static ./.next/static
COPY --from=builder /build/frontend/public ./public

EXPOSE 3000

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