VERSION 0.8

go-base:
    FROM golang:1.24.11-bookworm
    RUN apt-get update && apt-get install -y git curl && rm -rf /var/lib/apt/lists/*
    RUN curl -sSfL https://raw.githubusercontent.com/air-verse/air/master/install.sh | sh -s -- -b $(go env GOPATH)/bin
    RUN go install github.com/pressly/goose/v3/cmd/goose@v3.26.0
    RUN go install github.com/sqlc-dev/sqlc/cmd/sqlc@v1.30.0
    WORKDIR /app
    COPY go.mod go.sum ./

deps:
    FROM +go-base
    RUN go mod download

dev-image:
    ARG IMAGE_NAME="boost-ai/demo-backend-dev"
    ARG TAG="latest"

    FROM +deps
    COPY . .

    ENV GO_ENV=development
    ENV BACKEND_INTERNAL_PORT=8081
    ENV DATABASE_URL=postgres://boostai:boostai_dev_password@postgres-dev:5432/boostai?sslmode=disable
    EXPOSE 8081

    SAVE IMAGE $IMAGE_NAME:$TAG

dev-image-push:
    ARG REGISTRY="registry.mangopig.tech"
    ARG IMAGE_NAME="boost-ai/demo-backend-dev"
    ARG TAG="latest"

    FROM +dev-image --IMAGE_NAME=$IMAGE_NAME --TAG=$TAG
    SAVE IMAGE --push $REGISTRY/$IMAGE_NAME:$TAG

prod-bin:
    FROM +deps
    COPY . .
    RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /out/boostai-server ./cmd/server
    SAVE ARTIFACT /out/boostai-server AS LOCAL ./tmp/prod-a/boostai-server

prod-image:
    ARG IMAGE_NAME="boost-ai/demo-backend-prod-a"
    ARG TAG="latest"

    FROM alpine:3.22
    RUN apk add --no-cache ca-certificates
    WORKDIR /app
    COPY +prod-bin/boostai-server /app/boostai-server

    ENV GO_ENV=production
    ENV BACKEND_INTERNAL_PORT=8081
    EXPOSE 8081

    ENTRYPOINT ["/app/boostai-server"]

    SAVE IMAGE $IMAGE_NAME:$TAG

prod-image-push:
    ARG REGISTRY="registry.mangopig.tech"
    ARG IMAGE_NAME="boost-ai/demo-backend-prod-a"
    ARG TAG="latest"

    FROM +prod-image --IMAGE_NAME=$IMAGE_NAME --TAG=$TAG
    SAVE IMAGE --push $REGISTRY/$IMAGE_NAME:$TAG
