FROM node:24-alpine AS deps
WORKDIR /app
COPY package*.json ./

RUN mkdir frontend
RUN mkdir lstDocs
RUN mkdir controller
COPY frontend/package*.json ./frontend
COPY lstDocs/package*.json ./lstDocs
COPY controller/index.html ./controller

RUN npm install
RUN npm run install:front
RUN npm run install:docs

# Build the Next.js app
FROM node:24-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY --from=deps /app/frontend/node_modules ./frontend/node_modules
COPY --from=deps /app/lstDocs/node_modules ./lstDocs/node_modules
COPY --from=deps /app/controller/index.html ./controller/index.html
COPY . ./
RUN npm run build:app
RUN npm run build:front
RUN npm run build:docs

# Final stage
FROM node:24-alpine
WORKDIR /app
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/frontend/dist ./frontend/dist
COPY --from=builder /app/lstDocs/build ./lstDocs/build
COPY --from=deps /app/controller/index.html ./controller/index.html


ENV NODE_ENV=production
ENV RUNNING_IN_DOCKER=true
EXPOSE 4200
CMD ["node", "dist/main.js"]