# Multi-stage build for AI Trading Application (Pure Go, no CGO)
FROM golang:1.26-alpine AS builder

WORKDIR /build

COPY . .

# Build the application (static binary, no CGO, migrations embedded)
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
    -trimpath \
    -ldflags="-w -s" \
    -o aitrade \
    ./cmd/aitrade

# Build healthcheck utility
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
    -trimpath \
    -ldflags="-w -s" \
    -o healthcheck \
    ./cmd/healthcheck

# Runtime stage - Google Distroless (minimal, secure, with CA certs and tzdata)
FROM gcr.io/distroless/static-debian12:nonroot

# Set working directory
WORKDIR /app

# Copy binary from builder (migrations are embedded in binary)
COPY --from=builder /build/aitrade /app/aitrade

# Copy healthcheck utility
COPY --from=builder /build/healthcheck /app/healthcheck

# Note: Distroless runs as nonroot user (UID 65532) by default
# Data directory needs to be mounted with correct permissions:
# mkdir -p data && chown 65532:65532 data

# Expose web port
EXPOSE 8080

# Health check using custom binary (lightweight, no curl/wget needed)
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
    CMD ["/app/healthcheck", "localhost", "8080"]

# Run application
ENTRYPOINT ["/app/aitrade"]
