# Build stage: the server is pure Go (lib/pq is pure Go), so CGO is off and the
# result is a static binary that runs on a shell-less base image.
FROM golang:1.26-alpine AS build

WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY *.go ./

ARG TARGETOS
ARG TARGETARCH
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} \
    go build -trimpath -ldflags="-s -w" -o /prompt-sync .

# Runtime stage: distroless has no shell, so the binary does its own
# healthcheck (see -healthcheck in main.go). Runs as non-root.
FROM gcr.io/distroless/static-debian12:nonroot

COPY --from=build /prompt-sync /prompt-sync

ENV PROMPT_SYNC_ADDR=:8080
EXPOSE 8080

HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
    CMD ["/prompt-sync", "-healthcheck"]

ENTRYPOINT ["/prompt-sync"]
