// 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 PublicreportNotifyPhonePoolMod interface {
	Apply(context.Context, *PublicreportNotifyPhonePoolTemplate)
}

type PublicreportNotifyPhonePoolModFunc func(context.Context, *PublicreportNotifyPhonePoolTemplate)

func (f PublicreportNotifyPhonePoolModFunc) Apply(ctx context.Context, n *PublicreportNotifyPhonePoolTemplate) {
	f(ctx, n)
}

type PublicreportNotifyPhonePoolModSlice []PublicreportNotifyPhonePoolMod

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

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

	r publicreportNotifyPhonePoolR
	f *Factory

	alreadyPersisted bool
}

type publicreportNotifyPhonePoolR struct {
	PhoneE164Phone *publicreportNotifyPhonePoolRPhoneE164PhoneR
	Pool           *publicreportNotifyPhonePoolRPoolR
}

type publicreportNotifyPhonePoolRPhoneE164PhoneR struct {
	o *CommsPhoneTemplate
}
type publicreportNotifyPhonePoolRPoolR struct {
	o *PublicreportPoolTemplate
}

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

// setModelRels creates and sets the relationships on *models.PublicreportNotifyPhonePool
// according to the relationships in the template. Nothing is inserted into the db
func (t PublicreportNotifyPhonePoolTemplate) setModelRels(o *models.PublicreportNotifyPhonePool) {
	if t.r.PhoneE164Phone != nil {
		rel := t.r.PhoneE164Phone.o.Build()
		rel.R.PhoneE164NotifyPhonePools = append(rel.R.PhoneE164NotifyPhonePools, o)
		o.PhoneE164 = rel.E164 // h2
		o.R.PhoneE164Phone = rel
	}

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

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

	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.PhoneE164 != nil {
		val := o.PhoneE164()
		m.PhoneE164 = omit.From(val)
	}
	if o.PoolID != nil {
		val := o.PoolID()
		m.PoolID = omit.From(val)
	}

	return m
}

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

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

	return m
}

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

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

	o.setModelRels(m)

	return m
}

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

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

	return m
}

func ensureCreatablePublicreportNotifyPhonePool(m *models.PublicreportNotifyPhonePoolSetter) {
	if !(m.Created.IsValue()) {
		val := random_time_Time(nil)
		m.Created = omit.From(val)
	}
	if !(m.PhoneE164.IsValue()) {
		val := random_string(nil)
		m.PhoneE164 = omit.From(val)
	}
	if !(m.PoolID.IsValue()) {
		val := random_int32(nil)
		m.PoolID = omit.From(val)
	}
}

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

	return err
}

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

	if o.r.PhoneE164Phone == nil {
		PublicreportNotifyPhonePoolMods.WithNewPhoneE164Phone().Apply(ctx, o)
	}

	var rel0 *models.CommsPhone

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

	opt.PhoneE164 = omit.From(rel0.E164)

	if o.r.Pool == nil {
		PublicreportNotifyPhonePoolMods.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.PublicreportNotifyPhonePools.Insert(opt).One(ctx, exec)
	if err != nil {
		return nil, err
	}

	m.R.PhoneE164Phone = rel0
	m.R.Pool = rel1

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

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

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

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

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

	return m, nil
}

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

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

// PublicreportNotifyPhonePool has methods that act as mods for the PublicreportNotifyPhonePoolTemplate
var PublicreportNotifyPhonePoolMods publicreportNotifyPhonePoolMods

type publicreportNotifyPhonePoolMods struct{}

func (m publicreportNotifyPhonePoolMods) RandomizeAllColumns(f *faker.Faker) PublicreportNotifyPhonePoolMod {
	return PublicreportNotifyPhonePoolModSlice{
		PublicreportNotifyPhonePoolMods.RandomCreated(f),
		PublicreportNotifyPhonePoolMods.RandomDeleted(f),
		PublicreportNotifyPhonePoolMods.RandomPhoneE164(f),
		PublicreportNotifyPhonePoolMods.RandomPoolID(f),
	}
}

// Set the model columns to this value
func (m publicreportNotifyPhonePoolMods) Created(val time.Time) PublicreportNotifyPhonePoolMod {
	return PublicreportNotifyPhonePoolModFunc(func(_ context.Context, o *PublicreportNotifyPhonePoolTemplate) {
		o.Created = func() time.Time { return val }
	})
}

