FROM node:22-bookworm-slim AS build

WORKDIR /app

COPY package*.json ./
COPY frontend/package*.json ./frontend/

RUN npm ci
RUN npm --prefix frontend ci

COPY tsconfig.json ./
COPY server ./server
COPY migrations ./migrations
COPY frontend ./frontend

RUN npm run build

FROM node:22-bookworm-slim AS runtime

WORKDIR /app
ENV NODE_ENV=production
ENV PORT=3000

COPY package*.json ./
RUN npm ci --omit=dev && npm cache clean --force

COPY --from=build /app/dist ./dist
COPY --from=build /app/frontend/dist ./frontend/dist
COPY migrations ./migrations

EXPOSE 3000

CMD ["npm", "run", "start:deploy"]
