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

type PublicreportNotifyEmailNuisanceModFunc func(context.Context, *PublicreportNotifyEmailNuisanceTemplate)

func (f PublicreportNotifyEmailNuisanceModFunc) Apply(ctx context.Context, n *PublicreportNotifyEmailNuisanceTemplate) {
	f(ctx, n)
}

type PublicreportNotifyEmailNuisanceModSlice []PublicreportNotifyEmailNuisanceMod

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

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

	r publicreportNotifyEmailNuisanceR
	f *Factory

	alreadyPersisted bool
}

type publicreportNotifyEmailNuisanceR struct {
	EmailAddressEmailContact *publicreportNotifyEmailNuisanceREmailAddressEmailContactR
	Nuisance                 *publicreportNotifyEmailNuisanceRNuisanceR
}

type publicreportNotifyEmailNuisanceREmailAddressEmailContactR struct {
	o *CommsEmailContactTemplate
}
type publicreportNotifyEmailNuisanceRNuisanceR struct {
	o *PublicreportNuisanceTemplate
}

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

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

	if t.r.Nuisance != nil {
		rel := t.r.Nuisance.o.Build()
		rel.R.NotifyEmailNuisances = append(rel.R.NotifyEmailNuisances, o)
		o.NuisanceID = rel.ID // h2
		o.R.Nuisance = rel
	}
}

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

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

	return m
}

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

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

	return m
}

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

	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.EmailAddress != nil {
		m.EmailAddress = o.EmailAddress()
	}

	o.setModelRels(m)

	return m
}

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

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

	return m
}

func ensureCreatablePublicreportNotifyEmailNuisance(m *models.PublicreportNotifyEmailNuisanceSetter) {
	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.EmailAddress.IsValue()) {
		val := random_string(nil)
		m.EmailAddress = omit.From(val)
	}
}

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

	return err
}

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

	if o.r.EmailAddressEmailContact == nil {
		PublicreportNotifyEmailNuisanceMods.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.Nuisance == nil {
		PublicreportNotifyEmailNuisanceMods.WithNewNuisance().Apply(ctx, o)
	}

	var rel1 *models.PublicreportNuisance

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

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

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

	m.R.EmailAddressEmailContact = rel0
	m.R.Nuisance = rel1

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

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

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

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

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

	return m, nil
}

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

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

// PublicreportNotifyEmailNuisance has methods that act as mods for the PublicreportNotifyEmailNuisanceTemplate
var PublicreportNotifyEmailNuisanceMods publicreportNotifyEmailNuisanceMods

type publicreportNotifyEmailNuisanceMods struct{}

func (m publicreportNotifyEmailNuisanceMods) RandomizeAllColumns(f *faker.Faker) PublicreportNotifyEmailNuisanceMod {
	return PublicreportNotifyEmailNuisanceModSlice{
		PublicreportNotifyEmailNuisanceMods.RandomCreated(f),
		PublicreportNotifyEmailNuisanceMods.RandomDeleted(f),
		PublicreportNotifyEmailNuisanceMods.RandomNuisanceID(f),
		PublicreportNotifyEmailNuisanceMods.RandomEmailAddress(f),
	}
}

// Set the model columns to this value
func (m publicreportNotifyEmailNuisanceMods) Created(val time.Time) PublicreportNotifyEmailNuisanceMod {
	return PublicreportNotifyEmailNuisanceModFunc(func(_ context.Context, o *PublicreportNotifyEmailNuisanceTemplate) {
		o.Created = func() time.Time { return val }
	})
}

// Set the Column from the function
func (m publicreportNotifyEmailNuisanceMods) CreatedFunc(f func() time.Time) PublicreportNotifyEmailNuisanceMod {
	return PublicreportNotifyEmailNuisanceModFunc(func(_ context.Context, o *PublicreportNotifyEmailNuisanceTemplate) {
		o.Created = f
	})
}

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

// Set the model columns to this value
func (m publicreportNotifyEmailNuisanceMods) Deleted(val null.Val[time.Time]) PublicreportNotifyEmailNuisanceMod {
	return PublicreportNotifyEmailNuisanceModFunc(func(_ context.Context, o *PublicreportNotifyEmailNuisanceTemplate) {
		o.Deleted = func() null.Val[time.Time] { return val }
	})
}

// Set the Column from the function
func (m publicreportNotifyEmailNuisanceMods) DeletedFunc(f func() null.Val[time.Time]) PublicreportNotifyEmailNuisanceMod {
	return PublicreportNotifyEmailNuisanceModFunc(func(_ context.Context, o *PublicreportNotifyEmailNuisanceTemplate) {
		o.Deleted = f
	})
}

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

// Set the Column from the function
func (m publicreportNotifyEmailNuisanceMods) NuisanceIDFunc(f func() int32) PublicreportNotifyEmailNuisanceMod {
	return PublicreportNotifyEmailNuisanceModFunc(func(_ context.Context, o *PublicreportNotifyEmailNuisanceTemplate) {
		o.NuisanceID = f
	})
}

