// Code generated by BobGen psql v0.42.5. DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.

package factory

import (
	"context"
	"testing"
	"time"

	"github.com/Gleipnir-Technology/bob"
	enums "github.com/Gleipnir-Technology/nidus-sync/db/enums"
	models "github.com/Gleipnir-Technology/nidus-sync/db/models"
	"github.com/aarondl/opt/omit"
	"github.com/jaswdr/faker/v2"
)

type PoolMod interface {
	Apply(context.Context, *PoolTemplate)
}

type PoolModFunc func(context.Context, *PoolTemplate)

func (f PoolModFunc) Apply(ctx context.Context, n *PoolTemplate) {
	f(ctx, n)
}

type PoolModSlice []PoolMod

func (mods PoolModSlice) Apply(ctx context.Context, n *PoolTemplate) {
	for _, f := range mods {
		f.Apply(ctx, n)
	}
}

// PoolTemplate is an object representing the database table.
// all columns are optional and should be set by mods
type PoolTemplate struct {
	Condition   func() enums.Poolconditiontype
	Created     func() time.Time
	CreatorID   func() int32
	ID          func() int32
	SiteID      func() int32
	SiteVersion func() int32

	r poolR
	f *Factory

	alreadyPersisted bool
}

type poolR struct {
	CreatorUser *poolRCreatorUserR
	Site        *poolRSiteR
}

type poolRCreatorUserR struct {
	o *UserTemplate
}
type poolRSiteR struct {
	o *SiteTemplate
}

// Apply mods to the PoolTemplate
func (o *PoolTemplate) Apply(ctx context.Context, mods ...PoolMod) {
	for _, mod := range mods {
		mod.Apply(ctx, o)
	}
}

// setModelRels creates and sets the relationships on *models.Pool
// according to the relationships in the template. Nothing is inserted into the db
func (t PoolTemplate) setModelRels(o *models.Pool) {
	if t.r.CreatorUser != nil {
		rel := t.r.CreatorUser.o.Build()
		rel.R.CreatorPools = append(rel.R.CreatorPools, o)
		o.CreatorID = rel.ID // h2
		o.R.CreatorUser = rel
	}

	if t.r.Site != nil {
		rel := t.r.Site.o.Build()
		rel.R.Pools = append(rel.R.Pools, o)
		o.SiteID = rel.ID           // h2
		o.SiteVersion = rel.Version // h2
		o.R.Site = rel
	}
}

// BuildSetter returns an *models.PoolSetter
// this does nothing with the relationship templates
func (o PoolTemplate) BuildSetter() *models.PoolSetter {
	m := &models.PoolSetter{}

	if o.Condition != nil {
		val := o.Condition()
		m.Condition = omit.From(val)
	}
	if o.Created != nil {
		val := o.Created()
		m.Created = omit.From(val)
	}
	if o.CreatorID != nil {
		val := o.CreatorID()
		m.CreatorID = omit.From(val)
	}
	if o.ID != nil {
		val := o.ID()
		m.ID = omit.From(val)
	}
	if o.SiteID != nil {
		val := o.SiteID()
		m.SiteID = omit.From(val)
	}
	if o.SiteVersion != nil {
		val := o.SiteVersion()
		m.SiteVersion = omit.From(val)
	}

	return m
}

// BuildManySetter returns an []*models.PoolSetter
// this does nothing with the relationship templates
func (o PoolTemplate) BuildManySetter(number int) []*models.PoolSetter {
	m := make([]*models.PoolSetter, number)

	for i := range m {
		m[i] = o.BuildSetter()
	}

	return m
}

// Build returns an *models.Pool
// Related objects are also created and placed in the .R field
// NOTE: Objects are not inserted into the database. Use PoolTemplate.Create
func (o PoolTemplate) Build() *models.Pool {
	m := &models.Pool{}

	if o.Condition != nil {
		m.Condition = o.Condition()
	}
	if o.Created != nil {
		m.Created = o.Created()
	}
	if o.CreatorID != nil {
		m.CreatorID = o.CreatorID()
	}
	if o.ID != nil {
		m.ID = o.ID()
	}
	if o.SiteID != nil {
		m.SiteID = o.SiteID()
	}
	if o.SiteVersion != nil {
		m.SiteVersion = o.SiteVersion()
	}

	o.setModelRels(m)

	return m
}

