# Multi-stage build for minimal image size
FROM golang:1.21-alpine AS builder

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

WORKDIR /app

# Copy go mod files
COPY go.mod go.sum* ./

# Download dependencies
RUN go mod download || true

# Copy source code
COPY . .

# Build all services
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o /bin/gateway ./cmd/gateway
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o /bin/product ./cmd/product
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o /bin/user ./cmd/user
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o /bin/cart ./cmd/cart
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o /bin/order ./cmd/order

# Final stage - create minimal images for each service
FROM alpine:latest

RUN apk --no-cache add ca-certificates

WORKDIR /root/

# This Dockerfile can build any service by passing --build-arg SERVICE=<service-name>
ARG SERVICE
COPY --from=builder /bin/${SERVICE} /app/service

EXPOSE 8080

CMD ["/app/service"]
