# Multi-stage build for cross-platform support
FROM --platform=$BUILDPLATFORM golang:1.26.5-alpine@sha256:0178a641fbb4858c5f1b48e34bdaabe0350a330a1b1149aabd498d0699ff5fb2 AS builder

# Install git and ca-certificates (needed for go mod download)
RUN apk add --no-cache git ca-certificates

# Set working directory
WORKDIR /app

# Copy go mod files first for better caching
COPY go.mod go.sum ./

# Download dependencies
RUN go mod download

# Copy source code
COPY . .

# Build the binary with cross-compilation support
# The TARGETOS and TARGETARCH are automatically set by Docker Buildx for multi-platform builds
ARG TARGETOS
ARG TARGETARCH
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} \
    go build -o audit-ng ./main.go

FROM ghcr.io/pingcap-qe/bases/pingcap-base:v1.12.0@sha256:a9c4c9922df8ff8581fdc0ccdf77fe0bb6646d7852e85bda3ef4c80bc368f861
WORKDIR /

# Copy the binary from builder stage
COPY --from=builder /app/audit-ng .

# Do not set USER here, it should use the same user as the TiDB container
# This is to ensure that the binary has the same permissions as the TiDB container
ENTRYPOINT ["/audit-ng"]
