FROM public.ecr.aws/docker/library/golang:1.27-alpine AS builder

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

# Set the working directory inside the container
WORKDIR /app

# Copy go mod and sum files
COPY go.mod go.sum ./

# Download all dependencies
RUN go mod download

# Copy the source code into the container
COPY . .

# Build the application
ARG VERSION=dev
RUN NEWT_VERSION=$(go list -m -f '{{.Version}}' github.com/fosrl/newt | sed 's/^v//') && \
    CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w -X github.com/fosrl/cli/internal/version.Version=${VERSION} -X github.com/fosrl/cli/internal/version.NewtVersionOverride=${NEWT_VERSION}" -o /aileinsight-cli

FROM public.ecr.aws/docker/library/alpine:3.23 AS runner

RUN apk --no-cache add ca-certificates tzdata iputils

COPY --from=builder /aileinsight-cli /usr/local/bin/
COPY entrypoint.sh /

# Admin/metrics endpoint (Prometheus scrape)
EXPOSE 2112

RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
CMD ["aileinsight-cli"]