// BuildMany returns an models.PoolSlice
// Related objects are also created and placed in the .R field
// NOTE: Objects are not inserted into the database. Use PoolTemplate.CreateMany
func (o PoolTemplate) BuildMany(number int) models.PoolSlice {
	m := make(models.PoolSlice, number)

	for i := range m {
		m[i] = o.Build()
	}

	return m
}

func ensureCreatablePool(m *models.PoolSetter) {
	if !(m.Condition.IsValue()) {
		val := random_enums_Poolconditiontype(nil)
		m.Condition = omit.From(val)
	}
	if !(m.Created.IsValue()) {
		val := random_time_Time(nil)
		m.Created = omit.From(val)
	}
	if !(m.CreatorID.IsValue()) {
		val := random_int32(nil)
		m.CreatorID = omit.From(val)
	}
	if !(m.SiteID.IsValue()) {
		val := random_int32(nil)
		m.SiteID = omit.From(val)
	}
	if !(m.SiteVersion.IsValue()) {
		val := random_int32(nil)
		m.SiteVersion = omit.From(val)
	}
}

// insertOptRels creates and inserts any optional the relationships on *models.Pool
// according to the relationships in the template.
// any required relationship should have already exist on the model
func (o *PoolTemplate) insertOptRels(ctx context.Context, exec bob.Executor, m *models.Pool) error {
	var err error

	return err
}

// Create builds a pool and inserts it into the database
// Relations objects are also inserted and placed in the .R field
func (o *PoolTemplate) Create(ctx context.Context, exec bob.Executor) (*models.Pool, error) {
	var err error
	opt := o.BuildSetter()
	ensureCreatablePool(opt)

	if o.r.CreatorUser == nil {
		PoolMods.WithNewCreatorUser().Apply(ctx, o)
	}

	var rel0 *models.User

	if o.r.CreatorUser.o.alreadyPersisted {
		rel0 = o.r.CreatorUser.o.Build()
	} else {
		rel0, err = o.r.CreatorUser.o.Create(ctx, exec)
		if err != nil {
			return nil, err
		}
	}

	opt.CreatorID = omit.From(rel0.ID)

	if o.r.Site == nil {
		PoolMods.WithNewSite().Apply(ctx, o)
	}

	var rel1 *models.Site

	if o.r.Site.o.alreadyPersisted {
		rel1 = o.r.Site.o.Build()
	} else {
		rel1, err = o.r.Site.o.Create(ctx, exec)
		if err != nil {
			return nil, err
		}
	}

	opt.SiteID = omit.From(rel1.ID)
	opt.SiteVersion = omit.From(rel1.Version)

	m, err := models.Pools.Insert(opt).One(ctx, exec)
	if err != nil {
		return nil, err
	}

	m.R.CreatorUser = rel0
	m.R.Site = rel1

	if err := o.insertOptRels(ctx, exec, m); err != nil {
		return nil, err
	}
	return m, err
}

// MustCreate builds a pool and inserts it into the database
// Relations objects are also inserted and placed in the .R field
// panics if an error occurs
func (o *PoolTemplate) MustCreate(ctx context.Context, exec bob.Executor) *models.Pool {
	m, err := o.Create(ctx, exec)
	if err != nil {
		panic(err)
	}
	return m
}

// CreateOrFail builds a pool and inserts it into the database
// Relations objects are also inserted and placed in the .R field
// It calls `tb.Fatal(err)` on the test/benchmark if an error occurs
func (o *PoolTemplate) CreateOrFail(ctx context.Context, tb testing.TB, exec bob.Executor) *models.Pool {
	tb.Helper()
	m, err := o.Create(ctx, exec)
	if err != nil {
		tb.Fatal(err)
		return nil
	}
	return m
}

