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

type PublicreportSubscribePhoneModFunc func(context.Context, *PublicreportSubscribePhoneTemplate)

func (f PublicreportSubscribePhoneModFunc) Apply(ctx context.Context, n *PublicreportSubscribePhoneTemplate) {
	f(ctx, n)
}

type PublicreportSubscribePhoneModSlice []PublicreportSubscribePhoneMod

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

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

	r publicreportSubscribePhoneR
	f *Factory

	alreadyPersisted bool
}

type publicreportSubscribePhoneR struct {
	PhoneE164Phone *publicreportSubscribePhoneRPhoneE164PhoneR
}

type publicreportSubscribePhoneRPhoneE164PhoneR struct {
	o *CommsPhoneTemplate
}

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

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

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

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

	return m
}

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

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

	return m
}

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

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

	o.setModelRels(m)

	return m
}

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

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

	return m
}

func ensureCreatablePublicreportSubscribePhone(m *models.PublicreportSubscribePhoneSetter) {
	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)
	}
}

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

	return err
}

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

	if o.r.PhoneE164Phone == nil {
		PublicreportSubscribePhoneMods.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)

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

	m.R.PhoneE164Phone = rel0

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

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

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

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

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

	return m, nil
}

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

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

// PublicreportSubscribePhone has methods that act as mods for the PublicreportSubscribePhoneTemplate
var PublicreportSubscribePhoneMods publicreportSubscribePhoneMods

type publicreportSubscribePhoneMods struct{}

func (m publicreportSubscribePhoneMods) RandomizeAllColumns(f *faker.Faker) PublicreportSubscribePhoneMod {
	return PublicreportSubscribePhoneModSlice{
		PublicreportSubscribePhoneMods.RandomCreated(f),
		PublicreportSubscribePhoneMods.RandomDeleted(f),
		PublicreportSubscribePhoneMods.RandomID(f),
		PublicreportSubscribePhoneMods.RandomPhoneE164(f),
	}
}

// Set the model columns to this value
func (m publicreportSubscribePhoneMods) Created(val time.Time) PublicreportSubscribePhoneMod {
	return PublicreportSubscribePhoneModFunc(func(_ context.Context, o *PublicreportSubscribePhoneTemplate) {
		o.Created = func() time.Time { return val }
	})
}

// Set the Column from the function
func (m publicreportSubscribePhoneMods) CreatedFunc(f func() time.Time) PublicreportSubscribePhoneMod {
	return PublicreportSubscribePhoneModFunc(func(_ context.Context, o *PublicreportSubscribePhoneTemplate) {
		o.Created = f
	})
}

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

// Set the model columns to this value
func (m publicreportSubscribePhoneMods) Deleted(val null.Val[time.Time]) PublicreportSubscribePhoneMod {
	return PublicreportSubscribePhoneModFunc(func(_ context.Context, o *PublicreportSubscribePhoneTemplate) {
		o.Deleted = func() null.Val[time.Time] { return val }
	})
}

// Set the Column from the function
func (m publicreportSubscribePhoneMods) DeletedFunc(f func() null.Val[time.Time]) PublicreportSubscribePhoneMod {
	return PublicreportSubscribePhoneModFunc(func(_ context.Context, o *PublicreportSubscribePhoneTemplate) {
		o.Deleted = f
	})
}

