# syntax=docker/dockerfile:1

# Build stage
FROM golang:1.22-alpine AS build

WORKDIR /src

# Dependências do build
RUN apk add --no-cache git ca-certificates

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

COPY . .

# Build do WASM do widget (regras de negócio no cliente)
# Importante: a compilação do WASM exige GOOS=js/GOARCH=wasm.
RUN GOOS=js GOARCH=wasm go build -trimpath -ldflags="-s -w" -o web/static/e-li.nps.wasm ./cmd/widgetwasm

# Copia o runtime JS do Go para WASM (Go class, polyfills)
RUN cp "$(go env GOROOT)/lib/wasm/wasm_exec.js" web/static/wasm_exec.js

# Build do binário
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
  go build -trimpath -ldflags="-s -w" -o /out/server ./cmd/server


# Runtime stage
FROM alpine:3.20

RUN apk add --no-cache ca-certificates tzdata

WORKDIR /app

# Binário
COPY --from=build /out/server /app/server

# Entry point (exige /app/.env montado via volume)
COPY docker-entrypoint.sh /app/docker-entrypoint.sh
RUN chmod +x /app/docker-entrypoint.sh

# Assets/templates (o servidor lê do filesystem)
COPY web/ /app/web/
COPY README.md /app/README.md

# Variáveis default
ENV ADDR=":8080"

EXPOSE 8080

ENTRYPOINT ["/app/docker-entrypoint.sh"]
