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

type PublicreportNotifyPhoneNuisanceModFunc func(context.Context, *PublicreportNotifyPhoneNuisanceTemplate)

func (f PublicreportNotifyPhoneNuisanceModFunc) Apply(ctx context.Context, n *PublicreportNotifyPhoneNuisanceTemplate) {
	f(ctx, n)
}

type PublicreportNotifyPhoneNuisanceModSlice []PublicreportNotifyPhoneNuisanceMod

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

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

	r publicreportNotifyPhoneNuisanceR
	f *Factory

	alreadyPersisted bool
}

type publicreportNotifyPhoneNuisanceR struct {
	Nuisance       *publicreportNotifyPhoneNuisanceRNuisanceR
	PhoneE164Phone *publicreportNotifyPhoneNuisanceRPhoneE164PhoneR
}

type publicreportNotifyPhoneNuisanceRNuisanceR struct {
	o *PublicreportNuisanceTemplate
}
type publicreportNotifyPhoneNuisanceRPhoneE164PhoneR struct {
	o *CommsPhoneTemplate
}

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

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

	if t.r.PhoneE164Phone != nil {
		rel := t.r.PhoneE164Phone.o.Build()
		rel.R.PhoneE164NotifyPhoneNuisances = append(rel.R.PhoneE164NotifyPhoneNuisances, o)
		o.PhoneE164 = rel.E164 // h2
		o.R.PhoneE164Phone = rel
	}
}

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

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

	return m
}

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

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

	return m
}

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

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

	o.setModelRels(m)

	return m
}

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

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

	return m
}

func ensureCreatablePublicreportNotifyPhoneNuisance(m *models.PublicreportNotifyPhoneNuisanceSetter) {
	if !(m.Created.IsValue()) {
		val := random_time_Time(nil)
		m.Created = omit.From(val)
	}
	if !(m.NuisanceID.IsValue()) {
		val := random_int32(nil)
		m.NuisanceID = omit.From(val)
	}
	if !(m.PhoneE164.IsValue()) {
		val := random_string(nil)
		m.PhoneE164 = omit.From(val)
	}
}

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

	return err
}

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

	if o.r.Nuisance == nil {
		PublicreportNotifyPhoneNuisanceMods.WithNewNuisance().Apply(ctx, o)
	}

	var rel0 *models.PublicreportNuisance

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

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

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

	var rel1 *models.CommsPhone

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

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

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

	m.R.Nuisance = rel0
	m.R.PhoneE164Phone = rel1

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

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

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

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

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

	return m, nil
}

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

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

// PublicreportNotifyPhoneNuisance has methods that act as mods for the PublicreportNotifyPhoneNuisanceTemplate
var PublicreportNotifyPhoneNuisanceMods publicreportNotifyPhoneNuisanceMods

type publicreportNotifyPhoneNuisanceMods struct{}

func (m publicreportNotifyPhoneNuisanceMods) RandomizeAllColumns(f *faker.Faker) PublicreportNotifyPhoneNuisanceMod {
	return PublicreportNotifyPhoneNuisanceModSlice{
		PublicreportNotifyPhoneNuisanceMods.RandomCreated(f),
		PublicreportNotifyPhoneNuisanceMods.RandomDeleted(f),
		PublicreportNotifyPhoneNuisanceMods.RandomNuisanceID(f),
		PublicreportNotifyPhoneNuisanceMods.RandomPhoneE164(f),
	}
}

// Set the model columns to this value
func (m publicreportNotifyPhoneNuisanceMods) Created(val time.Time) PublicreportNotifyPhoneNuisanceMod {
	return PublicreportNotifyPhoneNuisanceModFunc(func(_ context.Context, o *PublicreportNotifyPhoneNuisanceTemplate) {
		o.Created = func() time.Time { return val }
	})
}

// Set the Column from the function
func (m publicreportNotifyPhoneNuisanceMods) CreatedFunc(f func() time.Time) PublicreportNotifyPhoneNuisanceMod {
	return PublicreportNotifyPhoneNuisanceModFunc(func(_ context.Context, o *PublicreportNotifyPhoneNuisanceTemplate) {
		o.Created = f
	})
}

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

// Set the model columns to this value
func (m publicreportNotifyPhoneNuisanceMods) Deleted(val null.Val[time.Time]) PublicreportNotifyPhoneNuisanceMod {
	return PublicreportNotifyPhoneNuisanceModFunc(func(_ context.Context, o *PublicreportNotifyPhoneNuisanceTemplate) {
		o.Deleted = func() null.Val[time.Time] { return val }
	})
}

