FROM node:20-bullseye AS frontend-builder

WORKDIR /build/frontend
COPY maubot/management/frontend/package.json maubot/management/frontend/yarn.lock ./
RUN corepack enable && yarn install --frozen-lockfile
COPY maubot/management/frontend/ ./
RUN yarn build

FROM cloudron/base:5.0.0

ENV APP_DIR=/app/code \
    DATA_DIR=/app/data \
    VENV_DIR=/app/code/venv \
    PYTHONUNBUFFERED=1 \
    DEBIAN_FRONTEND=noninteractive

RUN apt-get update && apt-get install -y --no-install-recommends \
        python3 python3-dev python3-venv python3-pip python3-wheel \
        build-essential git \
        libffi-dev libssl-dev libjpeg-dev zlib1g-dev libpq-dev \
        libmagic1 libmagic-dev libxml2-dev libxslt1-dev \
        libolm3 libolm-dev \
        libldap2-dev libsasl2-dev \
        nginx supervisor gosu ca-certificates curl \
    && rm -rf /var/lib/apt/lists/*

RUN mkdir -p ${APP_DIR} ${DATA_DIR} /run/nginx /tmp/data && chown cloudron:cloudron ${DATA_DIR}

WORKDIR ${APP_DIR}
COPY . ${APP_DIR}

RUN python3 -m venv ${VENV_DIR} \
    && ${VENV_DIR}/bin/pip install --upgrade pip setuptools wheel \
    && ${VENV_DIR}/bin/pip install --no-cache-dir \
        -r requirements.txt \
        -r optional-requirements.txt \
        dateparser \
        langdetect \
        python-gitlab \
        pyquery \
        tzlocal \
        pillow \
        python-magic \
        feedparser \
        python-dateutil \
        lxml \
        semver

COPY --from=frontend-builder /build/frontend/build /app/code/frontend

RUN cp /app/code/cloudron/default-config.yaml /tmp/data/config.yaml \
    && rm -f /etc/nginx/sites-enabled/default \
    && chmod 755 /app/code/start.sh

ENV PATH=${VENV_DIR}/bin:$PATH

VOLUME ["/app/data"]

CMD ["/app/code/start.sh"]
