package domaintest

import (
	"time"

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

type PlayerConfig struct {
	ID             int
	Points         int
	NumVillages    int
	ServerKey      string
	TribeID        int
	OD             domain.OpponentsDefeated
	BestRank       int
	BestRankAt     time.Time
	MostPoints     int
	MostPointsAt   time.Time
	MostVillages   int
	MostVillagesAt time.Time
	LastActivityAt time.Time
	DeletedAt      time.Time
}

func NewPlayer(tb TestingTB, opts ...func(cfg *PlayerConfig)) domain.Player {
	tb.Helper()

	now := time.Now()

	cfg := &PlayerConfig{
		ID:             RandID(),
		ServerKey:      RandServerKey(),
		Points:         gofakeit.IntRange(1, 10000),
		NumVillages:    gofakeit.IntRange(1, 10000),
		TribeID:        RandID(),
		OD:             NewOpponentsDefeated(tb),
		BestRank:       gofakeit.IntRange(1, 10000),
		BestRankAt:     now,
		MostPoints:     gofakeit.IntRange(1, 10000),
		MostPointsAt:   now,
		MostVillages:   gofakeit.IntRange(1, 10000),
		MostVillagesAt: now,
		LastActivityAt: now,
		DeletedAt:      time.Time{},
	}

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

	p, err := domain.UnmarshalPlayerFromDatabase(
		cfg.ID,
		cfg.ServerKey,
		gofakeit.LetterN(50),
		cfg.NumVillages,
		cfg.Points,
		gofakeit.IntRange(1, 10000),
		cfg.TribeID,
		cfg.OD,
		gofakeit.URL(),
		cfg.BestRank,
		cfg.BestRankAt,
		cfg.MostPoints,
		cfg.MostPointsAt,
		cfg.MostVillages,
		cfg.MostVillagesAt,
		cfg.LastActivityAt,
		now,
		cfg.DeletedAt,
	)
	require.NoError(tb, err)

	return p
}
