FROM rust:1.58 as base

# install third party dependencies
RUN curl -fsSL https://deb.nodesource.com/setup_lts.x | bash -
RUN apt-get update -y
RUN apt-get dist-upgrade -y
RUN apt-get install -y nodejs
RUN apt-get install libpq-dev -y
RUN cargo install diesel_cli --features postgres
RUN cargo install cargo-chef
RUN cargo install trunk
RUN cargo install wasm-bindgen-cli
RUN rustup target add wasm32-unknown-unknown
WORKDIR /aspiesolutions


FROM base as planner
COPY . .
RUN cargo chef prepare --recipe-path recipe.json

FROM base as build
COPY --from=planner /aspiesolutions/recipe.json recipe.json
RUN cargo chef cook --recipe-path recipe.json
COPY . .
# bulid the frontend components
ARG CONSOLE_AUTH0_CLIENT_ID
ARG CONSOLE_AUTH0_DOMAIN
ARG CONSOLE_PUBLIC_DOMAIN
WORKDIR /aspiesolutions
RUN CONSOLE_AUTH0_CLIENT_ID=$CONSOLE_AUTH0_CLIENT_ID\
    CONSOLE_AUTH0_DOMAIN=$CONSOLE_AUTH0_DOMAIN\
    CONSOLE_PUBLIC_DOMAIN=$CONSOLE_PUBLIC_DOMAIN\
    trunk build  ./yew-console-bin/index.html --release --public-url /
RUN mkdir -p html

COPY ./yew-console-bin/dist/ html
# nodejs
WORKDIR ./neon-api
RUN npm install
# prepare the frontend file structure



FROM base as runtime
WORKDIR /aspiesolutions
COPY --from=build /aspiesolutions/html/ html
COPY --from=build /aspiesolutions/neon-api/ ./
COPY --from=build /aspiesolutions/heroku_start.sh .
ENTRYPOINT ["./heroku_start.sh"]