// Set the Column from the function
func (m publicreportNotifyPhoneNuisanceMods) DeletedFunc(f func() null.Val[time.Time]) PublicreportNotifyPhoneNuisanceMod {
	return PublicreportNotifyPhoneNuisanceModFunc(func(_ context.Context, o *PublicreportNotifyPhoneNuisanceTemplate) {
		o.Deleted = f
	})
}

// Clear any values for the column
func (m publicreportNotifyPhoneNuisanceMods) UnsetDeleted() PublicreportNotifyPhoneNuisanceMod {
	return PublicreportNotifyPhoneNuisanceModFunc(func(_ context.Context, o *PublicreportNotifyPhoneNuisanceTemplate) {
		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 publicreportNotifyPhoneNuisanceMods) RandomDeleted(f *faker.Faker) PublicreportNotifyPhoneNuisanceMod {
	return PublicreportNotifyPhoneNuisanceModFunc(func(_ context.Context, o *PublicreportNotifyPhoneNuisanceTemplate) {
		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 publicreportNotifyPhoneNuisanceMods) RandomDeletedNotNull(f *faker.Faker) PublicreportNotifyPhoneNuisanceMod {
	return PublicreportNotifyPhoneNuisanceModFunc(func(_ context.Context, o *PublicreportNotifyPhoneNuisanceTemplate) {
		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 publicreportNotifyPhoneNuisanceMods) NuisanceID(val int32) PublicreportNotifyPhoneNuisanceMod {
	return PublicreportNotifyPhoneNuisanceModFunc(func(_ context.Context, o *PublicreportNotifyPhoneNuisanceTemplate) {
		o.NuisanceID = func() int32 { return val }
	})
}

// Set the Column from the function
func (m publicreportNotifyPhoneNuisanceMods) NuisanceIDFunc(f func() int32) PublicreportNotifyPhoneNuisanceMod {
	return PublicreportNotifyPhoneNuisanceModFunc(func(_ context.Context, o *PublicreportNotifyPhoneNuisanceTemplate) {
		o.NuisanceID = f
	})
}

// Clear any values for the column
func (m publicreportNotifyPhoneNuisanceMods) UnsetNuisanceID() PublicreportNotifyPhoneNuisanceMod {
	return PublicreportNotifyPhoneNuisanceModFunc(func(_ context.Context, o *PublicreportNotifyPhoneNuisanceTemplate) {
		o.NuisanceID = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m publicreportNotifyPhoneNuisanceMods) RandomNuisanceID(f *faker.Faker) PublicreportNotifyPhoneNuisanceMod {
	return PublicreportNotifyPhoneNuisanceModFunc(func(_ context.Context, o *PublicreportNotifyPhoneNuisanceTemplate) {
		o.NuisanceID = func() int32 {
			return random_int32(f)
		}
	})
}

// Set the model columns to this value
func (m publicreportNotifyPhoneNuisanceMods) PhoneE164(val string) PublicreportNotifyPhoneNuisanceMod {
	return PublicreportNotifyPhoneNuisanceModFunc(func(_ context.Context, o *PublicreportNotifyPhoneNuisanceTemplate) {
		o.PhoneE164 = func() string { return val }
	})
}

// Set the Column from the function
func (m publicreportNotifyPhoneNuisanceMods) PhoneE164Func(f func() string) PublicreportNotifyPhoneNuisanceMod {
	return PublicreportNotifyPhoneNuisanceModFunc(func(_ context.Context, o *PublicreportNotifyPhoneNuisanceTemplate) {
		o.PhoneE164 = f
	})
}

// Clear any values for the column
func (m publicreportNotifyPhoneNuisanceMods) UnsetPhoneE164() PublicreportNotifyPhoneNuisanceMod {
	return PublicreportNotifyPhoneNuisanceModFunc(func(_ context.Context, o *PublicreportNotifyPhoneNuisanceTemplate) {
		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 publicreportNotifyPhoneNuisanceMods) RandomPhoneE164(f *faker.Faker) PublicreportNotifyPhoneNuisanceMod {
	return PublicreportNotifyPhoneNuisanceModFunc(func(_ context.Context, o *PublicreportNotifyPhoneNuisanceTemplate) {
		o.PhoneE164 = func() string {
			return random_string(f)
		}
	})
}

func (m publicreportNotifyPhoneNuisanceMods) WithParentsCascading() PublicreportNotifyPhoneNuisanceMod {
	return PublicreportNotifyPhoneNuisanceModFunc(func(ctx context.Context, o *PublicreportNotifyPhoneNuisanceTemplate) {
		if isDone, _ := publicreportNotifyPhoneNuisanceWithParentsCascadingCtx.Value(ctx); isDone {
			return
		}
		ctx = publicreportNotifyPhoneNuisanceWithParentsCascadingCtx.WithValue(ctx, true)
		{

			related := o.f.NewPublicreportNuisanceWithContext(ctx, PublicreportNuisanceMods.WithParentsCascading())
			m.WithNuisance(related).Apply(ctx, o)
		}
		{

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

func (m publicreportNotifyPhoneNuisanceMods) WithNuisance(rel *PublicreportNuisanceTemplate) PublicreportNotifyPhoneNuisanceMod {
	return PublicreportNotifyPhoneNuisanceModFunc(func(ctx context.Context, o *PublicreportNotifyPhoneNuisanceTemplate) {
		o.r.Nuisance = &publicreportNotifyPhoneNuisanceRNuisanceR{
			o: rel,
		}
	})
}

func (m publicreportNotifyPhoneNuisanceMods) WithNewNuisance(mods ...PublicreportNuisanceMod) PublicreportNotifyPhoneNuisanceMod {
	return PublicreportNotifyPhoneNuisanceModFunc(func(ctx context.Context, o *PublicreportNotifyPhoneNuisanceTemplate) {
		related := o.f.NewPublicreportNuisanceWithContext(ctx, mods...)

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

func (m publicreportNotifyPhoneNuisanceMods) WithExistingNuisance(em *models.PublicreportNuisance) PublicreportNotifyPhoneNuisanceMod {
	return PublicreportNotifyPhoneNuisanceModFunc(func(ctx context.Context, o *PublicreportNotifyPhoneNuisanceTemplate) {
		o.r.Nuisance = &publicreportNotifyPhoneNuisanceRNuisanceR{
			o: o.f.FromExistingPublicreportNuisance(em),
		}
	})
}

func (m publicreportNotifyPhoneNuisanceMods) WithoutNuisance() PublicreportNotifyPhoneNuisanceMod {
	return PublicreportNotifyPhoneNuisanceModFunc(func(ctx context.Context, o *PublicreportNotifyPhoneNuisanceTemplate) {
		o.r.Nuisance = nil
	})
}

func (m publicreportNotifyPhoneNuisanceMods) WithPhoneE164Phone(rel *CommsPhoneTemplate) PublicreportNotifyPhoneNuisanceMod {
	return PublicreportNotifyPhoneNuisanceModFunc(func(ctx context.Context, o *PublicreportNotifyPhoneNuisanceTemplate) {
		o.r.PhoneE164Phone = &publicreportNotifyPhoneNuisanceRPhoneE164PhoneR{
			o: rel,
		}
	})
}

func (m publicreportNotifyPhoneNuisanceMods) WithNewPhoneE164Phone(mods ...CommsPhoneMod) PublicreportNotifyPhoneNuisanceMod {
	return PublicreportNotifyPhoneNuisanceModFunc(func(ctx context.Context, o *PublicreportNotifyPhoneNuisanceTemplate) {
		related := o.f.NewCommsPhoneWithContext(ctx, mods...)

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

func (m publicreportNotifyPhoneNuisanceMods) WithExistingPhoneE164Phone(em *models.CommsPhone) PublicreportNotifyPhoneNuisanceMod {
	return PublicreportNotifyPhoneNuisanceModFunc(func(ctx context.Context, o *PublicreportNotifyPhoneNuisanceTemplate) {
		o.r.PhoneE164Phone = &publicreportNotifyPhoneNuisanceRPhoneE164PhoneR{
			o: o.f.FromExistingCommsPhone(em),
		}
	})
}

func (m publicreportNotifyPhoneNuisanceMods) WithoutPhoneE164Phone() PublicreportNotifyPhoneNuisanceMod {
	return PublicreportNotifyPhoneNuisanceModFunc(func(ctx context.Context, o *PublicreportNotifyPhoneNuisanceTemplate) {
		o.r.PhoneE164Phone = nil
	})
}
