FROM golang:1.26-alpine AS build
WORKDIR /src

# Keep dependency resolution deterministic. Do NOT run `go mod tidy` before
# source files are present: it can remove required modules because no packages
# are visible yet.
COPY go.mod go.sum ./
RUN go mod download

COPY cmd ./cmd
COPY internal ./internal

# Fail early with a useful message if a repository/.dockerignore rule ever
# drops the application's internal data package from the build context.
RUN test -f /src/internal/data/store.go \
 && test -f /src/internal/data/schema.sql

RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/neuralhunt ./cmd/server \
 && CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/neuralhunt-client ./cmd/client

FROM alpine:3.24
RUN adduser -D -H app && mkdir -p /data /app && chown -R app:app /data /app
WORKDIR /app
COPY --from=build /out/neuralhunt /app/neuralhunt
COPY --from=build /out/neuralhunt-client /app/neuralhunt-client
USER app
ENV SQLITE_PATH=/data/neuralhunt.db \
    ARTIFACT_DIR=/data/artifacts
EXPOSE 8080
ENTRYPOINT ["/app/neuralhunt"]
