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

type ComplianceReportRequestMod interface {
	Apply(context.Context, *ComplianceReportRequestTemplate)
}

type ComplianceReportRequestModFunc func(context.Context, *ComplianceReportRequestTemplate)

func (f ComplianceReportRequestModFunc) Apply(ctx context.Context, n *ComplianceReportRequestTemplate) {
	f(ctx, n)
}

type ComplianceReportRequestModSlice []ComplianceReportRequestMod

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

// ComplianceReportRequestTemplate is an object representing the database table.
// all columns are optional and should be set by mods
type ComplianceReportRequestTemplate struct {
	Created     func() time.Time
	Creator     func() int32
	ID          func() int32
	PublicID    func() string
	SiteID      func() int32
	SiteVersion func() int32

	r complianceReportRequestR
	f *Factory

	alreadyPersisted bool
}

type complianceReportRequestR struct {
	CreatorUser *complianceReportRequestRCreatorUserR
	Site        *complianceReportRequestRSiteR
	Mailers     []*complianceReportRequestRMailersR
}

type complianceReportRequestRCreatorUserR struct {
	o *UserTemplate
}
type complianceReportRequestRSiteR struct {
	o *SiteTemplate
}
type complianceReportRequestRMailersR struct {
	number int
	o      *CommsMailerTemplate
}

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

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

	if t.r.Site != nil {
		rel := t.r.Site.o.Build()
		rel.R.ComplianceReportRequests = append(rel.R.ComplianceReportRequests, o)
		o.SiteID = rel.ID           // h2
		o.SiteVersion = rel.Version // h2
		o.R.Site = rel
	}

	if t.r.Mailers != nil {
		rel := models.CommsMailerSlice{}
		for _, r := range t.r.Mailers {
			related := r.o.BuildMany(r.number)
			for _, rel := range related {
				rel.R.ComplianceReportRequests = append(rel.R.ComplianceReportRequests, o)
			}
			rel = append(rel, related...)
		}
		o.R.Mailers = rel
	}
}

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

	if o.Created != nil {
		val := o.Created()
		m.Created = omit.From(val)
	}
	if o.Creator != nil {
		val := o.Creator()
		m.Creator = omit.From(val)
	}
	if o.ID != nil {
		val := o.ID()
		m.ID = omit.From(val)
	}
	if o.PublicID != nil {
		val := o.PublicID()
		m.PublicID = omit.From(val)
	}
	if o.SiteID != nil {
		val := o.SiteID()
		m.SiteID = omit.From(val)
	}
	if o.SiteVersion != nil {
		val := o.SiteVersion()
		m.SiteVersion = omit.From(val)
	}

	return m
}

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

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

	return m
}

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

	if o.Created != nil {
		m.Created = o.Created()
	}
	if o.Creator != nil {
		m.Creator = o.Creator()
	}
	if o.ID != nil {
		m.ID = o.ID()
	}
	if o.PublicID != nil {
		m.PublicID = o.PublicID()
	}
	if o.SiteID != nil {
		m.SiteID = o.SiteID()
	}
	if o.SiteVersion != nil {
		m.SiteVersion = o.SiteVersion()
	}

	o.setModelRels(m)

	return m
}

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

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

	return m
}

func ensureCreatableComplianceReportRequest(m *models.ComplianceReportRequestSetter) {
	if !(m.Created.IsValue()) {
		val := random_time_Time(nil)
		m.Created = omit.From(val)
	}
	if !(m.Creator.IsValue()) {
		val := random_int32(nil)
		m.Creator = omit.From(val)
	}
	if !(m.PublicID.IsValue()) {
		val := random_string(nil)
		m.PublicID = omit.From(val)
	}
	if !(m.SiteID.IsValue()) {
		val := random_int32(nil)
		m.SiteID = omit.From(val)
	}
	if !(m.SiteVersion.IsValue()) {
		val := random_int32(nil)
		m.SiteVersion = omit.From(val)
	}
}

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

	return err
}

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

	if o.r.CreatorUser == nil {
		ComplianceReportRequestMods.WithNewCreatorUser().Apply(ctx, o)
	}

	var rel0 *models.User

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

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

	if o.r.Site == nil {
		ComplianceReportRequestMods.WithNewSite().Apply(ctx, o)
	}

	var rel1 *models.Site

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

	opt.SiteID = omit.From(rel1.ID)
	opt.SiteVersion = omit.From(rel1.Version)

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

	m.R.CreatorUser = rel0
	m.R.Site = rel1

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

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

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

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

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

	return m, nil
}

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

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

