# =============================================================================
# Stage 1: Build Rust tools
# =============================================================================
FROM rust:bookworm AS builder

# tree-sitter-cli needs libclang (rquickjs-sys -> bindgen)
RUN apt-get update && apt-get install -y --no-install-recommends \
    libclang-dev \
    && rm -rf /var/lib/apt/lists/*

RUN cargo install codedash tree-sitter-cli tokei

# Build tree-sitter grammars as shared libraries
RUN git clone --depth 1 https://github.com/tree-sitter/tree-sitter-rust /opt/grammars/tree-sitter-rust \
    && cd /opt/grammars/tree-sitter-rust && tree-sitter build -o /opt/ts-parsers/rust.so

RUN git clone --depth 1 https://github.com/tree-sitter/tree-sitter-typescript /opt/grammars/tree-sitter-typescript \
    && cd /opt/grammars/tree-sitter-typescript/typescript && tree-sitter build -o /opt/ts-parsers/typescript.so \
    && cd /opt/grammars/tree-sitter-typescript/tsx && tree-sitter build -o /opt/ts-parsers/tsx.so

# =============================================================================
# Stage 1b: Build MicroPython
# =============================================================================
FROM debian:bookworm-slim AS micropython-builder

RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    python3 \
    git \
    ca-certificates \
    libffi-dev \
    pkg-config \
    && rm -rf /var/lib/apt/lists/*

RUN git clone --depth 1 --branch v1.24.1 https://github.com/micropython/micropython.git /opt/micropython \
    && cd /opt/micropython \
    && make -C mpy-cross -j$(nproc) \
    && cd ports/unix \
    && make -j$(nproc) submodules \
    && make -j$(nproc) VARIANT=standard \
    && strip build-standard/micropython

# =============================================================================
# Stage 2: Runtime
# =============================================================================
FROM debian:bookworm-slim

# Rust tool binaries
COPY --from=builder /usr/local/cargo/bin/codedash /usr/local/bin/
COPY --from=builder /usr/local/cargo/bin/tree-sitter /usr/local/bin/
COPY --from=builder /usr/local/cargo/bin/tokei /usr/local/bin/

# Pre-built tree-sitter grammars (source for config discovery + .so for dlopen)
COPY --from=builder /opt/grammars /opt/grammars
COPY --from=builder /opt/ts-parsers /root/.cache/tree-sitter/lib/

# tree-sitter config: parser discovery directory
RUN mkdir -p /etc/tree-sitter && printf '{\n\
  "parser-directories": ["/opt/grammars"]\n\
}\n' > /etc/tree-sitter/config.json

ENV TREE_SITTER_DIR=/etc/tree-sitter

# System tools + Lua scripting
RUN apt-get update && apt-get install -y --no-install-recommends \
    ripgrep \
    jq \
    git \
    fd-find \
    bat \
    ca-certificates \
    lua5.4 \
    lua-cjson \
    && rm -rf /var/lib/apt/lists/* \
    && ln -s /usr/bin/batcat /usr/local/bin/bat \
    && ln -s /usr/bin/fdfind /usr/local/bin/fd

# MicroPython (built from source)
COPY --from=micropython-builder /opt/micropython/ports/unix/build-standard/micropython /usr/local/bin/

WORKDIR /workspace
