# Base image
FROM node:20 as build
USER root

RUN npm install -g npm@9.6.5

# Create GitHub Access to clone repo of database
RUN  mkdir /root/.ssh/
COPY copolo.pem /root/.ssh/id_rsa
RUN chmod 400 /root/.ssh/id_rsa
RUN ssh-keyscan github.com >> /root/.ssh/known_hosts

# Create app directory
WORKDIR /usr/src/app

# A wildcard is used to ensure both package.json AND package-lock.json are copied
COPY package*.json ./

# Install app dependencies
RUN npm install

COPY . .

# Ensure the entrypoint script has the correct permissions and line endings
RUN sed -i 's/\r//' ./entrypoint.sh && chmod +x ./entrypoint.sh

RUN npm run build:all
RUN npm prune --omit=dev

# TODO ::: ENABLE THIS WHEN PLAN FOR LAMBDA
# FROM public.ecr.aws/lambda/nodejs:20 As production
FROM node:20-alpine as production
# Create app directory
WORKDIR /usr/src/app
# Copy the bundled code from the build stage to the production image
COPY --from=build /usr/src/app/node_modules /usr/src/app/node_modules
COPY --from=build /usr/src/app/ /usr/src/app/
COPY --from=build /usr/src/app/entrypoint.sh /usr/src/app/entrypoint.sh

# Environment variable to determine which app to run
ENV APP_NAME=microservice
# Make the entrypoint script executable
RUN chmod +x entrypoint.sh
EXPOSE 3000
# Entrypoint script, TODO:::ENABLE THIS WHEN PLAN FOR LAMBDA
# ENTRYPOINT ["sh", "./entrypoint.sh"]
# Default command for Lambda
# TODO: when plan for lambda enable lambda as default command
# CMD [ "dist/apps/booking-lambda/index.handler" ]
# now we have microservice as default command
CMD [ "node", "dist/apps/tbi-copolo-mobile-ms-booking/main.js" ]
