# ==========================================
# Chat Switchboard - Backend Dockerfile
# ==========================================
# Standalone Go API server for cluster deployment.
# Build context is the repo root (not ./server).
# ==========================================

# Build stage
FROM golang:1.23-bookworm AS builder

ARG APP_VERSION=dev

WORKDIR /app

# Copy module files and download deps
COPY server/go.mod server/go.sum* ./
RUN go mod download

# Copy source and tidy (resolves any go.sum gaps)
COPY server/ .
RUN go mod tidy

# Copy VERSION from repo root — used by ldflags and copied to runtime image.
# If APP_VERSION build-arg was supplied by CI, it takes precedence via ldflags.
# If not, $(cat VERSION) reads the file directly so the binary always has a real version.
COPY VERSION ./
RUN CGO_ENABLED=0 go build -ldflags="-s -w -X main.Version=$(cat VERSION)" -o /bin/switchboard .

# Runtime stage
FROM debian:bookworm-slim

RUN apt-get update && apt-get install -y --no-install-recommends \
    ca-certificates \
    && rm -rf /var/lib/apt/lists/*

COPY --from=builder /bin/switchboard /usr/local/bin/switchboard
COPY --from=builder /app/database/migrations /app/database/migrations

# VERSION file at /VERSION — version.go init() reads it as fallback for local/dev runs
COPY --from=builder /app/VERSION /VERSION

# Builtin extensions (seeded into DB on startup)
COPY extensions/builtin/ /app/extensions/builtin/

WORKDIR /app

EXPOSE 8080

ENTRYPOINT ["switchboard"]
