# Stage 1: Build the Vite/React frontend
FROM node:22-alpine AS frontend-builder

WORKDIR /build
COPY frontend/package.json ./
RUN npm install
COPY frontend/ ./

# Passed in via docker-compose build args so the host is baked into the bundle.
# e.g. VITE_WEBRTC_HOST=raspbot-v2.local
ARG VITE_WEBRTC_HOST
ENV VITE_WEBRTC_HOST=${VITE_WEBRTC_HOST}

RUN npm run build

# Stage 2: FastAPI + ROS 2 runtime
FROM ros:kilted-ros-core

ARG DEBIAN_FRONTEND=noninteractive

RUN apt-get update && apt-get install -y --no-install-recommends \
        python3-venv \
    && rm -rf /var/lib/apt/lists/*

COPY backend/requirements.txt /tmp/requirements.txt
RUN python3 -m venv /app/venv && \
    /app/venv/bin/pip install --no-cache-dir -r /tmp/requirements.txt

COPY --from=frontend-builder /build/dist /app/static
COPY backend/ /app/

WORKDIR /app

EXPOSE 8080

CMD ["/bin/bash", "-c", "source /opt/ros/$ROS_DISTRO/setup.bash && /app/venv/bin/python3 main.py"]
