FROM golang:1.26-alpine AS builder

WORKDIR /src/backend

COPY backend/go.mod backend/go.sum ./
RUN go mod download

COPY backend/ ./

ARG APP_VERSION=dev
RUN CGO_ENABLED=0 GOOS=linux go build \
  -trimpath \
  -ldflags="-s -w -X main.version=${APP_VERSION}" \
  -o /out/warpbox \
  ./cmd/warpbox

FROM alpine:3.22

ARG APP_VERSION=dev

RUN apk add --no-cache ca-certificates ffmpeg wget

ENV WARPBOX_ADDR=:8080 \
    WARPBOX_DATA_DIR=/data \
    WARPBOX_STATIC_DIR=/app/static \
    WARPBOX_TEMPLATE_DIR=/app/templates \
    APP_VERSION=${APP_VERSION}

WORKDIR /app

COPY --from=builder /out/warpbox /usr/local/bin/warpbox
COPY backend/static /app/static
COPY backend/templates /app/templates

RUN mkdir -p /data

EXPOSE 8080
VOLUME ["/data"]

HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
  CMD wget -qO- http://127.0.0.1:8080/health >/dev/null || exit 1

ENTRYPOINT ["warpbox"]
