// 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"
	enums "github.com/Gleipnir-Technology/nidus-sync/db/enums"
	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 CommsTextJobMod interface {
	Apply(context.Context, *CommsTextJobTemplate)
}

type CommsTextJobModFunc func(context.Context, *CommsTextJobTemplate)

func (f CommsTextJobModFunc) Apply(ctx context.Context, n *CommsTextJobTemplate) {
	f(ctx, n)
}

type CommsTextJobModSlice []CommsTextJobMod

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

// CommsTextJobTemplate is an object representing the database table.
// all columns are optional and should be set by mods
type CommsTextJobTemplate struct {
	Content     func() string
	Created     func() time.Time
	Destination func() string
	ID          func() int32
	Type        func() enums.CommsTextjobtype
	Source      func() enums.CommsTextjobsource
	Completed   func() null.Val[time.Time]

	r commsTextJobR
	f *Factory

	alreadyPersisted bool
}

type commsTextJobR struct {
	DestinationPhone *commsTextJobRDestinationPhoneR
}

type commsTextJobRDestinationPhoneR struct {
	o *CommsPhoneTemplate
}

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

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

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

	if o.Content != nil {
		val := o.Content()
		m.Content = omit.From(val)
	}
	if o.Created != nil {
		val := o.Created()
		m.Created = omit.From(val)
	}
	if o.Destination != nil {
		val := o.Destination()
		m.Destination = omit.From(val)
	}
	if o.ID != nil {
		val := o.ID()
		m.ID = omit.From(val)
	}
	if o.Type != nil {
		val := o.Type()
		m.Type = omit.From(val)
	}
	if o.Source != nil {
		val := o.Source()
		m.Source = omit.From(val)
	}
	if o.Completed != nil {
		val := o.Completed()
		m.Completed = omitnull.FromNull(val)
	}

	return m
}

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

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

	return m
}

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

	if o.Content != nil {
		m.Content = o.Content()
	}
	if o.Created != nil {
		m.Created = o.Created()
	}
	if o.Destination != nil {
		m.Destination = o.Destination()
	}
	if o.ID != nil {
		m.ID = o.ID()
	}
	if o.Type != nil {
		m.Type = o.Type()
	}
	if o.Source != nil {
		m.Source = o.Source()
	}
	if o.Completed != nil {
		m.Completed = o.Completed()
	}

	o.setModelRels(m)

	return m
}

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

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

	return m
}

func ensureCreatableCommsTextJob(m *models.CommsTextJobSetter) {
	if !(m.Content.IsValue()) {
		val := random_string(nil)
		m.Content = omit.From(val)
	}
	if !(m.Created.IsValue()) {
		val := random_time_Time(nil)
		m.Created = omit.From(val)
	}
	if !(m.Destination.IsValue()) {
		val := random_string(nil)
		m.Destination = omit.From(val)
	}
	if !(m.Type.IsValue()) {
		val := random_enums_CommsTextjobtype(nil)
		m.Type = omit.From(val)
	}
	if !(m.Source.IsValue()) {
		val := random_enums_CommsTextjobsource(nil)
		m.Source = omit.From(val)
	}
}

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

	return err
}

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

	if o.r.DestinationPhone == nil {
		CommsTextJobMods.WithNewDestinationPhone().Apply(ctx, o)
	}

	var rel0 *models.CommsPhone

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

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

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

	m.R.DestinationPhone = rel0

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

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

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

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

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

	return m, nil
}

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

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

// CommsTextJob has methods that act as mods for the CommsTextJobTemplate
var CommsTextJobMods commsTextJobMods

type commsTextJobMods struct{}

func (m commsTextJobMods) RandomizeAllColumns(f *faker.Faker) CommsTextJobMod {
	return CommsTextJobModSlice{
		CommsTextJobMods.RandomContent(f),
		CommsTextJobMods.RandomCreated(f),
		CommsTextJobMods.RandomDestination(f),
		CommsTextJobMods.RandomID(f),
		CommsTextJobMods.RandomType(f),
		CommsTextJobMods.RandomSource(f),
		CommsTextJobMods.RandomCompleted(f),
	}
}

