# Multi-stage build: small final image, no toolchain inside.
FROM golang:1.23-alpine AS builder

WORKDIR /src

# Cache module downloads.
COPY go.mod go.sum* ./
RUN go mod download

# Copy source.
COPY . .

# Build a static-ish binary (sqlite is pure Go via modernc, so no CGO needed).
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
    go build -trimpath -ldflags "-s -w" -o /out/tg-apicreator-bot ./main.go

# --- runtime stage ---
FROM alpine:3.20

RUN apk add --no-cache ca-certificates tzdata && \
    addgroup -S app && adduser -S -G app app

WORKDIR /app

COPY --from=builder /out/tg-apicreator-bot /app/tg-apicreator-bot
COPY --from=builder /src/config.example.yaml /app/config.example.yaml

# Data volume holds the SQLite db, sessions and logs.
RUN mkdir -p /app/data && chown -R app:app /app
VOLUME ["/app/data"]

USER app

EXPOSE 8080

ENTRYPOINT ["/app/tg-apicreator-bot"]
CMD ["-config", "/app/config.yaml"]
