FROM node:24-trixie-slim AS build

WORKDIR /app

COPY backend/package*.json backend/
COPY frontend/package*.json frontend/
RUN cd backend && npm ci
RUN cd frontend && npm ci

COPY . .

RUN cd backend && npm run build
RUN cd frontend && npm run build
RUN cd backend && npm prune --omit=dev

FROM node:24-trixie-slim AS runtime

WORKDIR /app
ENV NODE_ENV=production
ENV PORT=3000

COPY --from=build /app/dist dist
COPY --from=build /app/templates templates
COPY --from=build /app/templates.json templates.json
COPY --from=build /app/backend/node_modules dist/backend/node_modules

WORKDIR /app
COPY entrypoint.sh entrypoint.sh
COPY healthcheck.js healthcheck.js
RUN chmod +x entrypoint.sh

EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 CMD ["node", "/app/healthcheck.js"]
ENTRYPOINT ["./entrypoint.sh"]