// CreateMany builds multiple pools and inserts them into the database
// Relations objects are also inserted and placed in the .R field
func (o PoolTemplate) CreateMany(ctx context.Context, exec bob.Executor, number int) (models.PoolSlice, error) {
	var err error
	m := make(models.PoolSlice, number)

	for i := range m {
		m[i], err = o.Create(ctx, exec)
		if err != nil {
			return nil, err
		}
	}

	return m, nil
}

// MustCreateMany builds multiple pools and inserts them into the database
// Relations objects are also inserted and placed in the .R field
// panics if an error occurs
func (o PoolTemplate) MustCreateMany(ctx context.Context, exec bob.Executor, number int) models.PoolSlice {
	m, err := o.CreateMany(ctx, exec, number)
	if err != nil {
		panic(err)
	}
	return m
}

// CreateManyOrFail builds multiple pools and inserts them into the database
// Relations objects are also inserted and placed in the .R field
// It calls `tb.Fatal(err)` on the test/benchmark if an error occurs
func (o PoolTemplate) CreateManyOrFail(ctx context.Context, tb testing.TB, exec bob.Executor, number int) models.PoolSlice {
	tb.Helper()
	m, err := o.CreateMany(ctx, exec, number)
	if err != nil {
		tb.Fatal(err)
		return nil
	}
	return m
}

// Pool has methods that act as mods for the PoolTemplate
var PoolMods poolMods

type poolMods struct{}

func (m poolMods) RandomizeAllColumns(f *faker.Faker) PoolMod {
	return PoolModSlice{
		PoolMods.RandomCondition(f),
		PoolMods.RandomCreated(f),
		PoolMods.RandomCreatorID(f),
		PoolMods.RandomID(f),
		PoolMods.RandomSiteID(f),
		PoolMods.RandomSiteVersion(f),
	}
}

// Set the model columns to this value
func (m poolMods) Condition(val enums.Poolconditiontype) PoolMod {
	return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
		o.Condition = func() enums.Poolconditiontype { return val }
	})
}

// Set the Column from the function
func (m poolMods) ConditionFunc(f func() enums.Poolconditiontype) PoolMod {
	return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
		o.Condition = f
	})
}

// Clear any values for the column
func (m poolMods) UnsetCondition() PoolMod {
	return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
		o.Condition = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m poolMods) RandomCondition(f *faker.Faker) PoolMod {
	return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
		o.Condition = func() enums.Poolconditiontype {
			return random_enums_Poolconditiontype(f)
		}
	})
}

// Set the model columns to this value
func (m poolMods) Created(val time.Time) PoolMod {
	return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
		o.Created = func() time.Time { return val }
	})
}

// Set the Column from the function
func (m poolMods) CreatedFunc(f func() time.Time) PoolMod {
	return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
		o.Created = f
	})
}

// Clear any values for the column
func (m poolMods) UnsetCreated() PoolMod {
	return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
		o.Created = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m poolMods) RandomCreated(f *faker.Faker) PoolMod {
	return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
		o.Created = func() time.Time {
			return random_time_Time(f)
		}
	})
}

// Set the model columns to this value
func (m poolMods) CreatorID(val int32) PoolMod {
	return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
		o.CreatorID = func() int32 { return val }
	})
}

// Set the Column from the function
func (m poolMods) CreatorIDFunc(f func() int32) PoolMod {
	return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
		o.CreatorID = f
	})
}

// Clear any values for the column
func (m poolMods) UnsetCreatorID() PoolMod {
	return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
		o.CreatorID = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m poolMods) RandomCreatorID(f *faker.Faker) PoolMod {
	return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
		o.CreatorID = func() int32 {
			return random_int32(f)
		}
	})
}

// Set the model columns to this value
func (m poolMods) ID(val int32) PoolMod {
	return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
		o.ID = func() int32 { return val }
	})
}

// Set the Column from the function
func (m poolMods) IDFunc(f func() int32) PoolMod {
	return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
		o.ID = f
	})
}

// Clear any values for the column
func (m poolMods) UnsetID() PoolMod {
	return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
		o.ID = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m poolMods) RandomID(f *faker.Faker) PoolMod {
	return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
		o.ID = func() int32 {
			return random_int32(f)
		}
	})
}

