# The image builds the page and the binary itself, so `docker build .` works
# from a clean checkout on any machine.
FROM oven/bun:alpine AS ui

WORKDIR /ui
COPY internal/ui/package.json internal/ui/bun.lock internal/ui/.npmrc ./
RUN bun install --frozen-lockfile
COPY internal/ui/index.html internal/ui/main.js ./
RUN bun run build

FROM golang:1.25-alpine AS builder

WORKDIR /app

COPY go.mod go.sum ./
RUN go mod download

COPY . .
# go:embed reads the built page from here at compile time.
COPY --from=ui /ui/dist internal/ui/dist

ARG VERSION=dev
ARG COMMIT=unknown
ARG BUILD_DATE=unknown

RUN CGO_ENABLED=0 GOOS=linux go build -trimpath \
    -ldflags "-s -w -buildid= -X embedder/internal.Version=$VERSION -X embedder/internal.Commit=$COMMIT -X embedder/internal.BuildDate=$BUILD_DATE -X embedder/internal.BuildMode=release" \
    -o /embedder .

FROM alpine:latest

# Alpine ships no CA bundle and a Go binary reads the system one, so without this
# every HTTPS call fails verifying the certificate.
RUN apk --no-cache add ca-certificates tzdata

WORKDIR /app
COPY --from=builder /embedder ./embedder

# The healthcheck, which Coolify runs with the wget busybox gives alpine.
EXPOSE 8080

ENTRYPOINT ["./embedder"]
