# Trade-In MVP frontend — Next.js 15 + React 19 + TanStack Query.
# Без Sentry/Tailwind/Leaflet/echarts — лёгкая версия только для trade-in.

# ---- deps ----
FROM node:20-alpine AS deps
WORKDIR /app
COPY package.json package-lock.json* ./
RUN --mount=type=cache,target=/root/.npm \
    npm install --legacy-peer-deps --no-audit --no-fund


# ---- builder ----
FROM node:20-alpine AS builder
WORKDIR /app
ENV NEXT_TELEMETRY_DISABLED=1

# basePath + API-prefix baked-in во время build (Next.js — build-time only).
# В CI workflow для prod:
#   --build-arg NEXT_PUBLIC_BASE_PATH=/trade-in
#   --build-arg NEXT_PUBLIC_API_BASE_URL=/trade-in
# В локальном dev: оба пусто = no-op (same-origin).
ARG NEXT_PUBLIC_BASE_PATH=""
ENV NEXT_PUBLIC_BASE_PATH=$NEXT_PUBLIC_BASE_PATH
ARG NEXT_PUBLIC_API_BASE_URL=""
ENV NEXT_PUBLIC_API_BASE_URL=$NEXT_PUBLIC_API_BASE_URL
ARG NEXT_PUBLIC_ENABLE_PREVIEW=""
ENV NEXT_PUBLIC_ENABLE_PREVIEW=$NEXT_PUBLIC_ENABLE_PREVIEW

COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN npm run build


# ---- runner ----
FROM node:20-alpine AS runner
WORKDIR /app
# HOSTNAME=0.0.0.0 — Next.js standalone server.js биндится по этому env.
# Без него Docker ставит HOSTNAME=<container-id> и сервер недоступен по
# имени сервиса из других контейнеров (Caddy → 502).
ENV NODE_ENV=production \
    NEXT_TELEMETRY_DISABLED=1 \
    HOSTNAME=0.0.0.0 \
    PORT=3000

COPY --from=builder --chown=node:node /app/public ./public
COPY --from=builder --chown=node:node /app/.next/standalone ./
COPY --from=builder --chown=node:node /app/.next/static ./.next/static

USER node
EXPOSE 3000
CMD ["node", "server.js"]
