FROM golang:1.23-bookworm AS build
WORKDIR /src
COPY go.mod ./
COPY cmd ./cmd
COPY internal ./internal
RUN CGO_ENABLED=0 go build -o /out/jarvis ./cmd/homehub

# Build whisper.cpp inside the image so Docker STT does not depend on a host binary.
FROM debian:bookworm AS whisper-build
ARG WHISPER_CPP_REF=v1.9.1
RUN apt-get update && apt-get install -y --no-install-recommends \
    ca-certificates git cmake build-essential && rm -rf /var/lib/apt/lists/*
WORKDIR /src
RUN git clone --depth 1 --branch "${WHISPER_CPP_REF}" https://github.com/ggml-org/whisper.cpp.git . \
 && cmake -S . -B build \
      -DCMAKE_BUILD_TYPE=Release \
      -DWHISPER_BUILD_TESTS=OFF \
      -DWHISPER_BUILD_EXAMPLES=ON \
      -DBUILD_SHARED_LIBS=OFF \
 && cmake --build build --config Release --target whisper-cli -j2 \
 && cp build/bin/whisper-cli /out-whisper-cli

FROM debian:bookworm-slim
RUN apt-get update && apt-get install -y --no-install-recommends \
    ca-certificates ffmpeg poppler-utils tesseract-ocr tesseract-ocr-deu \
    libstdc++6 libgomp1 \
    && rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY --from=build /out/jarvis /app/jarvis
COPY --from=whisper-build /out-whisper-cli /usr/local/bin/whisper-cli
COPY scripts /app/scripts
COPY skills /app/skills
RUN chmod 0755 /usr/local/bin/whisper-cli /app/scripts/*.sh \
 && find /app/skills -maxdepth 2 -type f -name run -exec chmod 0755 {} + \
 && mkdir -p /app/data /app/skills /models /tools
EXPOSE 8080
CMD ["/app/jarvis"]
