package domaintest

import (
	"net/url"
	"time"

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

func RandServerKey() string {
	return gofakeit.LetterN(5)
}

type ServerConfig struct {
	Key         string
	VersionCode string
	URL         *url.URL
	Open        bool
	Special     bool
}

func NewServer(tb TestingTB, opts ...func(cfg *ServerConfig)) domain.Server {
	tb.Helper()

	cfg := &ServerConfig{
		Key:         RandServerKey(),
		VersionCode: RandVersionCode(),
		Open:        true,
		Special:     false,
	}

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

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

	s, err := domain.UnmarshalServerFromDatabase(
		cfg.Key,
		cfg.VersionCode,
		cfg.URL.String(),
		cfg.Open,
		cfg.Special,
		0,
		0,
		0,
		0,
		0,
		0,
		NewServerConfig(tb),
		NewBuildingInfo(tb),
		NewUnitInfo(tb),
		time.Now(),
		time.Time{},
		time.Time{},
		time.Time{},
		time.Time{},
		time.Time{},
		time.Time{},
	)
	require.NoError(tb, err)

	return s
}
