// 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"
	models "github.com/Gleipnir-Technology/nidus-sync/db/models"
	"github.com/aarondl/opt/null"
	"github.com/aarondl/opt/omit"
	"github.com/aarondl/opt/omitnull"
	"github.com/jaswdr/faker/v2"
)

type PublicreportNotifyEmailPoolMod interface {
	Apply(context.Context, *PublicreportNotifyEmailPoolTemplate)
}

type PublicreportNotifyEmailPoolModFunc func(context.Context, *PublicreportNotifyEmailPoolTemplate)

func (f PublicreportNotifyEmailPoolModFunc) Apply(ctx context.Context, n *PublicreportNotifyEmailPoolTemplate) {
	f(ctx, n)
}

type PublicreportNotifyEmailPoolModSlice []PublicreportNotifyEmailPoolMod

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

// PublicreportNotifyEmailPoolTemplate is an object representing the database table.
// all columns are optional and should be set by mods
type PublicreportNotifyEmailPoolTemplate struct {
	Created      func() time.Time
	Deleted      func() null.Val[time.Time]
	PoolID       func() int32
	EmailAddress func() string

	r publicreportNotifyEmailPoolR
	f *Factory

	alreadyPersisted bool
}

type publicreportNotifyEmailPoolR struct {
	EmailAddressEmailContact *publicreportNotifyEmailPoolREmailAddressEmailContactR
	Pool                     *publicreportNotifyEmailPoolRPoolR
}

type publicreportNotifyEmailPoolREmailAddressEmailContactR struct {
	o *CommsEmailContactTemplate
}
type publicreportNotifyEmailPoolRPoolR struct {
	o *PublicreportPoolTemplate
}

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

// setModelRels creates and sets the relationships on *models.PublicreportNotifyEmailPool
// according to the relationships in the template. Nothing is inserted into the db
func (t PublicreportNotifyEmailPoolTemplate) setModelRels(o *models.PublicreportNotifyEmailPool) {
	if t.r.EmailAddressEmailContact != nil {
		rel := t.r.EmailAddressEmailContact.o.Build()
		rel.R.EmailAddressNotifyEmailPools = append(rel.R.EmailAddressNotifyEmailPools, o)
		o.EmailAddress = rel.Address // h2
		o.R.EmailAddressEmailContact = rel
	}

	if t.r.Pool != nil {
		rel := t.r.Pool.o.Build()
		rel.R.NotifyEmailPools = append(rel.R.NotifyEmailPools, o)
		o.PoolID = rel.ID // h2
		o.R.Pool = rel
	}
}

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

	if o.Created != nil {
		val := o.Created()
		m.Created = omit.From(val)
	}
	if o.Deleted != nil {
		val := o.Deleted()
		m.Deleted = omitnull.FromNull(val)
	}
	if o.PoolID != nil {
		val := o.PoolID()
		m.PoolID = omit.From(val)
	}
	if o.EmailAddress != nil {
		val := o.EmailAddress()
		m.EmailAddress = omit.From(val)
	}

	return m
}

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

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

	return m
}

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

	if o.Created != nil {
		m.Created = o.Created()
	}
	if o.Deleted != nil {
		m.Deleted = o.Deleted()
	}
	if o.PoolID != nil {
		m.PoolID = o.PoolID()
	}
	if o.EmailAddress != nil {
		m.EmailAddress = o.EmailAddress()
	}

	o.setModelRels(m)

	return m
}

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

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

	return m
}

func ensureCreatablePublicreportNotifyEmailPool(m *models.PublicreportNotifyEmailPoolSetter) {
	if !(m.Created.IsValue()) {
		val := random_time_Time(nil)
		m.Created = omit.From(val)
	}
	if !(m.PoolID.IsValue()) {
		val := random_int32(nil)
		m.PoolID = omit.From(val)
	}
	if !(m.EmailAddress.IsValue()) {
		val := random_string(nil)
		m.EmailAddress = omit.From(val)
	}
}

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

	return err
}

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

	if o.r.EmailAddressEmailContact == nil {
		PublicreportNotifyEmailPoolMods.WithNewEmailAddressEmailContact().Apply(ctx, o)
	}

	var rel0 *models.CommsEmailContact

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

	opt.EmailAddress = omit.From(rel0.Address)

	if o.r.Pool == nil {
		PublicreportNotifyEmailPoolMods.WithNewPool().Apply(ctx, o)
	}

	var rel1 *models.PublicreportPool

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

	opt.PoolID = omit.From(rel1.ID)

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

	m.R.EmailAddressEmailContact = rel0
	m.R.Pool = rel1

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

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

