package html

import (
	"net/http"

	"github.com/rs/zerolog/log"
)

// Custom ResponseWriter to track Content-Type
type customResponseWriter struct {
	http.ResponseWriter
	contentType string
	wroteHeader bool
}

func (crw *customResponseWriter) WriteHeader(code int) {
	crw.wroteHeader = true
	crw.ResponseWriter.WriteHeader(code)
}

func (crw *customResponseWriter) Header() http.Header {
	return crw.ResponseWriter.Header()
}

func (crw *customResponseWriter) Write(b []byte) (int, error) {
	if !crw.wroteHeader {
		if crw.contentType == "" {
			crw.contentType = http.DetectContentType(b)
			crw.ResponseWriter.Header().Set("Content-Type", crw.contentType)
		}
		crw.WriteHeader(http.StatusOK)
	}
	return crw.ResponseWriter.Write(b)
}

// Respond with an error that is visible to the user
func RespondError(w http.ResponseWriter, m string, e error, s int) {
	log.Warn().Int("status", s).Err(e).Str("user message", m).Msg("Responding with an error")
	http.Error(w, m, s)
}
