# syntax=docker/dockerfile:1

# ===== STAGE 1: Build =====
FROM golang:1.24-alpine AS builder

WORKDIR /build

# Cache de dependências - só rebuild se go.mod/go.sum mudar
COPY go.mod go.sum ./
RUN --mount=type=cache,target=/go/pkg/mod \
    go mod download && go mod verify

# Copia código fonte
COPY . .

# Build otimizado com cache
RUN --mount=type=cache,target=/go/pkg/mod \
    --mount=type=cache,target=/root/.cache/go-build \
    CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
    go build -trimpath -ldflags="-s -w" \
    -o /app/server ./cmd/api

# ===== STAGE 2: Runtime (distroless - segurança + mínimo ~2MB) =====
FROM gcr.io/distroless/static-debian12:nonroot

# Binary
COPY --from=builder /app/server /server

EXPOSE 8214

ENTRYPOINT ["/server"]