// Set the model columns to this value
func (m poolMods) SiteID(val int32) PoolMod {
	return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
		o.SiteID = func() int32 { return val }
	})
}

// Set the Column from the function
func (m poolMods) SiteIDFunc(f func() int32) PoolMod {
	return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
		o.SiteID = f
	})
}

// Clear any values for the column
func (m poolMods) UnsetSiteID() PoolMod {
	return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
		o.SiteID = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m poolMods) RandomSiteID(f *faker.Faker) PoolMod {
	return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
		o.SiteID = func() int32 {
			return random_int32(f)
		}
	})
}

// Set the model columns to this value
func (m poolMods) SiteVersion(val int32) PoolMod {
	return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
		o.SiteVersion = func() int32 { return val }
	})
}

// Set the Column from the function
func (m poolMods) SiteVersionFunc(f func() int32) PoolMod {
	return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
		o.SiteVersion = f
	})
}

// Clear any values for the column
func (m poolMods) UnsetSiteVersion() PoolMod {
	return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
		o.SiteVersion = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m poolMods) RandomSiteVersion(f *faker.Faker) PoolMod {
	return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
		o.SiteVersion = func() int32 {
			return random_int32(f)
		}
	})
}

func (m poolMods) WithParentsCascading() PoolMod {
	return PoolModFunc(func(ctx context.Context, o *PoolTemplate) {
		if isDone, _ := poolWithParentsCascadingCtx.Value(ctx); isDone {
			return
		}
		ctx = poolWithParentsCascadingCtx.WithValue(ctx, true)
		{

			related := o.f.NewUserWithContext(ctx, UserMods.WithParentsCascading())
			m.WithCreatorUser(related).Apply(ctx, o)
		}
		{

			related := o.f.NewSiteWithContext(ctx, SiteMods.WithParentsCascading())
			m.WithSite(related).Apply(ctx, o)
		}
	})
}

func (m poolMods) WithCreatorUser(rel *UserTemplate) PoolMod {
	return PoolModFunc(func(ctx context.Context, o *PoolTemplate) {
		o.r.CreatorUser = &poolRCreatorUserR{
			o: rel,
		}
	})
}

func (m poolMods) WithNewCreatorUser(mods ...UserMod) PoolMod {
	return PoolModFunc(func(ctx context.Context, o *PoolTemplate) {
		related := o.f.NewUserWithContext(ctx, mods...)

		m.WithCreatorUser(related).Apply(ctx, o)
	})
}

func (m poolMods) WithExistingCreatorUser(em *models.User) PoolMod {
	return PoolModFunc(func(ctx context.Context, o *PoolTemplate) {
		o.r.CreatorUser = &poolRCreatorUserR{
			o: o.f.FromExistingUser(em),
		}
	})
}

func (m poolMods) WithoutCreatorUser() PoolMod {
	return PoolModFunc(func(ctx context.Context, o *PoolTemplate) {
		o.r.CreatorUser = nil
	})
}

func (m poolMods) WithSite(rel *SiteTemplate) PoolMod {
	return PoolModFunc(func(ctx context.Context, o *PoolTemplate) {
		o.r.Site = &poolRSiteR{
			o: rel,
		}
	})
}

func (m poolMods) WithNewSite(mods ...SiteMod) PoolMod {
	return PoolModFunc(func(ctx context.Context, o *PoolTemplate) {
		related := o.f.NewSiteWithContext(ctx, mods...)

		m.WithSite(related).Apply(ctx, o)
	})
}

func (m poolMods) WithExistingSite(em *models.Site) PoolMod {
	return PoolModFunc(func(ctx context.Context, o *PoolTemplate) {
		o.r.Site = &poolRSiteR{
			o: o.f.FromExistingSite(em),
		}
	})
}

func (m poolMods) WithoutSite() PoolMod {
	return PoolModFunc(func(ctx context.Context, o *PoolTemplate) {
		o.r.Site = nil
	})
}
