package pkg

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

type Option func(server *http.Server)

func WithBinding(binding string) Option {
	return func(server *http.Server) {
		server.Addr = binding
	}
}

func WithReadTimeout(readTimeout time.Duration) Option {
	return func(server *http.Server) {
		server.ReadTimeout = readTimeout
	}
}

func NewServer(opts ...Option) *http.Server {

	server := &http.Server{}
	for _, opt := range opts {
		opt(server)
	}

	return server

}

// ConfigKey ...
type ConfigKey string
type ConfigValue any
type ServerConfig map[ConfigKey]ConfigValue

const BindingConfigName = ConfigKey("binding")

func NewServerWithConfig(opts ServerConfig) *http.Server {

	server := &http.Server{}
	binding := opts[BindingConfigName]
	server.Addr = fmt.Sprintf("%s", binding)

	return server

}

type ServerBuilder struct {
	binding string
}

func NewServerBuilder() *ServerBuilder {
	return &ServerBuilder{}
}

func (b *ServerBuilder) AddBinding(binding string) *ServerBuilder {
	b.binding = binding
	return b
}

func (b *ServerBuilder) Build() *http.Server {
	server := &http.Server{}
	if b.binding != "" {
		server.Addr = b.binding
	} else {
		server.Addr = ":8080"
	}
	return server

}
