# syntax=docker/dockerfile:1.7

FROM debian:bookworm-slim@sha256:88200866dfff7ea7f5cbcb6ec7c8a701889efe6fe859fe64d6990e4b07ea4171 AS openrgb-builder
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y --no-install-recommends \
      build-essential \
      git \
      libhidapi-dev \
      libmbedtls-dev \
      libusb-1.0-0-dev \
      pkgconf \
      qt5-qmake \
      qtbase5-dev \
      qtbase5-dev-tools \
      qtchooser \
      qttools5-dev-tools \
    && rm -rf /var/lib/apt/lists/*
WORKDIR /src/openrgb
COPY *.cpp *.h OpenRGB.pro ./
COPY AutoStart/ AutoStart/
COPY Controllers/ Controllers/
COPY dependencies/ dependencies/
COPY dmiinfo/ dmiinfo/
COPY hidapi_wrapper/ hidapi_wrapper/
COPY i2c_smbus/ i2c_smbus/
COPY i2c_tools/ i2c_tools/
COPY interop/ interop/
COPY KeyboardLayoutManager/ KeyboardLayoutManager/
COPY mac/ mac/
COPY net_port/ net_port/
COPY pci_ids/ pci_ids/
COPY qt/ qt/
COPY RGBController/ RGBController/
COPY scripts/ scripts/
COPY scsiapi/ scsiapi/
COPY serial_port/ serial_port/
COPY SPDAccessor/ SPDAccessor/
COPY startup/ startup/
COPY super_io/ super_io/
COPY SuspendResume/ SuspendResume/
COPY wmi/ wmi/
RUN mkdir -p build \
    && cd build \
    && qmake ../OpenRGB.pro CONFIG+=release \
    && make -j"$(nproc)" \
    && test -x ./openrgb \
    && install -D -m 0755 ./openrgb /out/usr/local/bin/openrgb \
    && if [ -f ./60-openrgb.rules ]; then install -D -m 0644 ./60-openrgb.rules /out/usr/lib/udev/rules.d/60-openrgb.rules; fi

FROM node:22-bookworm-slim@sha256:83f487e0a63425e5b4d146fb5e5be574bcbe1b7b843d3ebafdd95eaf7767a7e5 AS frontend-builder
WORKDIR /src/frontend
COPY lumaops/frontend/package.json lumaops/frontend/package-lock.json ./
RUN npm ci --ignore-scripts
COPY lumaops/frontend/ ./
RUN npm run build

FROM python:3.12-slim-bookworm@sha256:0f5b26b9518d002b6173fd61daad821fa340635ebfec5bba471013f9ca114579 AS backend-builder
ENV VIRTUAL_ENV=/opt/venv
RUN python -m venv "$VIRTUAL_ENV"
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
WORKDIR /src/backend
COPY lumaops/backend/ ./
RUN pip install --no-cache-dir --upgrade "pip==26.1.2" \
    && pip install --no-cache-dir .

FROM python:3.12-slim-bookworm@sha256:0f5b26b9518d002b6173fd61daad821fa340635ebfec5bba471013f9ca114579 AS runtime
ARG DEBIAN_FRONTEND=noninteractive
ARG LUMAOPS_VERSION=0.1.0
LABEL org.opencontainers.image.title="LumaOps" \
      org.opencontainers.image.version="$LUMAOPS_VERSION" \
      org.opencontainers.image.description="Local-first OpenRGB control plane" \
      org.opencontainers.image.licenses="GPL-2.0-or-later"
RUN apt-get update && apt-get install -y --no-install-recommends \
      ca-certificates \
      gosu \
      libdbus-1-3 \
      libgl1 \
      libhidapi-hidraw0 \
      libhidapi-libusb0 \
      libmbedcrypto7 \
      libmbedtls14 \
      libmbedx509-1 \
      libqt5core5a \
      libqt5dbus5 \
      libqt5gui5 \
      libqt5network5 \
      libqt5widgets5 \
      libusb-1.0-0 \
      tini \
    && rm -rf /var/lib/apt/lists/* \
    && groupadd --gid 1000 lumaops \
    && useradd --uid 1000 --gid 1000 --home-dir /config/lumaops --shell /usr/sbin/nologin lumaops

COPY --from=openrgb-builder /out/ /
COPY --from=backend-builder /opt/venv /opt/venv
COPY --from=frontend-builder /src/frontend/dist /opt/lumaops/static
COPY docker/supervisor.py docker/healthcheck.py /opt/lumaops/
COPY docker/entrypoint.sh /usr/local/bin/lumaops-entrypoint
RUN chmod 0755 /usr/local/bin/lumaops-entrypoint /opt/lumaops/supervisor.py /opt/lumaops/healthcheck.py

ENV PATH="/opt/venv/bin:$PATH" \
    PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    LUMAOPS_ENV=production \
    APP_HOST=0.0.0.0 \
    APP_PORT=8080 \
    LOG_LEVEL=INFO \
    TZ=Europe/Brussels \
    OPENRGB_HOST=127.0.0.1 \
    OPENRGB_PORT=6742 \
    OPENRGB_CONFIG_DIR=/config/openrgb \
    DATABASE_URL=sqlite:////data/lumaops.db \
    CONFIG_DIR=/config/lumaops \
    DATA_DIR=/data \
    LOGS_DIR=/logs \
    STATIC_DIR=/opt/lumaops/static \
    ENABLE_NETWORK_DISCOVERY=true \
    ENABLE_HOME_ASSISTANT=false \
    ENABLE_WLED=false \
    PUID=99 \
    PGID=100

WORKDIR /opt/lumaops
VOLUME ["/config", "/data", "/logs"]
EXPOSE 8080
ENTRYPOINT ["/usr/local/bin/lumaops-entrypoint"]
HEALTHCHECK --interval=30s --timeout=8s --start-period=90s --retries=3 \
  CMD ["/opt/venv/bin/python", "/opt/lumaops/healthcheck.py"]
