package server

import (
	"log/slog"
	"net/http"
	"time"

	"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/handler"
	"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/web"
)

func (s *Server) RegisterRoutes() http.Handler {
	mux := http.NewServeMux()

	fileServer := http.FileServer(http.FS(web.GetStaticFS()))
	mux.Handle("/static/", http.StripPrefix("/static/", fileServer))

	mux.Handle("/", handler.Home())

	return s.loggingMiddleware(mux)
}

func (s *Server) loggingMiddleware(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		start := time.Now()
		next.ServeHTTP(w, r)
		slog.Info("request",
			"method", r.Method,
			"path", r.URL.Path,
			"duration", time.Since(start),
		)
	})
}
