# Build stage
FROM golang:1.25-alpine AS builder

WORKDIR /app

# Copy go mod files
COPY go.mod ./

# Download dependencies
RUN go mod download

# Copy source code
COPY . .

# Build binary
RUN CGO_ENABLED=0 GOOS=linux go build -o cv-server -ldflags="-s -w" .

# Runtime stage
FROM alpine:latest

# Install CA certificates for HTTPS
RUN apk --no-cache add ca-certificates

WORKDIR /root/

# Copy binary from builder
COPY --from=builder /app/cv-server .

# Copy application files
COPY templates templates/
COPY data data/
COPY static static/

# Expose port
EXPOSE 1999

# Set production environment
ENV GO_ENV=production
ENV PORT=1999

# Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
  CMD wget --no-verbose --tries=1 --spider http://localhost:1999/health || exit 1

# Run the binary
CMD ["./cv-server"]
