# ═══════════════════════════════════════════════════════════
# 🐍 OPHION Server - Dockerfile
# ═══════════════════════════════════════════════════════════

# Build stage
FROM golang:1.22-alpine AS builder

WORKDIR /build

# Install dependencies
RUN apk add --no-cache git ca-certificates

# Copy go modules first (cache layer)
COPY go.mod go.sum ./
RUN go mod download

# Copy source code
COPY cmd/ ./cmd/
COPY internal/ ./internal/

# Build the server binary
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
    go build -ldflags="-s -w" -o ophion-server ./cmd/server

# Runtime stage
FROM alpine:3.19

LABEL org.opencontainers.image.title="OPHION Server"
LABEL org.opencontainers.image.description="Observability Platform API Server"

# Install runtime dependencies
RUN apk add --no-cache ca-certificates tzdata wget

# Create non-root user
RUN addgroup -g 1000 ophion && \
    adduser -u 1000 -G ophion -s /bin/sh -D ophion

WORKDIR /app

# Copy binary from builder
COPY --from=builder /build/ophion-server /app/

# Create data directories
RUN mkdir -p /app/data /app/logs && \
    chown -R ophion:ophion /app

USER ophion

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

# Environment defaults
ENV TZ=America/Sao_Paulo \
    PORT=8080

EXPOSE 8080

ENTRYPOINT ["/app/ophion-server"]
