FROM golang:1.26.5-alpine3.24 AS build

WORKDIR /src
COPY . .
RUN go build -o /out/messenger ./cmd/messenger
RUN go build -o /out/lab3experiment ./cmd/lab3experiment
RUN go build -o /out/lab3summary ./cmd/lab3summary

FROM build AS test

CMD ["go", "test", "./internal/postgres", "./internal/session"]

FROM swaggerapi/swagger-ui:v5.31.2 AS swagger

COPY contracts/openapi.json /spec/openapi.json
ENV SWAGGER_JSON=/spec/openapi.json

FROM prom/prometheus:v3.13.3 AS prometheus

COPY monitoring/prometheus.yml /etc/prometheus/prometheus.yml

FROM grafana/grafana:13.1.6 AS grafana

COPY monitoring/grafana/provisioning /etc/grafana/provisioning
COPY monitoring/grafana/dashboards /etc/grafana/dashboards

FROM nginx:1.30.5-alpine AS loadbalancer

COPY nginx/nginx.conf /etc/nginx/nginx.conf

FROM alpine:3.24.2

RUN addgroup -S app && adduser -S -G app -u 10001 app
COPY --from=build --chown=app:app /out/messenger /app/messenger
COPY --from=build --chown=app:app /out/lab3experiment /app/lab3experiment
COPY --from=build --chown=app:app /out/lab3summary /app/lab3summary

USER app
WORKDIR /app
HEALTHCHECK --interval=10s --timeout=2s --start-period=5s --retries=3 \
    CMD wget -q -O /dev/null "http://127.0.0.1:${APP_PORT}/health/ready" || exit 1
CMD ["/app/messenger"]
