# =========== 1) Build stage ===========
FROM golang:1.25-alpine AS builder

# Install necessary packages for CGO and SQLite
RUN apk add --no-cache gcc musl-dev sqlite-dev

WORKDIR /app

# Copy go.mod and go.sum first
COPY go.mod go.sum ./
RUN go mod download

# Copy the rest of the backend code
COPY . .

# Build the Go binary
RUN go build -v -o server cmd/main.go

# =========== 2) Runtime stage ===========
FROM alpine:3.23

# Install Chromium for PDF rendering
RUN apk add --no-cache \
    chromium \
    chromium-chromedriver \
    nss \
    freetype \
    harfbuzz \
    ca-certificates \
    ttf-freefont

# Set Chrome path for chromedp
ENV CHROME_BIN=/usr/bin/chromium-browser \
    CHROME_PATH=/usr/bin/chromium-browser

WORKDIR /app

# Copy the compiled binary from builder stage
COPY --from=builder /app/server /app
COPY --from=builder /app/templates /app/default_templates

# Expose port
EXPOSE 8180

# Run the Go server
CMD ["./server"]
