package domaintest

import (
	"time"

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

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

type TribeConfig struct {
	ID             int
	ServerKey      string
	Tag            string
	OD             domain.OpponentsDefeated
	BestRank       int
	BestRankAt     time.Time
	MostPoints     int
	MostPointsAt   time.Time
	MostVillages   int
	MostVillagesAt time.Time
	DeletedAt      time.Time
}

func NewTribe(tb TestingTB, opts ...func(cfg *TribeConfig)) domain.Tribe {
	tb.Helper()

	now := time.Now()

	cfg := &TribeConfig{
		ID:             RandID(),
		ServerKey:      RandServerKey(),
		Tag:            RandTribeTag(),
		OD:             NewOpponentsDefeated(tb),
		BestRank:       gofakeit.IntRange(1, 10000),
		BestRankAt:     now,
		MostPoints:     gofakeit.IntRange(1, 10000),
		MostPointsAt:   now,
		MostVillages:   gofakeit.IntRange(1, 10000),
		MostVillagesAt: now,
		DeletedAt:      time.Time{},
	}

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

	t, err := domain.UnmarshalTribeFromDatabase(
		cfg.ID,
		cfg.ServerKey,
		gofakeit.LetterN(50),
		cfg.Tag,
		gofakeit.IntRange(1, 10000),
		gofakeit.IntRange(1, 10000),
		gofakeit.IntRange(1, 10000),
		gofakeit.IntRange(1, 10000),
		gofakeit.IntRange(1, 10000),
		cfg.OD,
		gofakeit.URL(),
		gofakeit.Float64Range(0.1, 99.9),
		cfg.BestRank,
		cfg.BestRankAt,
		cfg.MostPoints,
		cfg.MostPointsAt,
		cfg.MostVillages,
		cfg.MostVillagesAt,
		now,
		cfg.DeletedAt,
	)
	require.NoError(tb, err)

	return t
}
