# =========================
# Builder stage
# =========================
FROM public.ecr.aws/docker/library/golang:1.23-alpine AS builder

RUN apk add --no-cache git ca-certificates tzdata
WORKDIR /app

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

COPY . .

# Build API binary
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
    go build -trimpath -ldflags="-s -w" -o lti-api ./cmd/api

# Build SEED binary
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
    go build -trimpath -ldflags="-s -w" -o lti-seed ./cmd/seed

# Build migrate CLI with postgres + file drivers
RUN GOBIN=/usr/local/bin go install -tags "postgres file" -ldflags="-s -w" github.com/golang-migrate/migrate/v4/cmd/migrate@v4.18.3

# =========================
# Runtime stage
# =========================
FROM public.ecr.aws/docker/library/alpine:3.20

RUN apk add --no-cache ca-certificates tzdata curl bash postgresql-client \
  && adduser -D -H -u 10001 appuser

WORKDIR /app

COPY --from=builder /app/lti-api /app/lti-api
COPY --from=builder /app/lti-seed /app/lti-seed
COPY --from=builder /usr/local/bin/migrate /app/migrate
COPY --from=builder /app/internal/database/migrations /app/migrations

USER appuser
EXPOSE 8081

CMD ["/app/lti-api"]
