FROM ros:kilted
ARG USERNAME=USERNAME
ARG USER_UID=1000
ARG USER_GID=$USER_UID

# Delete user if it exists in container (e.g Ubuntu Noble: ubuntu)
RUN if id -u $USER_UID ; then userdel `id -un $USER_UID` ; fi

# Create the user
RUN groupadd --gid $USER_GID $USERNAME \
    && useradd --uid $USER_UID --gid $USER_GID -m $USERNAME -s /bin/bash \
    #
    # [Optional] Add sudo support. Omit if you don't need to install software after connecting.
    && apt-get update \
    && apt-get install -y sudo \
    && echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
    && chmod 0440 /etc/sudoers.d/$USERNAME
RUN apt-get update && apt-get upgrade -y

# C++ development tools
RUN apt-get update && apt-get install -y \
    build-essential \
    cmake \
    gdb \
    clang \
    clang-format \
    clang-tidy \
    libboost-all-dev

# Python development tools
RUN apt-get update && apt-get install -y \
    python3-pip \
    python3-venv \
    python3-dev \
    python3-argcomplete \
    python3-colcon-common-extensions \
    python3-colcon-mixin \
    python3-rosdep \
    python3-vcstool

# ROS 2 ament build tools
# RUN apt-get install -y \
#     ros-${ROS_DISTRO}-ament-cmake \
#     ros-${ROS_DISTRO}-ament-cmake-auto \
#     ros-${ROS_DISTRO}-ament-python \
#     ros-${ROS_DISTRO}-ament-lint-auto \
#     ros-${ROS_DISTRO}-ament-lint-common

# Node.js for frontend development
RUN curl -fsSL https://deb.nodesource.com/setup_22.x | bash - \
    && apt-get install -y nodejs

# Python venv for webui backend (isolated from system packages)
RUN python3 -m venv /opt/webui-venv \
    && /opt/webui-venv/bin/pip install --no-cache-dir \
        fastapi \
        "uvicorn[standard]" \
        numpy

# Docker CLI for accessing the host daemon via the mounted socket
# docker-compose-plugin is only in Docker's official repo, not Ubuntu's.
# .docker_gid is written by initializeCommand on the host before the build,
# ensuring the in-container docker group GID matches the host socket GID.
COPY .docker_gid /tmp/.docker_gid
RUN apt-get update && apt-get install -y ca-certificates curl gnupg \
    && install -m 0755 -d /etc/apt/keyrings \
    && curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg \
    && chmod a+r /etc/apt/keyrings/docker.gpg \
    && echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" \
       > /etc/apt/sources.list.d/docker.list \
    && apt-get update && apt-get install -y docker-ce-cli docker-compose-plugin \
    && groupadd -g "$(cat /tmp/.docker_gid)" docker \
    && usermod -aG docker $USERNAME

ENV SHELL /bin/bash

# ********************************************************
# * Anything else you want to do like clean up goes here *
# ********************************************************

# [Optional] Set the default user. Omit if you want to keep the default as root.
USER $USERNAME
CMD ["/bin/bash"]