ARG APP_USER_UID=1000
ARG APP_USER_GID=1000
ARG IMAGE_ID=development.00000000

FROM docker-hosted.nexus.bthlabs.pl/hotpocket/base:build-node-20251014-01 AS development

ARG APP_USER_UID
ARG APP_USER_GID
ARG IMAGE_ID

COPY --chown=$APP_USER_UID:$APP_USER_GID backend/ops/bin/*.sh /srv/bin/

VOLUME ["/srv/node_modules", "/srv/venv"]

FROM docker-hosted.nexus.bthlabs.pl/hotpocket/base:build-python-20251014-01 AS deployment-build

ARG APP_USER_UID
ARG APP_USER_GID
ARG IMAGE_ID

RUN mkdir /srv/app/hotpocket_backend /srv/packages/common /srv/packages/soa
COPY --chown=$APP_USER_UID:$APP_USER_GID backend/hotpocket_backend/ /srv/app/hotpocket_backend/
COPY --chown=$APP_USER_UID:$APP_USER_GID backend/poetry.lock backend/pyproject.toml backend/manage.py backend/README.md /srv/app/
COPY --chown=$APP_USER_UID:$APP_USER_GID packages/common/ /srv/packages/common/
COPY --chown=$APP_USER_UID:$APP_USER_GID packages/soa/ /srv/packages/soa/

RUN poetry install --only main,deployment && \
    minify -i --css-precision 0 --js-precision 0 --js-version 2022 hotpocket_backend/apps/ui/static/ui/css/hotpocket-backend*.css hotpocket_backend/apps/ui/static/ui/js/hotpocket*.js && \
    ./manage.py collectstatic --settings hotpocket_backend.settings.deployment.build --noinput && \
    find hotpocket_backend/static/ -name "*.map*" -delete && \
    rm -f hotpocket_backend/settings/deployment/build.py && \
    rm -rf node_modules/

FROM docker-hosted.nexus.bthlabs.pl/hotpocket/base:base-20251014-01 AS deployment-base

ARG APP_USER_UID
ARG APP_USER_GID
ARG IMAGE_ID

ENV HOTPOCKET_BACKEND_IMAGE_ID=${IMAGE_ID}
ENV PYTHONPATH="/srv/local"

COPY --from=deployment-build /srv/app /srv/app
COPY --from=deployment-build /srv/packages /srv/packages
COPY --from=deployment-build /srv/venv /srv/venv
COPY --chown=$APP_USER_UID:$APP_USER_GID backend/ops/bin/*.sh /srv/bin/
COPY --chown=$APP_USER_UID:$APP_USER_GID backend/ops/deployment/gunicorn.conf.py backend/ops/deployment/gunicorn.logging.conf /srv/lib/
COPY --chown=root:root backend/ops/etc/sudoers_app /etc/sudoers.d/app

USER root

RUN apt-get update && \
    apt-get install -y libpq5 dumb-init sudo && \
    apt-get clean autoclean && \
    apt-get autoremove --yes && \
    rm -rf /var/lib/apt /var/lib/dpkg && \
    rm -rf /home/app/.cache

USER app

ENTRYPOINT ["/srv/bin/entrypoint-deployment.sh"]
CMD ["/srv/venv/bin/gunicorn", "-c", "/srv/lib/gunicorn.conf.py", "hotpocket_backend.wsgi:application"]

FROM deployment-base AS deployment

ARG APP_USER_UID
ARG APP_USER_GID
ARG IMAGE_ID

ENV DJANGO_SETTINGS_MODULE=hotpocket_backend.settings.deployment.webapp
ENV HOTPOCKET_BACKEND_APP_USER_UID=${APP_USER_UID}
ENV HOTPOCKET_BACKEND_APP_USER_GID=${APP_USER_GID}
ENV HOTPOCKET_BACKEND_ENV=deployment
ENV HOTPOCKET_BACKEND_APP=webapp

VOLUME ["/srv/run", "/srv/uploads"]

FROM deployment-base AS aio

ARG APP_USER_UID
ARG APP_USER_GID
ARG IMAGE_ID

ENV DJANGO_SETTINGS_MODULE=hotpocket_backend.settings.aio
ENV HOTPOCKET_BACKEND_APP_USER_UID=${APP_USER_UID}
ENV HOTPOCKET_BACKEND_APP_USER_GID=${APP_USER_GID}
ENV HOTPOCKET_BACKEND_ENV=aio
ENV HOTPOCKET_BACKEND_APP=webapp
ENV HOTPOCKET_BACKEND_DEBUG=false
ENV HOTPOCKET_BACKEND_DATABASE_ENGINE=django.db.backends.sqlite3
ENV HOTPOCKET_BACKEND_DATABASE_NAME=/srv/run/hotpocket-backend-aio.sqlite
ENV HOTPOCKET_BACKEND_DATABASE_USER=
ENV HOTPOCKET_BACKEND_DATABASE_PASSWORD=
ENV HOTPOCKET_BACKEND_DATABASE_HOST=
ENV HOTPOCKET_BACKEND_DATABASE_PORT=
ENV HOTPOCKET_BACKEND_CELERY_IGNORE_RESULT=true
ENV HOTPOCKET_BACKEND_CELERY_ALWAYS_EAGER=true
ENV HOTPOCKET_BACKEND_GUNICORN_WORKERS=2
ENV HOTPOCKET_BACKEND_RUN_MIGRATIONS=true
ENV HOTPOCKET_BACKEND_CREATE_INITIAL_ACCOUNT=true
ENV HOTPOCKET_BACKEND_UPLOADS_PATH=/srv/run/uploads

VOLUME ["/srv/run"]

FROM development AS ci

COPY --chown=$APP_USER_UID:$APP_USER_GID backend/ /srv/app/
COPY --chown=$APP_USER_UID:$APP_USER_GID packages/ /srv/packages/
COPY --chown=$APP_USER_UID:$APP_USER_GID tls/ /srv/tls/

RUN ln -s /srv/app/ops/docker/settings /srv/app/hotpocket_backend/settings/docker && \
    ln -s /srv/app/ops/docker/secrets /srv/app/hotpocket_backend/secrets/docker
