package domaintest

import (
	"net/url"

	"gitea.dwysokinski.me/twhelp/corev3/internal/domain"
	"github.com/brianvoe/gofakeit/v6"
	"github.com/stretchr/testify/require"
)

type BaseServerConfig struct {
	Key  string
	URL  *url.URL
	Open bool
}

func NewBaseServer(tb TestingTB, opts ...func(cfg *BaseServerConfig)) domain.BaseServer {
	tb.Helper()

	cfg := &BaseServerConfig{
		Key:  RandServerKey(),
		Open: true,
	}

	for _, opt := range opts {
		opt(cfg)
	}

	if cfg.URL == nil {
		cfg.URL = &url.URL{
			Scheme: "https",
			Host:   cfg.Key + "." + gofakeit.DomainName(),
		}
	}

	s, err := domain.NewBaseServer(cfg.Key, cfg.URL, cfg.Open)
	require.NoError(tb, err)

	return s
}
