FROM nvidia/cuda:12.2.0-runtime-ubuntu22.04 AS requirements
RUN apt-get update
RUN apt-get upgrade -y
RUN apt install -y python3-pip
ENV TOKENIZERS_PARALLELISM=false
COPY requirements.txt /requirements.txt
RUN pip3 install -r /requirements.txt
RUN rm -rf /requirements.txt

FROM requirements AS sia-test
COPY ./ /root/sia/
WORKDIR /root/sia/
RUN mkdir -p /root/models/current
CMD ["python3", "-m", "unittest", "discover", "-v", "-p", "*test.py"]

FROM node:20-alpine AS web-test
WORKDIR /app
COPY web/package*.json ./
RUN npm install
COPY web .
RUN npm test

FROM node:20-alpine AS web-build
WORKDIR /app
COPY web/package*.json ./
RUN npm install
COPY web .
RUN npm run build

FROM requirements
RUN apt-get update
RUN apt-get install -y wget gnupg
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list
RUN apt-get update
RUN apt-get install -y google-chrome-stable
RUN rm -rf /var/lib/apt/lists/*

RUN mkdir -p \
    /root/sia \
    /root/data/iterations \
    /root/data/user \
    /root/data/tasks \
    /root/data/environment \
    /root/models \
    /root/desktop

COPY ./tools/itb/requirements.txt /root/sia/tools/itb/requirements.txt
RUN cd /root/sia/tools/itb/ && python3 -m pip install -r requirements.txt

COPY ./tools/ /root/sia/tools/
RUN cd /root/sia/tools/itb/ && python3 -m pip install -e ".[dev]"

COPY --from=web-build /app/dist /root/static/
WORKDIR /root/desktop

CMD ["/root/sia/scripts/restart.sh"]