// ComplianceReportRequest has methods that act as mods for the ComplianceReportRequestTemplate
var ComplianceReportRequestMods complianceReportRequestMods

type complianceReportRequestMods struct{}

func (m complianceReportRequestMods) RandomizeAllColumns(f *faker.Faker) ComplianceReportRequestMod {
	return ComplianceReportRequestModSlice{
		ComplianceReportRequestMods.RandomCreated(f),
		ComplianceReportRequestMods.RandomCreator(f),
		ComplianceReportRequestMods.RandomID(f),
		ComplianceReportRequestMods.RandomPublicID(f),
		ComplianceReportRequestMods.RandomSiteID(f),
		ComplianceReportRequestMods.RandomSiteVersion(f),
	}
}

// Set the model columns to this value
func (m complianceReportRequestMods) Created(val time.Time) ComplianceReportRequestMod {
	return ComplianceReportRequestModFunc(func(_ context.Context, o *ComplianceReportRequestTemplate) {
		o.Created = func() time.Time { return val }
	})
}

// Set the Column from the function
func (m complianceReportRequestMods) CreatedFunc(f func() time.Time) ComplianceReportRequestMod {
	return ComplianceReportRequestModFunc(func(_ context.Context, o *ComplianceReportRequestTemplate) {
		o.Created = f
	})
}

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

// Set the model columns to this value
func (m complianceReportRequestMods) Creator(val int32) ComplianceReportRequestMod {
	return ComplianceReportRequestModFunc(func(_ context.Context, o *ComplianceReportRequestTemplate) {
		o.Creator = func() int32 { return val }
	})
}

// Set the Column from the function
func (m complianceReportRequestMods) CreatorFunc(f func() int32) ComplianceReportRequestMod {
	return ComplianceReportRequestModFunc(func(_ context.Context, o *ComplianceReportRequestTemplate) {
		o.Creator = f
	})
}

