FROM ubuntu:25.10 AS builder

RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    cmake \
    pkg-config \
    libpqxx-dev \
    libpq-dev \
    libhiredis-dev \
    libsodium-dev \
    nlohmann-json3-dev \
 && rm -rf /var/lib/apt/lists/*

WORKDIR /src
COPY CMakeLists.txt ./
COPY src        ./src
COPY migrations ./migrations

RUN cmake -S . -B build -DCMAKE_BUILD_TYPE=Release \
 && cmake --build build -j

FROM ubuntu:25.10

RUN apt-get update && apt-get install -y --no-install-recommends \
    ca-certificates \
    curl \
    libpqxx-7.10 \
    libhiredis1.1.0 \
    libsodium23 \
 && rm -rf /var/lib/apt/lists/* \
 && useradd --system --no-create-home --uid 10001 app

WORKDIR /app
COPY --from=builder /src/build/messenger /app/messenger
COPY --from=builder /src/migrations    /app/migrations

RUN chown -R app:app /app

USER 10001

ENV APP_HOST=0.0.0.0
ENV APP_PORT=8080

EXPOSE 8080

HEALTHCHECK --interval=5s --timeout=3s --start-period=20s --retries=3 \
  CMD curl -fsS http://127.0.0.1:8080/health/live || exit 1

CMD ["/app/messenger"]