# Build stage
FROM golang:1.21 AS builder

WORKDIR /app

# Install build dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    sqlite3 \
    libsqlite3-dev \
    && rm -rf /var/lib/apt/lists/*

# Copy go mod files
COPY go.mod go.sum ./
RUN go mod download

# Copy source code
COPY . .

# Build binary with CGO enabled for SQLite
RUN CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o etl ./cmd/etl

# Runtime stage
FROM debian:bookworm-slim

WORKDIR /app

# Install runtime dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
    libsqlite3-0 \
    ca-certificates \
    && rm -rf /var/lib/apt/lists/*

# Copy binary from builder
COPY --from=builder /app/etl .

# Copy configs and testdata
COPY configs/ ./configs/
COPY testdata/ ./testdata/

# Create output directory
RUN mkdir -p output

# Run as non-root user
RUN useradd -m -u 1000 etluser && chown -R etluser:etluser /app
USER etluser

ENTRYPOINT ["./etl"]
CMD ["pipeline", "--config", "configs/pipeline.json", "--verbose"]