// CreateOrFail builds a publicreportNotifyEmailPool 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 *PublicreportNotifyEmailPoolTemplate) CreateOrFail(ctx context.Context, tb testing.TB, exec bob.Executor) *models.PublicreportNotifyEmailPool {
	tb.Helper()
	m, err := o.Create(ctx, exec)
	if err != nil {
		tb.Fatal(err)
		return nil
	}
	return m
}

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

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

	return m, nil
}

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

// CreateManyOrFail builds multiple publicreportNotifyEmailPools 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 PublicreportNotifyEmailPoolTemplate) CreateManyOrFail(ctx context.Context, tb testing.TB, exec bob.Executor, number int) models.PublicreportNotifyEmailPoolSlice {
	tb.Helper()
	m, err := o.CreateMany(ctx, exec, number)
	if err != nil {
		tb.Fatal(err)
		return nil
	}
	return m
}

// PublicreportNotifyEmailPool has methods that act as mods for the PublicreportNotifyEmailPoolTemplate
var PublicreportNotifyEmailPoolMods publicreportNotifyEmailPoolMods

type publicreportNotifyEmailPoolMods struct{}

func (m publicreportNotifyEmailPoolMods) RandomizeAllColumns(f *faker.Faker) PublicreportNotifyEmailPoolMod {
	return PublicreportNotifyEmailPoolModSlice{
		PublicreportNotifyEmailPoolMods.RandomCreated(f),
		PublicreportNotifyEmailPoolMods.RandomDeleted(f),
		PublicreportNotifyEmailPoolMods.RandomPoolID(f),
		PublicreportNotifyEmailPoolMods.RandomEmailAddress(f),
	}
}

// Set the model columns to this value
func (m publicreportNotifyEmailPoolMods) Created(val time.Time) PublicreportNotifyEmailPoolMod {
	return PublicreportNotifyEmailPoolModFunc(func(_ context.Context, o *PublicreportNotifyEmailPoolTemplate) {
		o.Created = func() time.Time { return val }
	})
}

// Set the Column from the function
func (m publicreportNotifyEmailPoolMods) CreatedFunc(f func() time.Time) PublicreportNotifyEmailPoolMod {
	return PublicreportNotifyEmailPoolModFunc(func(_ context.Context, o *PublicreportNotifyEmailPoolTemplate) {
		o.Created = f
	})
}

