# Go MSSQL Driver Development Container
# The Go major.minor version here must match the 'go' directive in go.mod.
# GOTOOLCHAIN=auto lets Go download the exact patch version (e.g. 1.25.9)
# when the base image is slightly behind. Downloads are verified against
# sum.golang.org.
FROM mcr.microsoft.com/devcontainers/go:1.25-bookworm
ENV GOTOOLCHAIN=auto

# The base image sets GOPATH=/go owned by root. Override to a user-writable
# location so GOTOOLCHAIN=auto can download patch releases without sudo.
ENV GOPATH=/home/vscode/go

# Install additional OS packages
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
    && apt-get install -y curl libkrb5-dev gnupg2 \
    && apt-get clean -y && rm -rf /var/lib/apt/lists/*

# Install Microsoft ODBC driver and mssql-tools18 (legacy ODBC sqlcmd/bcp for compatibility testing)
RUN curl -fsSL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor -o /usr/share/keyrings/microsoft-prod.gpg \
    && curl -fsSL https://packages.microsoft.com/config/debian/12/prod.list | tee /etc/apt/sources.list.d/mssql-release.list \
    && apt-get update \
    && ACCEPT_EULA=Y apt-get install -y msodbcsql18 mssql-tools18 unixodbc-dev \
    && apt-get clean -y && rm -rf /var/lib/apt/lists/*
ENV PATH="/opt/mssql-tools18/bin:${PATH}"

# Install go-sqlcmd - the Go-based sqlcmd that uses go-mssqldb (dogfooding!)
# Pinned version for reproducibility; update periodically
ARG SQLCMD_VERSION=v1.8.2
RUN go install github.com/microsoft/go-sqlcmd/cmd/modern@${SQLCMD_VERSION} \
    && mv $(go env GOPATH)/bin/modern $(go env GOPATH)/bin/sqlcmd

# Install golangci-lint for code quality
# Download pre-built binary with SHA256 checksum verification (supply chain security)
# Supports both amd64 and arm64 architectures
ARG GOLANGCI_LINT_VERSION=1.64.8
ARG GOLANGCI_LINT_SHA256_AMD64=b6270687afb143d019f387c791cd2a6f1cb383be9b3124d241ca11bd3ce2e54e
ARG GOLANGCI_LINT_SHA256_ARM64=a6ab58ebcb1c48572622146cdaec2956f56871038a54ed1149f1386e287789a5
RUN ARCH=$(dpkg --print-architecture) \
    && if [ "$ARCH" = "amd64" ]; then \
         CHECKSUM="${GOLANGCI_LINT_SHA256_AMD64}"; \
       elif [ "$ARCH" = "arm64" ]; then \
         CHECKSUM="${GOLANGCI_LINT_SHA256_ARM64}"; \
       else \
         echo "Unsupported architecture: $ARCH" && exit 1; \
       fi \
    && curl -fsSLO "https://github.com/golangci/golangci-lint/releases/download/v${GOLANGCI_LINT_VERSION}/golangci-lint-${GOLANGCI_LINT_VERSION}-linux-${ARCH}.tar.gz" \
    && echo "${CHECKSUM}  golangci-lint-${GOLANGCI_LINT_VERSION}-linux-${ARCH}.tar.gz" | sha256sum -c - \
    && tar -xzf "golangci-lint-${GOLANGCI_LINT_VERSION}-linux-${ARCH}.tar.gz" \
    && mv "golangci-lint-${GOLANGCI_LINT_VERSION}-linux-${ARCH}/golangci-lint" /usr/local/bin/ \
    && rm -rf "golangci-lint-${GOLANGCI_LINT_VERSION}-linux-${ARCH}" "golangci-lint-${GOLANGCI_LINT_VERSION}-linux-${ARCH}.tar.gz" \
    && golangci-lint --version

# Install additional Go tools (pinned versions for reproducibility)
# These versions must be compatible with the Go version in the base image.
RUN go install golang.org/x/tools/gopls@v0.21.1 \
    && go install github.com/go-delve/delve/cmd/dlv@v1.26.1 \
    && go install honnef.co/go/tools/cmd/staticcheck@v0.7.0

# Fix ownership: go install ran as root and populated $GOPATH with root-owned
# files. chown the entire tree so vscode can write (GOTOOLCHAIN=auto downloads).
RUN mkdir -p /home/vscode/bin \
    && chown -R vscode:vscode /home/vscode/go /home/vscode/bin

# PATH priority: ~/bin (convenience scripts like sql, sql-odbc, test-db) and
# GOPATH/bin (go-sqlcmd) come before mssql-tools18 so the modern go-sqlcmd is
# the default 'sqlcmd'. The legacy ODBC sqlcmd remains available at its full
# path (/opt/mssql-tools18/bin/sqlcmd) and via the 'sql-odbc' wrapper.
ENV PATH="/home/vscode/go/bin:/home/vscode/bin:${PATH}"
