# Dockerfile — multi-stage build for orca
#
# Stage 1: build the static binary with golang:1.25
# Stage 2: distroless static runtime (CGO-free, ~2MB image)
#
# Build args:
#   VERSION     — semver tag injected via -ldflags (e.g. v0.4.4)
#   GIT_COMMIT  — short commit hash
#   BUILD_TIME  — ISO 8601 build timestamp
#
# Build:
#   docker build --build-arg VERSION=v0.4.4 -t git.cloudinit.dev/coreci/orca:v0.4.4 .
#
# Run:
#   docker run --rm git.cloudinit.dev/coreci/orca:v0.4.4 version
#   docker run --rm -v orca-data:/var/lib/orca git.cloudinit.dev/coreci/orca:v0.4.4 init

ARG VERSION=dev
ARG GIT_COMMIT=unknown
ARG BUILD_TIME=unknown

# --- Stage 1: build -------------------------------------------------------

FROM golang:1.25.12 AS builder

ARG VERSION
ARG GIT_COMMIT
ARG BUILD_TIME

WORKDIR /src

# Cache module downloads — copy go.mod/go.sum first, download, then copy source.
COPY go.mod go.sum ./
RUN go mod download

COPY . .

# CGO_ENABLED=0 guarantees a static binary (modernc/sqlite is pure Go).
RUN CGO_ENABLED=0 go build -trimpath \
      -ldflags="-s -w \
        -X git.cloudinit.dev/coreci/orca/internal/cli.version=${VERSION} \
        -X git.cloudinit.dev/coreci/orca/internal/cli.gitCommit=${GIT_COMMIT} \
        -X git.cloudinit.dev/coreci/orca/internal/cli.buildTime=${BUILD_TIME}" \
      -o /orca ./cmd/orca

# --- Stage 2: runtime -----------------------------------------------------

FROM gcr.io/distroless/static-debian12:nonroot

# ORCA_HOME points to a volume-mountable path inside the container.
# Mount a volume at /var/lib/orca to persist state across container restarts.
ENV ORCA_HOME=/var/lib/orca

COPY --from=builder /orca /orca

ENTRYPOINT ["/orca"]