FROM ruby:3.4.9-slim AS build

ENV APP_PATH=/opt/hth/application/moonshine/current

RUN apt-get update -qq && \
    apt-get install -y --no-install-recommends \
      build-essential \
      libyaml-dev && \
    rm -rf /var/lib/apt/lists/* && \
    useradd -m -u 21212 -U -s /bin/bash hth

COPY --chown=hth:hth Gemfile Gemfile.lock ${APP_PATH}/
WORKDIR ${APP_PATH}

USER hth

ENV BUNDLE_APP_CONFIG=${APP_PATH}/.bundle

RUN bundle config set path 'vendor/bundle' && \
    bundle config set without 'development test' && \
    bundle install --jobs $(nproc) --retry 3 && \
    bundle clean --force

# --------------------------------------------------

FROM build AS development

COPY --chown=hth:hth . ${APP_PATH}/
COPY --chown=hth:hth --from=build ${APP_PATH}/vendor ${APP_PATH}/vendor
WORKDIR ${APP_PATH}

USER hth

RUN bundle config set path 'vendor/bundle' && \
    bundle config set without '' && \
    bundle install --jobs $(nproc) --retry 3 && \
    bundle clean --force

CMD ["bundle", "exec", "anycable", "--require", "./app.rb"]

# --------------------------------------------------

FROM ruby:3.4.9-slim AS production

ENV APP_PATH=/opt/hth/application/moonshine/current

COPY --chown=21212:21212 . ${APP_PATH}/
COPY --chown=21212:21212 --from=build ${APP_PATH}/vendor ${APP_PATH}/vendor
WORKDIR ${APP_PATH}

USER 21212:21212

ENV BUNDLE_APP_CONFIG=${APP_PATH}/.bundle

RUN bundle config set path 'vendor/bundle' && \
    bundle config set without 'development test'

CMD ["bundle", "exec", "anycable", "--require", "./app.rb"]
