# syntax = docker/dockerfile:1

ARG NODE_VERSION=20-slim

FROM node:${NODE_VERSION} AS base

WORKDIR /src

RUN apt-get update && \
    apt-get install -y --no-install-recommends curl && \
    rm -rf /var/lib/apt/lists/*

# ── Build ─────────────────────────────────────────────────────────────────────
FROM base AS build

ENV NODE_ENV=development

COPY frontend/package.json frontend/package-lock.json* ./
RUN npm ci

COPY frontend/ .
RUN npm run build

# ── Production ────────────────────────────────────────────────────────────────
FROM base AS production

ENV PORT=3000 \
    NODE_ENV=production

COPY --from=build /src/.output /src/.output

EXPOSE $PORT

HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 \
    CMD curl -f http://localhost:${PORT}/ || exit 1

CMD ["node", ".output/server/index.mjs"]

# ── Development ───────────────────────────────────────────────────────────────
FROM base AS development

ENV NODE_ENV=development

WORKDIR /app

COPY frontend/package.json ./
RUN npm install

EXPOSE 3002

CMD ["npm", "run", "dev"]
