# Build stage
FROM node:24-trixie-slim AS build

WORKDIR /app

# Copy sources required for build
COPY backend/*.json backend/
COPY backend/src backend/src

COPY frontend/*.json frontend/
COPY frontend/vite.config.ts frontend/vite.config.ts
COPY frontend/index.html frontend/index.html
COPY frontend/src frontend/src
COPY frontend/test frontend/test

COPY templates templates
COPY templates.json templates.json

# Build backend and frontend
RUN cd backend && npm ci && npm run build && npm prune --omit=dev
RUN cd frontend && npm ci && npm run build

# Build the container
FROM node:24-trixie-slim AS runtime

WORKDIR /app

# Copy built artifacts
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

# Copy entrypoint and healthcheck scripts
COPY entrypoint.sh entrypoint.sh
COPY healthcheck.js healthcheck.js

# Ensure entrypoint script is executable
RUN chmod +x entrypoint.sh

# Set environment variables and expose port
ENV NODE_ENV=production
ENV PORT=3000
EXPOSE 3000

# Configure health check
HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 CMD ["node", "/app/healthcheck.js"]

# Configure entrypoint
ENTRYPOINT ["./entrypoint.sh"]
