FROM node:22-alpine AS build
WORKDIR /app

ARG VITE_API_BASE_URL
ARG VITE_GOOGLE_MAPS_PLATFORM_KEY

ENV VITE_API_BASE_URL=$VITE_API_BASE_URL
ENV VITE_GOOGLE_MAPS_PLATFORM_KEY=$VITE_GOOGLE_MAPS_PLATFORM_KEY

COPY package*.json ./
RUN npm ci

COPY . .
RUN npm run build && npm run build:admin && npm run build:api

FROM nginx:alpine AS web
COPY --from=build /app/dist /usr/share/nginx/html
COPY nginx.default.conf /etc/nginx/conf.d/default.conf

FROM nginx:alpine AS web-admin
COPY --from=build /app/dist-admin /usr/share/nginx/html
COPY nginx.admin.conf /etc/nginx/conf.d/default.conf

FROM node:22-alpine AS runtime-base
WORKDIR /app

COPY package*.json ./
RUN npm ci --omit=dev

COPY --from=build /app/dist-server ./dist-server
COPY db/migrations ./dist-server/db/migrations

FROM runtime-base AS api
CMD ["npm", "run", "start:api"]

FROM runtime-base AS worker
CMD ["npm", "run", "start:worker"]

FROM runtime-base AS migrate
CMD ["node", "dist-server/db/scripts/migrate.js"]
