# ── Dev stage (default) ──────────────────────────────────────────────────────
FROM node:20-alpine AS dev

WORKDIR /app

# Install dependencies first to leverage Docker layer cache
COPY package*.json ./
RUN npm ci

# Source is mounted at runtime via volume (see docker-compose.yml)
COPY . .

EXPOSE 5173

CMD ["npm", "run", "dev", "--", "--host", "0.0.0.0"]


# ── Build stage ───────────────────────────────────────────────────────────────
FROM dev AS build

# Public API base URL. For root deployments: http://host:port.
# For subpath deployments behind a reverse proxy: /hp-content-agent/api
ARG VITE_API_URL=http://localhost:8000
ENV VITE_API_URL=$VITE_API_URL

# Vite base path. Default "/" for root deployments; set to "/hp-content-agent/"
# (trailing slash required) when hosting behind a subpath.
ARG VITE_BASE_PATH=/
ENV VITE_BASE_PATH=$VITE_BASE_PATH

RUN npm run build


# ── Production stage ──────────────────────────────────────────────────────────
FROM nginx:stable-alpine AS prod

COPY --from=build /app/dist /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.conf

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]
