package main

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

	"github.com/go-chi/chi/v5"
)

type httpServerConfig struct {
	port              uint
	readTimeout       time.Duration
	readHeaderTimeout time.Duration
	writeTimeout      time.Duration
	idleTimeout       time.Duration
}

func newHTTPServer(cfg httpServerConfig, registerHandlers func(r chi.Router) error) (*http.Server, error) {
	r := chi.NewRouter()

	if err := registerHandlers(r); err != nil {
		return nil, fmt.Errorf("couldn't register handlers: %w", err)
	}

	return &http.Server{
		Addr:              fmt.Sprintf(":%d", cfg.port),
		Handler:           r,
		ReadTimeout:       cfg.readTimeout,
		ReadHeaderTimeout: cfg.readHeaderTimeout,
		WriteTimeout:      cfg.writeTimeout,
		IdleTimeout:       cfg.idleTimeout,
	}, nil
}