// Clear any values for the column
func (m publicreportNotifyEmailPoolMods) UnsetCreated() PublicreportNotifyEmailPoolMod {
	return PublicreportNotifyEmailPoolModFunc(func(_ context.Context, o *PublicreportNotifyEmailPoolTemplate) {
		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 publicreportNotifyEmailPoolMods) RandomCreated(f *faker.Faker) PublicreportNotifyEmailPoolMod {
	return PublicreportNotifyEmailPoolModFunc(func(_ context.Context, o *PublicreportNotifyEmailPoolTemplate) {
		o.Created = func() time.Time {
			return random_time_Time(f)
		}
	})
}

// Set the model columns to this value
func (m publicreportNotifyEmailPoolMods) Deleted(val null.Val[time.Time]) PublicreportNotifyEmailPoolMod {
	return PublicreportNotifyEmailPoolModFunc(func(_ context.Context, o *PublicreportNotifyEmailPoolTemplate) {
		o.Deleted = func() null.Val[time.Time] { return val }
	})
}

// Set the Column from the function
func (m publicreportNotifyEmailPoolMods) DeletedFunc(f func() null.Val[time.Time]) PublicreportNotifyEmailPoolMod {
	return PublicreportNotifyEmailPoolModFunc(func(_ context.Context, o *PublicreportNotifyEmailPoolTemplate) {
		o.Deleted = f
	})
}

// Clear any values for the column
func (m publicreportNotifyEmailPoolMods) UnsetDeleted() PublicreportNotifyEmailPoolMod {
	return PublicreportNotifyEmailPoolModFunc(func(_ context.Context, o *PublicreportNotifyEmailPoolTemplate) {
		o.Deleted = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
// The generated value is sometimes null
func (m publicreportNotifyEmailPoolMods) RandomDeleted(f *faker.Faker) PublicreportNotifyEmailPoolMod {
	return PublicreportNotifyEmailPoolModFunc(func(_ context.Context, o *PublicreportNotifyEmailPoolTemplate) {
		o.Deleted = func() null.Val[time.Time] {
			if f == nil {
				f = &defaultFaker
			}

			val := random_time_Time(f)
			return null.From(val)
		}
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
// The generated value is never null
func (m publicreportNotifyEmailPoolMods) RandomDeletedNotNull(f *faker.Faker) PublicreportNotifyEmailPoolMod {
	return PublicreportNotifyEmailPoolModFunc(func(_ context.Context, o *PublicreportNotifyEmailPoolTemplate) {
		o.Deleted = func() null.Val[time.Time] {
			if f == nil {
				f = &defaultFaker
			}

			val := random_time_Time(f)
			return null.From(val)
		}
	})
}

// Set the model columns to this value
func (m publicreportNotifyEmailPoolMods) PoolID(val int32) PublicreportNotifyEmailPoolMod {
	return PublicreportNotifyEmailPoolModFunc(func(_ context.Context, o *PublicreportNotifyEmailPoolTemplate) {
		o.PoolID = func() int32 { return val }
	})
}

// Set the Column from the function
func (m publicreportNotifyEmailPoolMods) PoolIDFunc(f func() int32) PublicreportNotifyEmailPoolMod {
	return PublicreportNotifyEmailPoolModFunc(func(_ context.Context, o *PublicreportNotifyEmailPoolTemplate) {
		o.PoolID = f
	})
}

// Clear any values for the column
func (m publicreportNotifyEmailPoolMods) UnsetPoolID() PublicreportNotifyEmailPoolMod {
	return PublicreportNotifyEmailPoolModFunc(func(_ context.Context, o *PublicreportNotifyEmailPoolTemplate) {
		o.PoolID = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m publicreportNotifyEmailPoolMods) RandomPoolID(f *faker.Faker) PublicreportNotifyEmailPoolMod {
	return PublicreportNotifyEmailPoolModFunc(func(_ context.Context, o *PublicreportNotifyEmailPoolTemplate) {
		o.PoolID = func() int32 {
			return random_int32(f)
		}
	})
}

// Set the model columns to this value
func (m publicreportNotifyEmailPoolMods) EmailAddress(val string) PublicreportNotifyEmailPoolMod {
	return PublicreportNotifyEmailPoolModFunc(func(_ context.Context, o *PublicreportNotifyEmailPoolTemplate) {
		o.EmailAddress = func() string { return val }
	})
}

// Set the Column from the function
func (m publicreportNotifyEmailPoolMods) EmailAddressFunc(f func() string) PublicreportNotifyEmailPoolMod {
	return PublicreportNotifyEmailPoolModFunc(func(_ context.Context, o *PublicreportNotifyEmailPoolTemplate) {
		o.EmailAddress = f
	})
}

// Clear any values for the column
func (m publicreportNotifyEmailPoolMods) UnsetEmailAddress() PublicreportNotifyEmailPoolMod {
	return PublicreportNotifyEmailPoolModFunc(func(_ context.Context, o *PublicreportNotifyEmailPoolTemplate) {
		o.EmailAddress = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m publicreportNotifyEmailPoolMods) RandomEmailAddress(f *faker.Faker) PublicreportNotifyEmailPoolMod {
	return PublicreportNotifyEmailPoolModFunc(func(_ context.Context, o *PublicreportNotifyEmailPoolTemplate) {
		o.EmailAddress = func() string {
			return random_string(f)
		}
	})
}

func (m publicreportNotifyEmailPoolMods) WithParentsCascading() PublicreportNotifyEmailPoolMod {
	return PublicreportNotifyEmailPoolModFunc(func(ctx context.Context, o *PublicreportNotifyEmailPoolTemplate) {
		if isDone, _ := publicreportNotifyEmailPoolWithParentsCascadingCtx.Value(ctx); isDone {
			return
		}
		ctx = publicreportNotifyEmailPoolWithParentsCascadingCtx.WithValue(ctx, true)
		{

			related := o.f.NewCommsEmailContactWithContext(ctx, CommsEmailContactMods.WithParentsCascading())
			m.WithEmailAddressEmailContact(related).Apply(ctx, o)
		}
		{

			related := o.f.NewPublicreportPoolWithContext(ctx, PublicreportPoolMods.WithParentsCascading())
			m.WithPool(related).Apply(ctx, o)
		}
	})
}

func (m publicreportNotifyEmailPoolMods) WithEmailAddressEmailContact(rel *CommsEmailContactTemplate) PublicreportNotifyEmailPoolMod {
	return PublicreportNotifyEmailPoolModFunc(func(ctx context.Context, o *PublicreportNotifyEmailPoolTemplate) {
		o.r.EmailAddressEmailContact = &publicreportNotifyEmailPoolREmailAddressEmailContactR{
			o: rel,
		}
	})
}

func (m publicreportNotifyEmailPoolMods) WithNewEmailAddressEmailContact(mods ...CommsEmailContactMod) PublicreportNotifyEmailPoolMod {
	return PublicreportNotifyEmailPoolModFunc(func(ctx context.Context, o *PublicreportNotifyEmailPoolTemplate) {
		related := o.f.NewCommsEmailContactWithContext(ctx, mods...)

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

func (m publicreportNotifyEmailPoolMods) WithExistingEmailAddressEmailContact(em *models.CommsEmailContact) PublicreportNotifyEmailPoolMod {
	return PublicreportNotifyEmailPoolModFunc(func(ctx context.Context, o *PublicreportNotifyEmailPoolTemplate) {
		o.r.EmailAddressEmailContact = &publicreportNotifyEmailPoolREmailAddressEmailContactR{
			o: o.f.FromExistingCommsEmailContact(em),
		}
	})
}

func (m publicreportNotifyEmailPoolMods) WithoutEmailAddressEmailContact() PublicreportNotifyEmailPoolMod {
	return PublicreportNotifyEmailPoolModFunc(func(ctx context.Context, o *PublicreportNotifyEmailPoolTemplate) {
		o.r.EmailAddressEmailContact = nil
	})
}

func (m publicreportNotifyEmailPoolMods) WithPool(rel *PublicreportPoolTemplate) PublicreportNotifyEmailPoolMod {
	return PublicreportNotifyEmailPoolModFunc(func(ctx context.Context, o *PublicreportNotifyEmailPoolTemplate) {
		o.r.Pool = &publicreportNotifyEmailPoolRPoolR{
			o: rel,
		}
	})
}

func (m publicreportNotifyEmailPoolMods) WithNewPool(mods ...PublicreportPoolMod) PublicreportNotifyEmailPoolMod {
	return PublicreportNotifyEmailPoolModFunc(func(ctx context.Context, o *PublicreportNotifyEmailPoolTemplate) {
		related := o.f.NewPublicreportPoolWithContext(ctx, mods...)

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

func (m publicreportNotifyEmailPoolMods) WithExistingPool(em *models.PublicreportPool) PublicreportNotifyEmailPoolMod {
	return PublicreportNotifyEmailPoolModFunc(func(ctx context.Context, o *PublicreportNotifyEmailPoolTemplate) {
		o.r.Pool = &publicreportNotifyEmailPoolRPoolR{
			o: o.f.FromExistingPublicreportPool(em),
		}
	})
}

func (m publicreportNotifyEmailPoolMods) WithoutPool() PublicreportNotifyEmailPoolMod {
	return PublicreportNotifyEmailPoolModFunc(func(ctx context.Context, o *PublicreportNotifyEmailPoolTemplate) {
		o.r.Pool = nil
	})
}