// Set the Column from the function
func (m publicreportNotifyPhonePoolMods) CreatedFunc(f func() time.Time) PublicreportNotifyPhonePoolMod {
	return PublicreportNotifyPhonePoolModFunc(func(_ context.Context, o *PublicreportNotifyPhonePoolTemplate) {
		o.Created = f
	})
}

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

// Set the model columns to this value
func (m publicreportNotifyPhonePoolMods) Deleted(val null.Val[time.Time]) PublicreportNotifyPhonePoolMod {
	return PublicreportNotifyPhonePoolModFunc(func(_ context.Context, o *PublicreportNotifyPhonePoolTemplate) {
		o.Deleted = func() null.Val[time.Time] { return val }
	})
}

// Set the Column from the function
func (m publicreportNotifyPhonePoolMods) DeletedFunc(f func() null.Val[time.Time]) PublicreportNotifyPhonePoolMod {
	return PublicreportNotifyPhonePoolModFunc(func(_ context.Context, o *PublicreportNotifyPhonePoolTemplate) {
		o.Deleted = f
	})
}

// Clear any values for the column
func (m publicreportNotifyPhonePoolMods) UnsetDeleted() PublicreportNotifyPhonePoolMod {
	return PublicreportNotifyPhonePoolModFunc(func(_ context.Context, o *PublicreportNotifyPhonePoolTemplate) {
		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 publicreportNotifyPhonePoolMods) RandomDeleted(f *faker.Faker) PublicreportNotifyPhonePoolMod {
	return PublicreportNotifyPhonePoolModFunc(func(_ context.Context, o *PublicreportNotifyPhonePoolTemplate) {
		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 publicreportNotifyPhonePoolMods) RandomDeletedNotNull(f *faker.Faker) PublicreportNotifyPhonePoolMod {
	return PublicreportNotifyPhonePoolModFunc(func(_ context.Context, o *PublicreportNotifyPhonePoolTemplate) {
		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 publicreportNotifyPhonePoolMods) PhoneE164(val string) PublicreportNotifyPhonePoolMod {
	return PublicreportNotifyPhonePoolModFunc(func(_ context.Context, o *PublicreportNotifyPhonePoolTemplate) {
		o.PhoneE164 = func() string { return val }
	})
}

// Set the Column from the function
func (m publicreportNotifyPhonePoolMods) PhoneE164Func(f func() string) PublicreportNotifyPhonePoolMod {
	return PublicreportNotifyPhonePoolModFunc(func(_ context.Context, o *PublicreportNotifyPhonePoolTemplate) {
		o.PhoneE164 = f
	})
}

// Clear any values for the column
func (m publicreportNotifyPhonePoolMods) UnsetPhoneE164() PublicreportNotifyPhonePoolMod {
	return PublicreportNotifyPhonePoolModFunc(func(_ context.Context, o *PublicreportNotifyPhonePoolTemplate) {
		o.PhoneE164 = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m publicreportNotifyPhonePoolMods) RandomPhoneE164(f *faker.Faker) PublicreportNotifyPhonePoolMod {
	return PublicreportNotifyPhonePoolModFunc(func(_ context.Context, o *PublicreportNotifyPhonePoolTemplate) {
		o.PhoneE164 = func() string {
			return random_string(f)
		}
	})
}

// Set the model columns to this value
func (m publicreportNotifyPhonePoolMods) PoolID(val int32) PublicreportNotifyPhonePoolMod {
	return PublicreportNotifyPhonePoolModFunc(func(_ context.Context, o *PublicreportNotifyPhonePoolTemplate) {
		o.PoolID = func() int32 { return val }
	})
}

// Set the Column from the function
func (m publicreportNotifyPhonePoolMods) PoolIDFunc(f func() int32) PublicreportNotifyPhonePoolMod {
	return PublicreportNotifyPhonePoolModFunc(func(_ context.Context, o *PublicreportNotifyPhonePoolTemplate) {
		o.PoolID = f
	})
}

// Clear any values for the column
func (m publicreportNotifyPhonePoolMods) UnsetPoolID() PublicreportNotifyPhonePoolMod {
	return PublicreportNotifyPhonePoolModFunc(func(_ context.Context, o *PublicreportNotifyPhonePoolTemplate) {
		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 publicreportNotifyPhonePoolMods) RandomPoolID(f *faker.Faker) PublicreportNotifyPhonePoolMod {
	return PublicreportNotifyPhonePoolModFunc(func(_ context.Context, o *PublicreportNotifyPhonePoolTemplate) {
		o.PoolID = func() int32 {
			return random_int32(f)
		}
	})
}

func (m publicreportNotifyPhonePoolMods) WithParentsCascading() PublicreportNotifyPhonePoolMod {
	return PublicreportNotifyPhonePoolModFunc(func(ctx context.Context, o *PublicreportNotifyPhonePoolTemplate) {
		if isDone, _ := publicreportNotifyPhonePoolWithParentsCascadingCtx.Value(ctx); isDone {
			return
		}
		ctx = publicreportNotifyPhonePoolWithParentsCascadingCtx.WithValue(ctx, true)
		{

			related := o.f.NewCommsPhoneWithContext(ctx, CommsPhoneMods.WithParentsCascading())
			m.WithPhoneE164Phone(related).Apply(ctx, o)
		}
		{

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

func (m publicreportNotifyPhonePoolMods) WithPhoneE164Phone(rel *CommsPhoneTemplate) PublicreportNotifyPhonePoolMod {
	return PublicreportNotifyPhonePoolModFunc(func(ctx context.Context, o *PublicreportNotifyPhonePoolTemplate) {
		o.r.PhoneE164Phone = &publicreportNotifyPhonePoolRPhoneE164PhoneR{
			o: rel,
		}
	})
}

func (m publicreportNotifyPhonePoolMods) WithNewPhoneE164Phone(mods ...CommsPhoneMod) PublicreportNotifyPhonePoolMod {
	return PublicreportNotifyPhonePoolModFunc(func(ctx context.Context, o *PublicreportNotifyPhonePoolTemplate) {
		related := o.f.NewCommsPhoneWithContext(ctx, mods...)

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

func (m publicreportNotifyPhonePoolMods) WithExistingPhoneE164Phone(em *models.CommsPhone) PublicreportNotifyPhonePoolMod {
	return PublicreportNotifyPhonePoolModFunc(func(ctx context.Context, o *PublicreportNotifyPhonePoolTemplate) {
		o.r.PhoneE164Phone = &publicreportNotifyPhonePoolRPhoneE164PhoneR{
			o: o.f.FromExistingCommsPhone(em),
		}
	})
}

func (m publicreportNotifyPhonePoolMods) WithoutPhoneE164Phone() PublicreportNotifyPhonePoolMod {
	return PublicreportNotifyPhonePoolModFunc(func(ctx context.Context, o *PublicreportNotifyPhonePoolTemplate) {
		o.r.PhoneE164Phone = nil
	})
}

func (m publicreportNotifyPhonePoolMods) WithPool(rel *PublicreportPoolTemplate) PublicreportNotifyPhonePoolMod {
	return PublicreportNotifyPhonePoolModFunc(func(ctx context.Context, o *PublicreportNotifyPhonePoolTemplate) {
		o.r.Pool = &publicreportNotifyPhonePoolRPoolR{
			o: rel,
		}
	})
}

func (m publicreportNotifyPhonePoolMods) WithNewPool(mods ...PublicreportPoolMod) PublicreportNotifyPhonePoolMod {
	return PublicreportNotifyPhonePoolModFunc(func(ctx context.Context, o *PublicreportNotifyPhonePoolTemplate) {
		related := o.f.NewPublicreportPoolWithContext(ctx, mods...)

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

func (m publicreportNotifyPhonePoolMods) WithExistingPool(em *models.PublicreportPool) PublicreportNotifyPhonePoolMod {
	return PublicreportNotifyPhonePoolModFunc(func(ctx context.Context, o *PublicreportNotifyPhonePoolTemplate) {
		o.r.Pool = &publicreportNotifyPhonePoolRPoolR{
			o: o.f.FromExistingPublicreportPool(em),
		}
	})
}

func (m publicreportNotifyPhonePoolMods) WithoutPool() PublicreportNotifyPhonePoolMod {
	return PublicreportNotifyPhonePoolModFunc(func(ctx context.Context, o *PublicreportNotifyPhonePoolTemplate) {
		o.r.Pool = nil
	})
}
