# ==============================================================================
# Stage 1: Build React Frontend PWA
# ==============================================================================
FROM node:20-alpine AS frontend-builder
WORKDIR /app/web

COPY web/package*.json ./
RUN npm ci || npm install

COPY web/ ./
RUN npm run build

# ==============================================================================
# Stage 2: Build Go Backend Binary
# ==============================================================================
FROM golang:1.22-alpine AS backend-builder
WORKDIR /app

RUN apk add --no-cache git

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

COPY . .
COPY --from=frontend-builder /app/web/dist ./web/dist

RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o rsspipe cmd/server/main.go

# ==============================================================================
# Stage 3: Minimal Production Runtime
# ==============================================================================
FROM alpine:3.20

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

WORKDIR /app

COPY --from=backend-builder /app/rsspipe /app/rsspipe
COPY --from=frontend-builder /app/web/dist /app/web/dist

RUN mkdir -p /app/data

ENV PORT=8080 \
    DB_PATH=/app/data/rsspipe.db \
    GIN_MODE=release

VOLUME ["/app/data"]

EXPOSE 8080

ENTRYPOINT ["/app/rsspipe"]
