# Multi-stage build for BZZZ SLURP Context Distributor
FROM golang:1.21-alpine AS builder

# Install build dependencies
RUN apk add --no-cache git ca-certificates tzdata

# Set working directory
WORKDIR /build

# Copy go mod files
COPY go.mod go.sum ./
RUN go mod download

# Copy source code
COPY . .

# Build the application with optimizations
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
    -ldflags='-w -s -extldflags "-static"' \
    -a -installsuffix cgo \
    -o slurp-distributor \
    ./cmd/slurp-distributor

# Create minimal runtime image
FROM scratch

# Copy CA certificates and timezone data from builder
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo

# Copy the binary
COPY --from=builder /build/slurp-distributor /slurp-distributor

# Create non-root user directories
COPY --from=builder /etc/passwd /etc/passwd
COPY --from=builder /etc/group /etc/group

# Health check endpoint
HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \
    CMD ["/slurp-distributor", "health"]

# Expose ports
EXPOSE 8080 9090 11434

# Set entrypoint
ENTRYPOINT ["/slurp-distributor"]

# Labels for container metadata
LABEL maintainer="BZZZ Team"
LABEL version="1.0.0"
LABEL description="BZZZ SLURP Distributed Context System"
LABEL org.label-schema.schema-version="1.0"
LABEL org.label-schema.name="slurp-distributor"
LABEL org.label-schema.description="Enterprise-grade distributed context distribution system"
LABEL org.label-schema.url="https://github.com/anthonyrawlins/bzzz"
LABEL org.label-schema.vcs-url="https://github.com/anthonyrawlins/bzzz"
LABEL org.label-schema.build-date="2024-01-01T00:00:00Z"