// Clear any values for the column
func (m complianceReportRequestMods) UnsetCreator() ComplianceReportRequestMod {
	return ComplianceReportRequestModFunc(func(_ context.Context, o *ComplianceReportRequestTemplate) {
		o.Creator = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m complianceReportRequestMods) RandomCreator(f *faker.Faker) ComplianceReportRequestMod {
	return ComplianceReportRequestModFunc(func(_ context.Context, o *ComplianceReportRequestTemplate) {
		o.Creator = func() int32 {
			return random_int32(f)
		}
	})
}

// Set the model columns to this value
func (m complianceReportRequestMods) ID(val int32) ComplianceReportRequestMod {
	return ComplianceReportRequestModFunc(func(_ context.Context, o *ComplianceReportRequestTemplate) {
		o.ID = func() int32 { return val }
	})
}

// Set the Column from the function
func (m complianceReportRequestMods) IDFunc(f func() int32) ComplianceReportRequestMod {
	return ComplianceReportRequestModFunc(func(_ context.Context, o *ComplianceReportRequestTemplate) {
		o.ID = f
	})
}

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

// Set the model columns to this value
func (m complianceReportRequestMods) PublicID(val string) ComplianceReportRequestMod {
	return ComplianceReportRequestModFunc(func(_ context.Context, o *ComplianceReportRequestTemplate) {
		o.PublicID = func() string { return val }
	})
}

// Set the Column from the function
func (m complianceReportRequestMods) PublicIDFunc(f func() string) ComplianceReportRequestMod {
	return ComplianceReportRequestModFunc(func(_ context.Context, o *ComplianceReportRequestTemplate) {
		o.PublicID = f
	})
}

// Clear any values for the column
func (m complianceReportRequestMods) UnsetPublicID() ComplianceReportRequestMod {
	return ComplianceReportRequestModFunc(func(_ context.Context, o *ComplianceReportRequestTemplate) {
		o.PublicID = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m complianceReportRequestMods) RandomPublicID(f *faker.Faker) ComplianceReportRequestMod {
	return ComplianceReportRequestModFunc(func(_ context.Context, o *ComplianceReportRequestTemplate) {
		o.PublicID = func() string {
			return random_string(f)
		}
	})
}

// Set the model columns to this value
func (m complianceReportRequestMods) SiteID(val int32) ComplianceReportRequestMod {
	return ComplianceReportRequestModFunc(func(_ context.Context, o *ComplianceReportRequestTemplate) {
		o.SiteID = func() int32 { return val }
	})
}

// Set the Column from the function
func (m complianceReportRequestMods) SiteIDFunc(f func() int32) ComplianceReportRequestMod {
	return ComplianceReportRequestModFunc(func(_ context.Context, o *ComplianceReportRequestTemplate) {
		o.SiteID = f
	})
}

// Clear any values for the column
func (m complianceReportRequestMods) UnsetSiteID() ComplianceReportRequestMod {
	return ComplianceReportRequestModFunc(func(_ context.Context, o *ComplianceReportRequestTemplate) {
		o.SiteID = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m complianceReportRequestMods) RandomSiteID(f *faker.Faker) ComplianceReportRequestMod {
	return ComplianceReportRequestModFunc(func(_ context.Context, o *ComplianceReportRequestTemplate) {
		o.SiteID = func() int32 {
			return random_int32(f)
		}
	})
}

// Set the model columns to this value
func (m complianceReportRequestMods) SiteVersion(val int32) ComplianceReportRequestMod {
	return ComplianceReportRequestModFunc(func(_ context.Context, o *ComplianceReportRequestTemplate) {
		o.SiteVersion = func() int32 { return val }
	})
}

// Set the Column from the function
func (m complianceReportRequestMods) SiteVersionFunc(f func() int32) ComplianceReportRequestMod {
	return ComplianceReportRequestModFunc(func(_ context.Context, o *ComplianceReportRequestTemplate) {
		o.SiteVersion = f
	})
}

// Clear any values for the column
func (m complianceReportRequestMods) UnsetSiteVersion() ComplianceReportRequestMod {
	return ComplianceReportRequestModFunc(func(_ context.Context, o *ComplianceReportRequestTemplate) {
		o.SiteVersion = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m complianceReportRequestMods) RandomSiteVersion(f *faker.Faker) ComplianceReportRequestMod {
	return ComplianceReportRequestModFunc(func(_ context.Context, o *ComplianceReportRequestTemplate) {
		o.SiteVersion = func() int32 {
			return random_int32(f)
		}
	})
}

func (m complianceReportRequestMods) WithParentsCascading() ComplianceReportRequestMod {
	return ComplianceReportRequestModFunc(func(ctx context.Context, o *ComplianceReportRequestTemplate) {
		if isDone, _ := complianceReportRequestWithParentsCascadingCtx.Value(ctx); isDone {
			return
		}
		ctx = complianceReportRequestWithParentsCascadingCtx.WithValue(ctx, true)
		{

			related := o.f.NewUserWithContext(ctx, UserMods.WithParentsCascading())
			m.WithCreatorUser(related).Apply(ctx, o)
		}
		{

			related := o.f.NewSiteWithContext(ctx, SiteMods.WithParentsCascading())
			m.WithSite(related).Apply(ctx, o)
		}
	})
}

func (m complianceReportRequestMods) WithCreatorUser(rel *UserTemplate) ComplianceReportRequestMod {
	return ComplianceReportRequestModFunc(func(ctx context.Context, o *ComplianceReportRequestTemplate) {
		o.r.CreatorUser = &complianceReportRequestRCreatorUserR{
			o: rel,
		}
	})
}

func (m complianceReportRequestMods) WithNewCreatorUser(mods ...UserMod) ComplianceReportRequestMod {
	return ComplianceReportRequestModFunc(func(ctx context.Context, o *ComplianceReportRequestTemplate) {
		related := o.f.NewUserWithContext(ctx, mods...)

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

func (m complianceReportRequestMods) WithExistingCreatorUser(em *models.User) ComplianceReportRequestMod {
	return ComplianceReportRequestModFunc(func(ctx context.Context, o *ComplianceReportRequestTemplate) {
		o.r.CreatorUser = &complianceReportRequestRCreatorUserR{
			o: o.f.FromExistingUser(em),
		}
	})
}

func (m complianceReportRequestMods) WithoutCreatorUser() ComplianceReportRequestMod {
	return ComplianceReportRequestModFunc(func(ctx context.Context, o *ComplianceReportRequestTemplate) {
		o.r.CreatorUser = nil
	})
}

func (m complianceReportRequestMods) WithSite(rel *SiteTemplate) ComplianceReportRequestMod {
	return ComplianceReportRequestModFunc(func(ctx context.Context, o *ComplianceReportRequestTemplate) {
		o.r.Site = &complianceReportRequestRSiteR{
			o: rel,
		}
	})
}

func (m complianceReportRequestMods) WithNewSite(mods ...SiteMod) ComplianceReportRequestMod {
	return ComplianceReportRequestModFunc(func(ctx context.Context, o *ComplianceReportRequestTemplate) {
		related := o.f.NewSiteWithContext(ctx, mods...)

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

func (m complianceReportRequestMods) WithExistingSite(em *models.Site) ComplianceReportRequestMod {
	return ComplianceReportRequestModFunc(func(ctx context.Context, o *ComplianceReportRequestTemplate) {
		o.r.Site = &complianceReportRequestRSiteR{
			o: o.f.FromExistingSite(em),
		}
	})
}

func (m complianceReportRequestMods) WithoutSite() ComplianceReportRequestMod {
	return ComplianceReportRequestModFunc(func(ctx context.Context, o *ComplianceReportRequestTemplate) {
		o.r.Site = nil
	})
}

func (m complianceReportRequestMods) WithMailers(number int, related *CommsMailerTemplate) ComplianceReportRequestMod {
	return ComplianceReportRequestModFunc(func(ctx context.Context, o *ComplianceReportRequestTemplate) {
		o.r.Mailers = []*complianceReportRequestRMailersR{{
			number: number,
			o:      related,
		}}
	})
}

func (m complianceReportRequestMods) WithNewMailers(number int, mods ...CommsMailerMod) ComplianceReportRequestMod {
	return ComplianceReportRequestModFunc(func(ctx context.Context, o *ComplianceReportRequestTemplate) {
		related := o.f.NewCommsMailerWithContext(ctx, mods...)
		m.WithMailers(number, related).Apply(ctx, o)
	})
}

func (m complianceReportRequestMods) AddMailers(number int, related *CommsMailerTemplate) ComplianceReportRequestMod {
	return ComplianceReportRequestModFunc(func(ctx context.Context, o *ComplianceReportRequestTemplate) {
		o.r.Mailers = append(o.r.Mailers, &complianceReportRequestRMailersR{
			number: number,
			o:      related,
		})
	})
}

func (m complianceReportRequestMods) AddNewMailers(number int, mods ...CommsMailerMod) ComplianceReportRequestMod {
	return ComplianceReportRequestModFunc(func(ctx context.Context, o *ComplianceReportRequestTemplate) {
		related := o.f.NewCommsMailerWithContext(ctx, mods...)
		m.AddMailers(number, related).Apply(ctx, o)
	})
}

func (m complianceReportRequestMods) AddExistingMailers(existingModels ...*models.CommsMailer) ComplianceReportRequestMod {
	return ComplianceReportRequestModFunc(func(ctx context.Context, o *ComplianceReportRequestTemplate) {
		for _, em := range existingModels {
			o.r.Mailers = append(o.r.Mailers, &complianceReportRequestRMailersR{
				o: o.f.FromExistingCommsMailer(em),
			})
		}
	})
}

func (m complianceReportRequestMods) WithoutMailers() ComplianceReportRequestMod {
	return ComplianceReportRequestModFunc(func(ctx context.Context, o *ComplianceReportRequestTemplate) {
		o.r.Mailers = nil
	})
}
