# v1.12 Track B — codecontext sidecar container.
#
# Multi-stage build: golang:1.24-alpine builder produces two binaries
# (codecontext from source + our HTTP shim), then a minimal alpine:3.20
# runtime holds both.
#
# No upstream Docker image exists for codecontext. We clone the repo
# directly because the module path declared in go.mod
# (github.com/nuthan-ms/codecontext) differs from the GitHub repo URL
# (github.com/nmakod/codecontext) — `go install` against the GitHub path
# wouldn't resolve. The tagged v3.2.1 source tree is the same either way.

FROM golang:1.24-alpine AS builder
WORKDIR /build

RUN apk add --no-cache git ca-certificates build-base

# Build codecontext from the boocode-ts fork (has .codecontextignore support).
# Source is staged into the build context by the pre-build step:
#   tar -czf codecontext/fork.tar.gz -C /opt/forks/codecontext .
# CGO is required: codecontext binds tree-sitter via cgo.
COPY fork.tar.gz /build/fork.tar.gz
RUN mkdir -p /build/codecontext && tar -xzf /build/fork.tar.gz -C /build/codecontext
WORKDIR /build/codecontext
RUN CGO_ENABLED=1 GOOS=linux go build -o /build/codecontext-bin ./cmd/codecontext

# Build the shim. Stdlib-only — no go.sum needed.
WORKDIR /build/shim
COPY go.mod ./
COPY shim.go ./
RUN CGO_ENABLED=0 GOOS=linux go build -o /build/shim-bin ./

# Runtime: alpine matches the build target so codecontext's cgo bindings
# resolve against the same musl libc.
FROM alpine:3.20
RUN apk add --no-cache ca-certificates
COPY --from=builder /build/codecontext-bin /usr/local/bin/codecontext
COPY --from=builder /build/shim-bin /usr/local/bin/shim

EXPOSE 8080
HEALTHCHECK --interval=30s --timeout=5s --start-period=30s \
  CMD wget -qO- http://localhost:8080/health || exit 1
ENTRYPOINT ["/usr/local/bin/shim"]