// Clear any values for the column
func (m publicreportNotifyEmailNuisanceMods) UnsetNuisanceID() PublicreportNotifyEmailNuisanceMod {
	return PublicreportNotifyEmailNuisanceModFunc(func(_ context.Context, o *PublicreportNotifyEmailNuisanceTemplate) {
		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 publicreportNotifyEmailNuisanceMods) RandomNuisanceID(f *faker.Faker) PublicreportNotifyEmailNuisanceMod {
	return PublicreportNotifyEmailNuisanceModFunc(func(_ context.Context, o *PublicreportNotifyEmailNuisanceTemplate) {
		o.NuisanceID = func() int32 {
			return random_int32(f)
		}
	})
}

// Set the model columns to this value
func (m publicreportNotifyEmailNuisanceMods) EmailAddress(val string) PublicreportNotifyEmailNuisanceMod {
	return PublicreportNotifyEmailNuisanceModFunc(func(_ context.Context, o *PublicreportNotifyEmailNuisanceTemplate) {
		o.EmailAddress = func() string { return val }
	})
}

// Set the Column from the function
func (m publicreportNotifyEmailNuisanceMods) EmailAddressFunc(f func() string) PublicreportNotifyEmailNuisanceMod {
	return PublicreportNotifyEmailNuisanceModFunc(func(_ context.Context, o *PublicreportNotifyEmailNuisanceTemplate) {
		o.EmailAddress = f
	})
}

// Clear any values for the column
func (m publicreportNotifyEmailNuisanceMods) UnsetEmailAddress() PublicreportNotifyEmailNuisanceMod {
	return PublicreportNotifyEmailNuisanceModFunc(func(_ context.Context, o *PublicreportNotifyEmailNuisanceTemplate) {
		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 publicreportNotifyEmailNuisanceMods) RandomEmailAddress(f *faker.Faker) PublicreportNotifyEmailNuisanceMod {
	return PublicreportNotifyEmailNuisanceModFunc(func(_ context.Context, o *PublicreportNotifyEmailNuisanceTemplate) {
		o.EmailAddress = func() string {
			return random_string(f)
		}
	})
}

func (m publicreportNotifyEmailNuisanceMods) WithParentsCascading() PublicreportNotifyEmailNuisanceMod {
	return PublicreportNotifyEmailNuisanceModFunc(func(ctx context.Context, o *PublicreportNotifyEmailNuisanceTemplate) {
		if isDone, _ := publicreportNotifyEmailNuisanceWithParentsCascadingCtx.Value(ctx); isDone {
			return
		}
		ctx = publicreportNotifyEmailNuisanceWithParentsCascadingCtx.WithValue(ctx, true)
		{

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

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

func (m publicreportNotifyEmailNuisanceMods) WithEmailAddressEmailContact(rel *CommsEmailContactTemplate) PublicreportNotifyEmailNuisanceMod {
	return PublicreportNotifyEmailNuisanceModFunc(func(ctx context.Context, o *PublicreportNotifyEmailNuisanceTemplate) {
		o.r.EmailAddressEmailContact = &publicreportNotifyEmailNuisanceREmailAddressEmailContactR{
			o: rel,
		}
	})
}

func (m publicreportNotifyEmailNuisanceMods) WithNewEmailAddressEmailContact(mods ...CommsEmailContactMod) PublicreportNotifyEmailNuisanceMod {
	return PublicreportNotifyEmailNuisanceModFunc(func(ctx context.Context, o *PublicreportNotifyEmailNuisanceTemplate) {
		related := o.f.NewCommsEmailContactWithContext(ctx, mods...)

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

func (m publicreportNotifyEmailNuisanceMods) WithExistingEmailAddressEmailContact(em *models.CommsEmailContact) PublicreportNotifyEmailNuisanceMod {
	return PublicreportNotifyEmailNuisanceModFunc(func(ctx context.Context, o *PublicreportNotifyEmailNuisanceTemplate) {
		o.r.EmailAddressEmailContact = &publicreportNotifyEmailNuisanceREmailAddressEmailContactR{
			o: o.f.FromExistingCommsEmailContact(em),
		}
	})
}

func (m publicreportNotifyEmailNuisanceMods) WithoutEmailAddressEmailContact() PublicreportNotifyEmailNuisanceMod {
	return PublicreportNotifyEmailNuisanceModFunc(func(ctx context.Context, o *PublicreportNotifyEmailNuisanceTemplate) {
		o.r.EmailAddressEmailContact = nil
	})
}

func (m publicreportNotifyEmailNuisanceMods) WithNuisance(rel *PublicreportNuisanceTemplate) PublicreportNotifyEmailNuisanceMod {
	return PublicreportNotifyEmailNuisanceModFunc(func(ctx context.Context, o *PublicreportNotifyEmailNuisanceTemplate) {
		o.r.Nuisance = &publicreportNotifyEmailNuisanceRNuisanceR{
			o: rel,
		}
	})
}

func (m publicreportNotifyEmailNuisanceMods) WithNewNuisance(mods ...PublicreportNuisanceMod) PublicreportNotifyEmailNuisanceMod {
	return PublicreportNotifyEmailNuisanceModFunc(func(ctx context.Context, o *PublicreportNotifyEmailNuisanceTemplate) {
		related := o.f.NewPublicreportNuisanceWithContext(ctx, mods...)

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

func (m publicreportNotifyEmailNuisanceMods) WithExistingNuisance(em *models.PublicreportNuisance) PublicreportNotifyEmailNuisanceMod {
	return PublicreportNotifyEmailNuisanceModFunc(func(ctx context.Context, o *PublicreportNotifyEmailNuisanceTemplate) {
		o.r.Nuisance = &publicreportNotifyEmailNuisanceRNuisanceR{
			o: o.f.FromExistingPublicreportNuisance(em),
		}
	})
}

func (m publicreportNotifyEmailNuisanceMods) WithoutNuisance() PublicreportNotifyEmailNuisanceMod {
	return PublicreportNotifyEmailNuisanceModFunc(func(ctx context.Context, o *PublicreportNotifyEmailNuisanceTemplate) {
		o.r.Nuisance = nil
	})
}
