FROM golang:1.26-alpine AS builder
WORKDIR /workspace

COPY go.work go.work.sum ./

# Manifests only, copied first, so `go mod download` gets its own cache layer
# invalidated only by dependency changes - not by every source edit below.
COPY gen/go.mod gen/go.sum ./gen/
COPY ha-gateway/go.mod ha-gateway/go.sum ./ha-gateway/
COPY discord-bot/go.mod discord-bot/go.sum ./discord-bot/
COPY tts-gateway/go.mod tts-gateway/go.sum ./tts-gateway/
COPY ai-gateway/go.mod ai-gateway/go.sum ./ai-gateway/

WORKDIR /workspace/ai-gateway
RUN go mod download

WORKDIR /workspace
COPY gen/ ./gen/
COPY ha-gateway/ ./ha-gateway/
COPY discord-bot/ ./discord-bot/
COPY tts-gateway/ ./tts-gateway/
COPY ai-gateway/ ./ai-gateway/

WORKDIR /workspace/ai-gateway
ARG VERSION=dev
RUN CGO_ENABLED=0 GOOS=linux go build \
  -ldflags="-s -w -X main.version=${VERSION}" \
  -o /ai-gateway ./cmd/gateway

FROM gcr.io/distroless/static:nonroot
COPY --from=builder /ai-gateway /ai-gateway
EXPOSE 50052
ENTRYPOINT ["/ai-gateway"]
