// 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/omit"
	"github.com/jaswdr/faker/v2"
)

type CommsMailerMod interface {
	Apply(context.Context, *CommsMailerTemplate)
}

type CommsMailerModFunc func(context.Context, *CommsMailerTemplate)

func (f CommsMailerModFunc) Apply(ctx context.Context, n *CommsMailerTemplate) {
	f(ctx, n)
}

type CommsMailerModSlice []CommsMailerMod

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

// CommsMailerTemplate is an object representing the database table.
// all columns are optional and should be set by mods
type CommsMailerTemplate struct {
	Created func() time.Time
	ID      func() int32
	Type    func() enums.CommsMailertype

	f *Factory

	alreadyPersisted bool
}

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

// setModelRels creates and sets the relationships on *models.CommsMailer
// according to the relationships in the template. Nothing is inserted into the db
func (t CommsMailerTemplate) setModelRels(o *models.CommsMailer) {}

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

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

	return m
}

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

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

	return m
}

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

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

	o.setModelRels(m)

	return m
}

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

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

	return m
}

func ensureCreatableCommsMailer(m *models.CommsMailerSetter) {
	if !(m.Created.IsValue()) {
		val := random_time_Time(nil)
		m.Created = omit.From(val)
	}
	if !(m.Type.IsValue()) {
		val := random_enums_CommsMailertype(nil)
		m.Type = omit.From(val)
	}
}

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

	return err
}

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

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

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

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

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

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

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

	return m, nil
}

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

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

// CommsMailer has methods that act as mods for the CommsMailerTemplate
var CommsMailerMods commsMailerMods

type commsMailerMods struct{}

func (m commsMailerMods) RandomizeAllColumns(f *faker.Faker) CommsMailerMod {
	return CommsMailerModSlice{
		CommsMailerMods.RandomCreated(f),
		CommsMailerMods.RandomID(f),
		CommsMailerMods.RandomType(f),
	}
}

// Set the model columns to this value
func (m commsMailerMods) Created(val time.Time) CommsMailerMod {
	return CommsMailerModFunc(func(_ context.Context, o *CommsMailerTemplate) {
		o.Created = func() time.Time { return val }
	})
}

// Set the Column from the function
func (m commsMailerMods) CreatedFunc(f func() time.Time) CommsMailerMod {
	return CommsMailerModFunc(func(_ context.Context, o *CommsMailerTemplate) {
		o.Created = f
	})
}

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

// Set the model columns to this value
func (m commsMailerMods) ID(val int32) CommsMailerMod {
	return CommsMailerModFunc(func(_ context.Context, o *CommsMailerTemplate) {
		o.ID = func() int32 { return val }
	})
}

// Set the Column from the function
func (m commsMailerMods) IDFunc(f func() int32) CommsMailerMod {
	return CommsMailerModFunc(func(_ context.Context, o *CommsMailerTemplate) {
		o.ID = f
	})
}

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

// Set the model columns to this value
func (m commsMailerMods) Type(val enums.CommsMailertype) CommsMailerMod {
	return CommsMailerModFunc(func(_ context.Context, o *CommsMailerTemplate) {
		o.Type = func() enums.CommsMailertype { return val }
	})
}

// Set the Column from the function
func (m commsMailerMods) TypeFunc(f func() enums.CommsMailertype) CommsMailerMod {
	return CommsMailerModFunc(func(_ context.Context, o *CommsMailerTemplate) {
		o.Type = f
	})
}

// Clear any values for the column
func (m commsMailerMods) UnsetType() CommsMailerMod {
	return CommsMailerModFunc(func(_ context.Context, o *CommsMailerTemplate) {
		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 commsMailerMods) RandomType(f *faker.Faker) CommsMailerMod {
	return CommsMailerModFunc(func(_ context.Context, o *CommsMailerTemplate) {
		o.Type = func() enums.CommsMailertype {
			return random_enums_CommsMailertype(f)
		}
	})
}

func (m commsMailerMods) WithParentsCascading() CommsMailerMod {
	return CommsMailerModFunc(func(ctx context.Context, o *CommsMailerTemplate) {
		if isDone, _ := commsMailerWithParentsCascadingCtx.Value(ctx); isDone {
			return
		}
		ctx = commsMailerWithParentsCascadingCtx.WithValue(ctx, true)
	})
}
