# ---- stage 1: build FE ----
FROM node:22-alpine AS build
WORKDIR /app

# Biến môi trường Vite (đọc từ .env trên prod lúc compose build).
# Đổi API URL -> build lại là nhận (compose up --build tự làm).
ARG VITE_API_URL=http://localhost:8000/api/v1
ARG VITE_APP_NAME=danable
ARG VITE_USE_MOCK_DATA=true
ENV VITE_API_URL=$VITE_API_URL \
    VITE_APP_NAME=$VITE_APP_NAME \
    VITE_USE_MOCK_DATA=$VITE_USE_MOCK_DATA

COPY package.json package-lock.json ./
RUN npm ci
COPY index.html vite.config.ts tsconfig.json tailwind.config.js postcss.config.js ./
COPY public ./public
COPY src ./src
RUN npm run build

# ---- stage 2: serve tĩnh bằng nginx ----
FROM nginx:alpine
COPY --from=build /app/dist /usr/share/nginx/html
# Ghi đè default conf: SPA fallback + cache lâu cho /assets.
# ($$ trong Dockerfile -> $ trong file config.)
RUN printf '%s\n' \
  'server {' \
  '    listen 80;' \
  '    server_name _;' \
  '    root /usr/share/nginx/html;' \
  '    index index.html;' \
  '    location / {' \
  '        try_files $uri $uri/ /index.html;' \
  '    }' \
  '    location /assets/ {' \
  '        expires 30d;' \
  '        add_header Cache-Control "public, immutable";' \
  '    }' \
  '}' > /etc/nginx/conf.d/default.conf
EXPOSE 80