// Set the model columns to this value
func (m commsTextJobMods) Content(val string) CommsTextJobMod {
	return CommsTextJobModFunc(func(_ context.Context, o *CommsTextJobTemplate) {
		o.Content = func() string { return val }
	})
}

// Set the Column from the function
func (m commsTextJobMods) ContentFunc(f func() string) CommsTextJobMod {
	return CommsTextJobModFunc(func(_ context.Context, o *CommsTextJobTemplate) {
		o.Content = f
	})
}

// Clear any values for the column
func (m commsTextJobMods) UnsetContent() CommsTextJobMod {
	return CommsTextJobModFunc(func(_ context.Context, o *CommsTextJobTemplate) {
		o.Content = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m commsTextJobMods) RandomContent(f *faker.Faker) CommsTextJobMod {
	return CommsTextJobModFunc(func(_ context.Context, o *CommsTextJobTemplate) {
		o.Content = func() string {
			return random_string(f)
		}
	})
}

// Set the model columns to this value
func (m commsTextJobMods) Created(val time.Time) CommsTextJobMod {
	return CommsTextJobModFunc(func(_ context.Context, o *CommsTextJobTemplate) {
		o.Created = func() time.Time { return val }
	})
}

// Set the Column from the function
func (m commsTextJobMods) CreatedFunc(f func() time.Time) CommsTextJobMod {
	return CommsTextJobModFunc(func(_ context.Context, o *CommsTextJobTemplate) {
		o.Created = f
	})
}

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

// Set the model columns to this value
func (m commsTextJobMods) Destination(val string) CommsTextJobMod {
	return CommsTextJobModFunc(func(_ context.Context, o *CommsTextJobTemplate) {
		o.Destination = func() string { return val }
	})
}

// Set the Column from the function
func (m commsTextJobMods) DestinationFunc(f func() string) CommsTextJobMod {
	return CommsTextJobModFunc(func(_ context.Context, o *CommsTextJobTemplate) {
		o.Destination = f
	})
}

// Clear any values for the column
func (m commsTextJobMods) UnsetDestination() CommsTextJobMod {
	return CommsTextJobModFunc(func(_ context.Context, o *CommsTextJobTemplate) {
		o.Destination = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m commsTextJobMods) RandomDestination(f *faker.Faker) CommsTextJobMod {
	return CommsTextJobModFunc(func(_ context.Context, o *CommsTextJobTemplate) {
		o.Destination = func() string {
			return random_string(f)
		}
	})
}

// Set the model columns to this value
func (m commsTextJobMods) ID(val int32) CommsTextJobMod {
	return CommsTextJobModFunc(func(_ context.Context, o *CommsTextJobTemplate) {
		o.ID = func() int32 { return val }
	})
}

// Set the Column from the function
func (m commsTextJobMods) IDFunc(f func() int32) CommsTextJobMod {
	return CommsTextJobModFunc(func(_ context.Context, o *CommsTextJobTemplate) {
		o.ID = f
	})
}

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

// Set the model columns to this value
func (m commsTextJobMods) Type(val enums.CommsTextjobtype) CommsTextJobMod {
	return CommsTextJobModFunc(func(_ context.Context, o *CommsTextJobTemplate) {
		o.Type = func() enums.CommsTextjobtype { return val }
	})
}

// Set the Column from the function
func (m commsTextJobMods) TypeFunc(f func() enums.CommsTextjobtype) CommsTextJobMod {
	return CommsTextJobModFunc(func(_ context.Context, o *CommsTextJobTemplate) {
		o.Type = f
	})
}

// Clear any values for the column
func (m commsTextJobMods) UnsetType() CommsTextJobMod {
	return CommsTextJobModFunc(func(_ context.Context, o *CommsTextJobTemplate) {
		o.Type = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m commsTextJobMods) RandomType(f *faker.Faker) CommsTextJobMod {
	return CommsTextJobModFunc(func(_ context.Context, o *CommsTextJobTemplate) {
		o.Type = func() enums.CommsTextjobtype {
			return random_enums_CommsTextjobtype(f)
		}
	})
}

// Set the model columns to this value
func (m commsTextJobMods) Source(val enums.CommsTextjobsource) CommsTextJobMod {
	return CommsTextJobModFunc(func(_ context.Context, o *CommsTextJobTemplate) {
		o.Source = func() enums.CommsTextjobsource { return val }
	})
}

// Set the Column from the function
func (m commsTextJobMods) SourceFunc(f func() enums.CommsTextjobsource) CommsTextJobMod {
	return CommsTextJobModFunc(func(_ context.Context, o *CommsTextJobTemplate) {
		o.Source = f
	})
}

// Clear any values for the column
func (m commsTextJobMods) UnsetSource() CommsTextJobMod {
	return CommsTextJobModFunc(func(_ context.Context, o *CommsTextJobTemplate) {
		o.Source = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m commsTextJobMods) RandomSource(f *faker.Faker) CommsTextJobMod {
	return CommsTextJobModFunc(func(_ context.Context, o *CommsTextJobTemplate) {
		o.Source = func() enums.CommsTextjobsource {
			return random_enums_CommsTextjobsource(f)
		}
	})
}

// Set the model columns to this value
func (m commsTextJobMods) Completed(val null.Val[time.Time]) CommsTextJobMod {
	return CommsTextJobModFunc(func(_ context.Context, o *CommsTextJobTemplate) {
		o.Completed = func() null.Val[time.Time] { return val }
	})
}

// Set the Column from the function
func (m commsTextJobMods) CompletedFunc(f func() null.Val[time.Time]) CommsTextJobMod {
	return CommsTextJobModFunc(func(_ context.Context, o *CommsTextJobTemplate) {
		o.Completed = f
	})
}

// Clear any values for the column
func (m commsTextJobMods) UnsetCompleted() CommsTextJobMod {
	return CommsTextJobModFunc(func(_ context.Context, o *CommsTextJobTemplate) {
		o.Completed = 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 commsTextJobMods) RandomCompleted(f *faker.Faker) CommsTextJobMod {
	return CommsTextJobModFunc(func(_ context.Context, o *CommsTextJobTemplate) {
		o.Completed = 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 commsTextJobMods) RandomCompletedNotNull(f *faker.Faker) CommsTextJobMod {
	return CommsTextJobModFunc(func(_ context.Context, o *CommsTextJobTemplate) {
		o.Completed = func() null.Val[time.Time] {
			if f == nil {
				f = &defaultFaker
			}

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

func (m commsTextJobMods) WithParentsCascading() CommsTextJobMod {
	return CommsTextJobModFunc(func(ctx context.Context, o *CommsTextJobTemplate) {
		if isDone, _ := commsTextJobWithParentsCascadingCtx.Value(ctx); isDone {
			return
		}
		ctx = commsTextJobWithParentsCascadingCtx.WithValue(ctx, true)
		{

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

func (m commsTextJobMods) WithDestinationPhone(rel *CommsPhoneTemplate) CommsTextJobMod {
	return CommsTextJobModFunc(func(ctx context.Context, o *CommsTextJobTemplate) {
		o.r.DestinationPhone = &commsTextJobRDestinationPhoneR{
			o: rel,
		}
	})
}

func (m commsTextJobMods) WithNewDestinationPhone(mods ...CommsPhoneMod) CommsTextJobMod {
	return CommsTextJobModFunc(func(ctx context.Context, o *CommsTextJobTemplate) {
		related := o.f.NewCommsPhoneWithContext(ctx, mods...)

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

func (m commsTextJobMods) WithExistingDestinationPhone(em *models.CommsPhone) CommsTextJobMod {
	return CommsTextJobModFunc(func(ctx context.Context, o *CommsTextJobTemplate) {
		o.r.DestinationPhone = &commsTextJobRDestinationPhoneR{
			o: o.f.FromExistingCommsPhone(em),
		}
	})
}

func (m commsTextJobMods) WithoutDestinationPhone() CommsTextJobMod {
	return CommsTextJobModFunc(func(ctx context.Context, o *CommsTextJobTemplate) {
		o.r.DestinationPhone = nil
	})
}
