# Build stage
FROM golang:1.22-alpine AS builder

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

WORKDIR /build

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

# Copy the entire monorepo
COPY . .

# Build the scraper-google service
WORKDIR /build/services/scraper-google
RUN CGO_ENABLED=0 GOOS=linux go build -o scraper-google .

# Runtime stage - Use Debian with Chrome installed
FROM debian:bookworm-slim

# Install Chrome and dependencies
RUN apt-get update && apt-get install -y \
    ca-certificates \
    chromium \
    chromium-sandbox \
    fonts-liberation \
    libasound2 \
    libatk-bridge2.0-0 \
    libatk1.0-0 \
    libatspi2.0-0 \
    libcups2 \
    libdbus-1-3 \
    libdrm2 \
    libgbm1 \
    libgtk-3-0 \
    libnspr4 \
    libnss3 \
    libxcomposite1 \
    libxdamage1 \
    libxfixes3 \
    libxkbcommon0 \
    libxrandr2 \
    xdg-utils \
    && rm -rf /var/lib/apt/lists/*

# Set Chrome path for chromedp
ENV CHROME_PATH=/usr/bin/chromium

WORKDIR /app

# Copy the binary from builder
COPY --from=builder /build/services/scraper-google/scraper-google .

# Run the scraper
ENTRYPOINT ["./scraper-google"]
