# Multi-stage build for optimized image size

# Stage 1: Builder
FROM golang:1.24.5-alpine AS builder

WORKDIR /build

# Install build dependencies
RUN apk add --no-cache git

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

# Copy source code
COPY . .

# Build binary with optimizations (disable VCS stamping in Docker builds)
RUN CGO_ENABLED=0 GOOS=linux go build -buildvcs=false -ldflags="-w -s" -o seo-optimizer ./cmd/optimizer

# Stage 3: Final image
FROM ubuntu:22.04

# Install runtime deps for avifenc + HTTPS/tzdata
RUN apt-get update && \
    DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
      ca-certificates tzdata wget libavif-bin && \
    rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Copy binary from builder
COPY --from=builder /build/seo-optimizer .
COPY --from=builder /build/configs ./configs

# Create non-root user
RUN useradd -m -u 1000 appuser

COPY docker/entrypoint.sh /usr/local/bin/entrypoint.sh

# Expose API port
EXPOSE 8080

# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=10s --retries=3 \
    CMD wget --quiet --tries=1 --spider http://localhost:8080/api/v1/health || exit 1

# Run application
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
