package server

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

	"git.jmbit.de/jmb/scanfile/server/internal/middlewares"

	"github.com/spf13/viper"
)

func NewServer() *http.Server {
	port := viper.GetInt("web.port")
	host := viper.GetString("web.host")
	middlewareStack := middlewares.CreateStack()
	if viper.GetBool("debug.log.http") {
		slog.Info("Enabling HTTP logging")
		middlewareStack = middlewares.CreateStack(
			middlewares.Logging,
		)
	} else {
		middlewareStack = middlewares.CreateStack()
	}

	// Declare Server config
	server := &http.Server{
		Addr:         fmt.Sprintf("%s:%d", host, port),
		Handler:      middlewareStack(RegisterRoutes()),
		IdleTimeout:  time.Minute,
		ReadTimeout:  10 * time.Second,
		WriteTimeout: 30 * time.Second,
	}

	return server
}
