package domaintest

import (
	"time"

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

type EnnoblementCursorConfig struct {
	ID        int
	ServerKey string
	CreatedAt time.Time
}

func NewEnnoblementCursor(tb TestingTB, opts ...func(cfg *EnnoblementCursorConfig)) domain.EnnoblementCursor {
	tb.Helper()

	cfg := &EnnoblementCursorConfig{
		ID:        RandID(),
		ServerKey: RandServerKey(),
		CreatedAt: gofakeit.Date(),
	}

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

	ec, err := domain.NewEnnoblementCursor(
		cfg.ID,
		cfg.ServerKey,
		cfg.CreatedAt,
	)
	require.NoError(tb, err)

	return ec
}

type EnnoblementConfig struct {
	ID        int
	ServerKey string
	VillageID int
}

func NewEnnoblement(tb TestingTB, opts ...func(cfg *EnnoblementConfig)) domain.Ennoblement {
	tb.Helper()

	cfg := &EnnoblementConfig{
		ID:        RandID(),
		ServerKey: RandServerKey(),
		VillageID: RandID(),
	}

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

	e, err := domain.UnmarshalEnnoblementFromDatabase(
		cfg.ID,
		cfg.ServerKey,
		cfg.VillageID,
		RandID(),
		RandID(),
		RandID(),
		RandID(),
		gofakeit.IntRange(1, 10000),
		time.Now(),
	)
	require.NoError(tb, err)

	return e
}

type EnnoblementWithRelationsConfig struct {
	EnnoblementOptions []func(cfg *EnnoblementConfig)
	VillageOptions     []func(cfg *VillageConfig)
	NewOwnerOptions    []func(cfg *PlayerConfig)
	NewTribeOptions    []func(cfg *TribeConfig)
	OldOwnerOptions    []func(cfg *PlayerConfig)
	OldTribeOptions    []func(cfg *TribeConfig)
}

//nolint:gocyclo
func NewEnnoblementWithRelations(
	tb TestingTB,
	opts ...func(cfg *EnnoblementWithRelationsConfig),
) domain.EnnoblementWithRelations {
	tb.Helper()

	cfg := &EnnoblementWithRelationsConfig{}

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

	e := NewEnnoblement(tb, cfg.EnnoblementOptions...)

	if e.VillageID() > 0 {
		cfg.VillageOptions = append([]func(cfg *VillageConfig){
			func(cfg *VillageConfig) {
				cfg.ID = e.VillageID()
			},
		}, cfg.VillageOptions...)
	}

	if e.NewOwnerID() > 0 {
		cfg.NewOwnerOptions = append([]func(cfg *PlayerConfig){
			func(cfg *PlayerConfig) {
				cfg.ID = e.NewOwnerID()
			},
		}, cfg.NewOwnerOptions...)
	}

	if e.NewTribeID() > 0 {
		cfg.NewTribeOptions = append([]func(cfg *TribeConfig){
			func(cfg *TribeConfig) {
				cfg.ID = e.NewTribeID()
			},
		}, cfg.NewTribeOptions...)
	}

	if e.OldOwnerID() > 0 {
		cfg.OldOwnerOptions = append([]func(cfg *PlayerConfig){
			func(cfg *PlayerConfig) {
				cfg.ID = e.OldOwnerID()
			},
		}, cfg.OldOwnerOptions...)
	}

	if e.OldTribeID() > 0 {
		cfg.OldTribeOptions = append([]func(cfg *TribeConfig){
			func(cfg *TribeConfig) {
				cfg.ID = e.OldTribeID()
			},
		}, cfg.OldTribeOptions...)
	}

	var village domain.VillageMeta
	if len(cfg.VillageOptions) > 0 {
		village = NewVillage(tb, cfg.VillageOptions...).Meta()
	}

	var newOwner domain.PlayerMeta
	if len(cfg.NewOwnerOptions) > 0 {
		newOwner = NewPlayer(tb, cfg.NewOwnerOptions...).Meta()
	}

	var newTribe domain.TribeMeta
	if len(cfg.NewTribeOptions) > 0 {
		newTribe = NewTribe(tb, cfg.NewTribeOptions...).Meta()
	}

	var oldOwner domain.PlayerMeta
	if len(cfg.OldOwnerOptions) > 0 {
		oldOwner = NewPlayer(tb, cfg.OldOwnerOptions...).Meta()
	}

	var oldTribe domain.TribeMeta
	if len(cfg.OldTribeOptions) > 0 {
		oldTribe = NewTribe(tb, cfg.OldTribeOptions...).Meta()
	}

	return e.WithRelations(
		village,
		newOwner,
		domain.NullTribeMeta{
			V:     newTribe,
			Valid: !newTribe.IsZero(),
		},
		domain.NullPlayerMeta{
			V:     oldOwner,
			Valid: !oldOwner.IsZero(),
		},
		domain.NullTribeMeta{
			V:     oldTribe,
			Valid: !oldTribe.IsZero(),
		},
	)
}
