/*
	This logging middleware is based on

https://github.com/elithrar/admission-control/blob/v0.6.3/request_logger.go

		by Matt Silverlock licensed under the Apache-2.0 license.

	   I am using slog and added a couple of small modifications.
*/
package rest

import (
	"log/slog"
	"net/http"
	"runtime/debug"
	"time"
)

// responseWriter is a minimal wrapper for http.ResponseWriter that allows the
// written HTTP status code to be captured for logging.
type responseWriter struct {
	http.ResponseWriter
	status      int
	size        int
	wroteHeader bool
}

func wrapResponseWriter(w http.ResponseWriter) *responseWriter {
	return &responseWriter{ResponseWriter: w}
}

func (rw *responseWriter) Status() int {
	return rw.status
}

func (rw *responseWriter) Size() int {
	return rw.size
}

func (rw *responseWriter) WriteHeader(code int) {
	if rw.wroteHeader {
		return
	}

	rw.status = code
	rw.ResponseWriter.WriteHeader(code)
	rw.wroteHeader = true
}

func (rw *responseWriter) Write(data []byte) (int, error) {

	written, err := rw.ResponseWriter.Write(data)
	rw.size += written

	return written, err
}

// LoggingMiddleware logs the incoming HTTP request & its duration.
func LogHandler() func(http.Handler) http.Handler {
	return func(next http.Handler) http.Handler {
		fn := func(resp http.ResponseWriter, req *http.Request) {
			defer func() {
				if err := recover(); err != nil {
					resp.WriteHeader(http.StatusInternalServerError)
					slog.Info(
						"internal server error",
						"err", err,
						"trace", string(debug.Stack()),
					)
				}
			}()

			start := time.Now()
			wrapped := wrapResponseWriter(resp)
			next.ServeHTTP(wrapped, req)

			header := wrapped.Header()["Content-Type"]
			contenttype := ""
			if header == nil {
				contenttype = "text/plain"
			} else {
				contenttype = header[0]
			}

			slog.Info("request",
				"ip", req.RemoteAddr,
				"status", wrapped.status,
				"method", req.Method,
				"path", req.URL.EscapedPath(),
				"size", wrapped.Size(),
				"content-type", contenttype,
				"duration", time.Since(start),
			)
		}

		return http.HandlerFunc(fn)
	}
}
