# Build Angular/Ionic
FROM node:20-alpine AS build
WORKDIR /app

# Activer pnpm via corepack en épinglant explicitement la version.
# Sans cela, corepack télécharge le dernier pnpm (>=11) qui exige Node 22+
# et casse sur node:20-alpine (No such built-in module: node:sqlite).
ENV COREPACK_DEFAULT_TO_LATEST=0
RUN corepack enable && corepack prepare pnpm@10.29.3 --activate

# Copier les fichiers de package depuis le contexte racine
COPY frontend/package.json ./
COPY frontend/ionic.config.json ./
COPY frontend/angular.json ./
COPY frontend/tsconfig*.json ./

# Installer les dépendances
RUN pnpm install

# Copier le code source
COPY frontend/src/ ./src/
COPY frontend/public/ ./public/
COPY frontend/.browserslistrc ./
COPY frontend/.editorconfig ./
COPY frontend/.eslintrc.json ./

# Build pour Docker — BUILD_CMD=build:docker:dev pour le local (logs actifs)
ARG BUILD_CMD=build:docker
ENV NODE_OPTIONS="--max_old_space_size=4096"
RUN pnpm run ${BUILD_CMD}

# Serve with Nginx
FROM nginx:alpine
COPY --from=build /app/www/ /usr/share/nginx/html
COPY docker/nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
