###########
# Stage 1 #
###########
# Build stage with all dependencies
FROM node:24.12-alpine as build

WORKDIR /app

# Copy package files
COPY . .

# build backend
RUN npm ci
RUN npm run build:docker

# build frontend
RUN npm --prefix frontend ci
RUN npm --prefix frontend run build

###########
# Stage 2 #
###########
# Small final image with only what’s needed to run
FROM node:24.12-alpine AS production

WORKDIR /app

# Copy package files first to install runtime deps
COPY package*.json ./

# curl install 
RUN apk add --no-cache curl

# Only install production dependencies
RUN npm ci --omit=dev


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

# TODO add in drizzle migrates

ENV RUNNING_IN_DOCKER=true
EXPOSE 3000
# start the app up
CMD ["npm", "run", "start:docker"]

# Add health check
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
  CMD curl -f http://localhost:3000/lst/api/stats || exit 1