FROM python:3.13-slim

ARG puid=1000
ARG pgid=1000
ARG debug=false
ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    LANG=C.UTF-8 \
    LC_ALL=C.UTF-8


RUN groupadd -g ${pgid} -o custom_user
RUN useradd -m -u ${puid} -g ${pgid} -o -s /bin/bash custom_user

#RUN apt update && apt install -y gcc graphviz graphviz-dev pkg-config libpq-dev g++ supervisor
RUN apt update && apt install -y curl inotify-tools

# install nodejs 20
#RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
#RUN apt update && apt install -y nodejs
RUN apt-get update && apt-get install -y \
    ca-certificates \
    curl \
    gnupg \
    inotify-tools \
    && rm -rf /var/lib/apt/lists/*
RUN mkdir -p /etc/apt/keyrings
RUN curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
RUN echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list
RUN apt-get update
RUN apt-get install nodejs -y

# install yarn
RUN npm install -g yarn

# setup node dependencies
WORKDIR /app/frontend
COPY ./frontend/package.json ./package.json
RUN yarn install

WORKDIR /app
RUN pip install --upgrade pip

COPY requirements*.txt ./
RUN if [ "$debug" = "true" ] ; then \
        pip install --no-cache-dir -r requirements-dev.txt ; \
    else \
        pip install --no-cache-dir -r requirements-prod.txt ; \
    fi


#COPY . .

RUN chown ${puid}:${pgid} /app -R

USER custom_user

RUN mkdir -p ~/.ipython/profile_default/
RUN echo "c.InteractiveShellApp.extensions = ['autoreload']\nc.InteractiveShellApp.exec_lines = ['%autoreload 2']" > ~/.ipython/profile_default/ipython_config.py

EXPOSE 8000

#HEALTHCHECK --interval=30s --timeout=3s \
#    CMD curl -f http://127.0.0.1:8000/health/ || exit 1

CMD bash -c "sleep 2 && python manage.py collectstatic --noinput && python manage.py migrate && uvicorn oxpanel.asgi:application --workers 5 --host 0.0.0.0 --port 8000 --lifespan off --loop asyncio --ws websockets"