// Clear any values for the column
func (m publicreportSubscribePhoneMods) UnsetDeleted() PublicreportSubscribePhoneMod {
	return PublicreportSubscribePhoneModFunc(func(_ context.Context, o *PublicreportSubscribePhoneTemplate) {
		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 publicreportSubscribePhoneMods) RandomDeleted(f *faker.Faker) PublicreportSubscribePhoneMod {
	return PublicreportSubscribePhoneModFunc(func(_ context.Context, o *PublicreportSubscribePhoneTemplate) {
		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 publicreportSubscribePhoneMods) RandomDeletedNotNull(f *faker.Faker) PublicreportSubscribePhoneMod {
	return PublicreportSubscribePhoneModFunc(func(_ context.Context, o *PublicreportSubscribePhoneTemplate) {
		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 publicreportSubscribePhoneMods) ID(val int32) PublicreportSubscribePhoneMod {
	return PublicreportSubscribePhoneModFunc(func(_ context.Context, o *PublicreportSubscribePhoneTemplate) {
		o.ID = func() int32 { return val }
	})
}

// Set the Column from the function
func (m publicreportSubscribePhoneMods) IDFunc(f func() int32) PublicreportSubscribePhoneMod {
	return PublicreportSubscribePhoneModFunc(func(_ context.Context, o *PublicreportSubscribePhoneTemplate) {
		o.ID = f
	})
}

// Clear any values for the column
func (m publicreportSubscribePhoneMods) UnsetID() PublicreportSubscribePhoneMod {
	return PublicreportSubscribePhoneModFunc(func(_ context.Context, o *PublicreportSubscribePhoneTemplate) {
		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 publicreportSubscribePhoneMods) RandomID(f *faker.Faker) PublicreportSubscribePhoneMod {
	return PublicreportSubscribePhoneModFunc(func(_ context.Context, o *PublicreportSubscribePhoneTemplate) {
		o.ID = func() int32 {
			return random_int32(f)
		}
	})
}

// Set the model columns to this value
func (m publicreportSubscribePhoneMods) PhoneE164(val string) PublicreportSubscribePhoneMod {
	return PublicreportSubscribePhoneModFunc(func(_ context.Context, o *PublicreportSubscribePhoneTemplate) {
		o.PhoneE164 = func() string { return val }
	})
}

// Set the Column from the function
func (m publicreportSubscribePhoneMods) PhoneE164Func(f func() string) PublicreportSubscribePhoneMod {
	return PublicreportSubscribePhoneModFunc(func(_ context.Context, o *PublicreportSubscribePhoneTemplate) {
		o.PhoneE164 = f
	})
}

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

func (m publicreportSubscribePhoneMods) WithParentsCascading() PublicreportSubscribePhoneMod {
	return PublicreportSubscribePhoneModFunc(func(ctx context.Context, o *PublicreportSubscribePhoneTemplate) {
		if isDone, _ := publicreportSubscribePhoneWithParentsCascadingCtx.Value(ctx); isDone {
			return
		}
		ctx = publicreportSubscribePhoneWithParentsCascadingCtx.WithValue(ctx, true)
		{

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

func (m publicreportSubscribePhoneMods) WithPhoneE164Phone(rel *CommsPhoneTemplate) PublicreportSubscribePhoneMod {
	return PublicreportSubscribePhoneModFunc(func(ctx context.Context, o *PublicreportSubscribePhoneTemplate) {
		o.r.PhoneE164Phone = &publicreportSubscribePhoneRPhoneE164PhoneR{
			o: rel,
		}
	})
}

func (m publicreportSubscribePhoneMods) WithNewPhoneE164Phone(mods ...CommsPhoneMod) PublicreportSubscribePhoneMod {
	return PublicreportSubscribePhoneModFunc(func(ctx context.Context, o *PublicreportSubscribePhoneTemplate) {
		related := o.f.NewCommsPhoneWithContext(ctx, mods...)

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

func (m publicreportSubscribePhoneMods) WithExistingPhoneE164Phone(em *models.CommsPhone) PublicreportSubscribePhoneMod {
	return PublicreportSubscribePhoneModFunc(func(ctx context.Context, o *PublicreportSubscribePhoneTemplate) {
		o.r.PhoneE164Phone = &publicreportSubscribePhoneRPhoneE164PhoneR{
			o: o.f.FromExistingCommsPhone(em),
		}
	})
}

func (m publicreportSubscribePhoneMods) WithoutPhoneE164Phone() PublicreportSubscribePhoneMod {
	return PublicreportSubscribePhoneModFunc(func(ctx context.Context, o *PublicreportSubscribePhoneTemplate) {
		o.r.PhoneE164Phone = nil
	})
}
