# syntax=docker/dockerfile:1
#
# Developer station: Neovim (LazyVim) with nvim-dap configured to connect
# to a codelldb DAP server running on the "remote" container.
#
# Builds the newest Neovim from source (Alpine uses musl, so prebuilt
# glibc binaries don't run here) and layers on LazyVim, Catppuccin, and
# the nvim-dap plugin stack.  codelldb itself lives on the remote container.
#
# Build (run from repository root):
#   docker build -t dap-debug-dev -f devstation/Dockerfile .
#
# Pin a specific Neovim release:
#   docker build --build-arg NVIM_REF=v0.12.3 -t dap-debug-dev -f devstation/Dockerfile .

###############################################################################
# Stage 1 - build the newest Neovim from source
###############################################################################
FROM alpine:latest AS nvim-builder

ARG NVIM_REF=stable

RUN apk add --no-cache \
  build-base cmake coreutils curl gettext-tiny-dev git \
  linux-headers ninja unzip \
  && git clone --depth 1 --branch "${NVIM_REF}" https://github.com/neovim/neovim.git /tmp/neovim \
  && make -C /tmp/neovim \
  CMAKE_BUILD_TYPE=Release \
  CMAKE_EXTRA_FLAGS="-DCMAKE_INSTALL_PREFIX=/opt/nvim" \
  && make -C /tmp/neovim install \
  && strip /opt/nvim/bin/nvim

###############################################################################
# Stage 2 - runtime image with LazyVim and DAP configuration
###############################################################################
FROM alpine:latest

ENV HOME=/home/dev \
  TERM=xterm-256color \
  PATH="/opt/nvim/bin:${PATH}"

RUN addgroup -g 1000 dev \
  && adduser -D -u 1000 -G dev -h /home/dev -s /bin/ash dev

# LazyVim runtime dependencies + C++ toolchain for reading/browsing code.
# clang-extra-tools provides clangd for LSP code navigation.
RUN apk add --no-cache \
  git curl ca-certificates \
  ripgrep fd fzf \
  build-base cmake ccmake \
  gdb strace \
  clang-extra-tools \
  bash unzip less \
  ncurses-terminfo \
  tree-sitter-cli

# Neovim built in stage 1.
COPY --from=nvim-builder /opt/nvim /opt/nvim

# C++ demo project — source for editing, browsing, and setting breakpoints.
COPY --chown=dev:dev common/project/ /project/

USER dev
WORKDIR /project

RUN mkdir -p build \
  && cd build \
  && cmake -DCMAKE_BUILD_TYPE=Debug .. \
  && make -j"$(nproc)" \
  && ln -sf /project/build/compile_commands.json /project/compile_commands.json

# ---------------------------------------------------------------------------
# LazyVim configuration
# ---------------------------------------------------------------------------

# LazyVim starter configuration (https://github.com/LazyVim/starter).
RUN git clone --depth 1 https://github.com/LazyVim/starter "${HOME}/.config/nvim" \
  && rm -rf "${HOME}/.config/nvim/.git"

# Plugin specs — theme, LSP, and DAP configuration.
# See devstation/nvim-config/lua/plugins/ for the source files.
COPY --chown=dev:dev devstation/nvim-config/lua/plugins/ "${HOME}/.config/nvim/lua/plugins/"

# Pre-install plugins so the editor is ready to use on the first launch.
RUN timeout 600 nvim --headless "+Lazy! sync" +qa || true

# Pre-compile LazyVim's treesitter parsers.
COPY --chown=dev:dev devstation/scripts/precompile-treesitter.lua /tmp/precompile-treesitter.lua
RUN timeout 900 nvim --headless -c "luafile /tmp/precompile-treesitter.lua" \
  && rm -f /tmp/precompile-treesitter.lua

CMD ["nvim"]
