## Build the new Angular frontend (same-origin /api/v1/ base URL)
FROM node:22-alpine AS frontend-build
WORKDIR /app/frontend
COPY myteamwallet_frontend_modern/package.json myteamwallet_frontend_modern/package-lock.json ./
RUN npm ci
COPY myteamwallet_frontend_modern/ ./
RUN npm run build:container

## Build the NestJS backend
FROM node:22-alpine AS backend-build
WORKDIR /app
COPY myteamwallet_backend/package.json myteamwallet_backend/package-lock.json ./
RUN npm ci --ignore-scripts
COPY myteamwallet_backend/ ./
RUN npm run build

## Runtime image: backend + frontend static assets, external DB only
FROM node:22-alpine
WORKDIR /app

RUN apk add --no-cache bash

COPY myteamwallet_backend/package.json myteamwallet_backend/package-lock.json ./
RUN npm ci --omit=dev --ignore-scripts
ENV NODE_ENV=production
COPY --from=backend-build /app/dist ./dist
COPY myteamwallet_backend/wait-for-it.sh ./wait-for-it.sh
COPY myteamwallet_backend/docker-entrypoint.sh ./docker-entrypoint.sh
RUN sed -i 's/\r$//' wait-for-it.sh docker-entrypoint.sh \
  && chmod +x wait-for-it.sh docker-entrypoint.sh

COPY --from=frontend-build /app/frontend/dist/myteamwallet_frontend_modern/browser ./client

EXPOSE 3999

ENTRYPOINT ["./docker-entrypoint.sh"]
