// 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"

	"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 DistrictSubscriptionEmailMod interface {
	Apply(context.Context, *DistrictSubscriptionEmailTemplate)
}

type DistrictSubscriptionEmailModFunc func(context.Context, *DistrictSubscriptionEmailTemplate)

func (f DistrictSubscriptionEmailModFunc) Apply(ctx context.Context, n *DistrictSubscriptionEmailTemplate) {
	f(ctx, n)
}

type DistrictSubscriptionEmailModSlice []DistrictSubscriptionEmailMod

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

// DistrictSubscriptionEmailTemplate is an object representing the database table.
// all columns are optional and should be set by mods
type DistrictSubscriptionEmailTemplate struct {
	OrganizationID      func() int32
	EmailContactAddress func() string

	r districtSubscriptionEmailR
	f *Factory

	alreadyPersisted bool
}

type districtSubscriptionEmailR struct {
	EmailContactAddressEmailContact *districtSubscriptionEmailREmailContactAddressEmailContactR
	Organization                    *districtSubscriptionEmailROrganizationR
}

type districtSubscriptionEmailREmailContactAddressEmailContactR struct {
	o *CommsEmailContactTemplate
}
type districtSubscriptionEmailROrganizationR struct {
	o *OrganizationTemplate
}

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

// setModelRels creates and sets the relationships on *models.DistrictSubscriptionEmail
// according to the relationships in the template. Nothing is inserted into the db
func (t DistrictSubscriptionEmailTemplate) setModelRels(o *models.DistrictSubscriptionEmail) {
	if t.r.EmailContactAddressEmailContact != nil {
		rel := t.r.EmailContactAddressEmailContact.o.Build()
		o.EmailContactAddress = rel.Address // h2
		o.R.EmailContactAddressEmailContact = rel
	}

	if t.r.Organization != nil {
		rel := t.r.Organization.o.Build()
		o.OrganizationID = rel.ID // h2
		o.R.Organization = rel
	}
}

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

	if o.OrganizationID != nil {
		val := o.OrganizationID()
		m.OrganizationID = omit.From(val)
	}
	if o.EmailContactAddress != nil {
		val := o.EmailContactAddress()
		m.EmailContactAddress = omit.From(val)
	}

	return m
}

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

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

	return m
}

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

	if o.OrganizationID != nil {
		m.OrganizationID = o.OrganizationID()
	}
	if o.EmailContactAddress != nil {
		m.EmailContactAddress = o.EmailContactAddress()
	}

	o.setModelRels(m)

	return m
}

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

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

	return m
}

func ensureCreatableDistrictSubscriptionEmail(m *models.DistrictSubscriptionEmailSetter) {
	if !(m.OrganizationID.IsValue()) {
		val := random_int32(nil)
		m.OrganizationID = omit.From(val)
	}
	if !(m.EmailContactAddress.IsValue()) {
		val := random_string(nil)
		m.EmailContactAddress = omit.From(val)
	}
}

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

	return err
}

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

	if o.r.EmailContactAddressEmailContact == nil {
		DistrictSubscriptionEmailMods.WithNewEmailContactAddressEmailContact().Apply(ctx, o)
	}

	var rel0 *models.CommsEmailContact

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

	opt.EmailContactAddress = omit.From(rel0.Address)

	if o.r.Organization == nil {
		DistrictSubscriptionEmailMods.WithNewOrganization().Apply(ctx, o)
	}

	var rel1 *models.Organization

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

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

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

	m.R.EmailContactAddressEmailContact = rel0
	m.R.Organization = rel1

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

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

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

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

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

	return m, nil
}

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

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

// DistrictSubscriptionEmail has methods that act as mods for the DistrictSubscriptionEmailTemplate
var DistrictSubscriptionEmailMods districtSubscriptionEmailMods

type districtSubscriptionEmailMods struct{}

func (m districtSubscriptionEmailMods) RandomizeAllColumns(f *faker.Faker) DistrictSubscriptionEmailMod {
	return DistrictSubscriptionEmailModSlice{
		DistrictSubscriptionEmailMods.RandomOrganizationID(f),
		DistrictSubscriptionEmailMods.RandomEmailContactAddress(f),
	}
}

// Set the model columns to this value
func (m districtSubscriptionEmailMods) OrganizationID(val int32) DistrictSubscriptionEmailMod {
	return DistrictSubscriptionEmailModFunc(func(_ context.Context, o *DistrictSubscriptionEmailTemplate) {
		o.OrganizationID = func() int32 { return val }
	})
}

// Set the Column from the function
func (m districtSubscriptionEmailMods) OrganizationIDFunc(f func() int32) DistrictSubscriptionEmailMod {
	return DistrictSubscriptionEmailModFunc(func(_ context.Context, o *DistrictSubscriptionEmailTemplate) {
		o.OrganizationID = f
	})
}

// Clear any values for the column
func (m districtSubscriptionEmailMods) UnsetOrganizationID() DistrictSubscriptionEmailMod {
	return DistrictSubscriptionEmailModFunc(func(_ context.Context, o *DistrictSubscriptionEmailTemplate) {
		o.OrganizationID = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m districtSubscriptionEmailMods) RandomOrganizationID(f *faker.Faker) DistrictSubscriptionEmailMod {
	return DistrictSubscriptionEmailModFunc(func(_ context.Context, o *DistrictSubscriptionEmailTemplate) {
		o.OrganizationID = func() int32 {
			return random_int32(f)
		}
	})
}

// Set the model columns to this value
func (m districtSubscriptionEmailMods) EmailContactAddress(val string) DistrictSubscriptionEmailMod {
	return DistrictSubscriptionEmailModFunc(func(_ context.Context, o *DistrictSubscriptionEmailTemplate) {
		o.EmailContactAddress = func() string { return val }
	})
}

// Set the Column from the function
func (m districtSubscriptionEmailMods) EmailContactAddressFunc(f func() string) DistrictSubscriptionEmailMod {
	return DistrictSubscriptionEmailModFunc(func(_ context.Context, o *DistrictSubscriptionEmailTemplate) {
		o.EmailContactAddress = f
	})
}

// Clear any values for the column
func (m districtSubscriptionEmailMods) UnsetEmailContactAddress() DistrictSubscriptionEmailMod {
	return DistrictSubscriptionEmailModFunc(func(_ context.Context, o *DistrictSubscriptionEmailTemplate) {
		o.EmailContactAddress = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m districtSubscriptionEmailMods) RandomEmailContactAddress(f *faker.Faker) DistrictSubscriptionEmailMod {
	return DistrictSubscriptionEmailModFunc(func(_ context.Context, o *DistrictSubscriptionEmailTemplate) {
		o.EmailContactAddress = func() string {
			return random_string(f)
		}
	})
}

func (m districtSubscriptionEmailMods) WithParentsCascading() DistrictSubscriptionEmailMod {
	return DistrictSubscriptionEmailModFunc(func(ctx context.Context, o *DistrictSubscriptionEmailTemplate) {
		if isDone, _ := districtSubscriptionEmailWithParentsCascadingCtx.Value(ctx); isDone {
			return
		}
		ctx = districtSubscriptionEmailWithParentsCascadingCtx.WithValue(ctx, true)
		{

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

			related := o.f.NewOrganizationWithContext(ctx, OrganizationMods.WithParentsCascading())
			m.WithOrganization(related).Apply(ctx, o)
		}
	})
}

func (m districtSubscriptionEmailMods) WithEmailContactAddressEmailContact(rel *CommsEmailContactTemplate) DistrictSubscriptionEmailMod {
	return DistrictSubscriptionEmailModFunc(func(ctx context.Context, o *DistrictSubscriptionEmailTemplate) {
		o.r.EmailContactAddressEmailContact = &districtSubscriptionEmailREmailContactAddressEmailContactR{
			o: rel,
		}
	})
}

func (m districtSubscriptionEmailMods) WithNewEmailContactAddressEmailContact(mods ...CommsEmailContactMod) DistrictSubscriptionEmailMod {
	return DistrictSubscriptionEmailModFunc(func(ctx context.Context, o *DistrictSubscriptionEmailTemplate) {
		related := o.f.NewCommsEmailContactWithContext(ctx, mods...)

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

func (m districtSubscriptionEmailMods) WithExistingEmailContactAddressEmailContact(em *models.CommsEmailContact) DistrictSubscriptionEmailMod {
	return DistrictSubscriptionEmailModFunc(func(ctx context.Context, o *DistrictSubscriptionEmailTemplate) {
		o.r.EmailContactAddressEmailContact = &districtSubscriptionEmailREmailContactAddressEmailContactR{
			o: o.f.FromExistingCommsEmailContact(em),
		}
	})
}

func (m districtSubscriptionEmailMods) WithoutEmailContactAddressEmailContact() DistrictSubscriptionEmailMod {
	return DistrictSubscriptionEmailModFunc(func(ctx context.Context, o *DistrictSubscriptionEmailTemplate) {
		o.r.EmailContactAddressEmailContact = nil
	})
}

func (m districtSubscriptionEmailMods) WithOrganization(rel *OrganizationTemplate) DistrictSubscriptionEmailMod {
	return DistrictSubscriptionEmailModFunc(func(ctx context.Context, o *DistrictSubscriptionEmailTemplate) {
		o.r.Organization = &districtSubscriptionEmailROrganizationR{
			o: rel,
		}
	})
}

func (m districtSubscriptionEmailMods) WithNewOrganization(mods ...OrganizationMod) DistrictSubscriptionEmailMod {
	return DistrictSubscriptionEmailModFunc(func(ctx context.Context, o *DistrictSubscriptionEmailTemplate) {
		related := o.f.NewOrganizationWithContext(ctx, mods...)

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

func (m districtSubscriptionEmailMods) WithExistingOrganization(em *models.Organization) DistrictSubscriptionEmailMod {
	return DistrictSubscriptionEmailModFunc(func(ctx context.Context, o *DistrictSubscriptionEmailTemplate) {
		o.r.Organization = &districtSubscriptionEmailROrganizationR{
			o: o.f.FromExistingOrganization(em),
		}
	})
}

func (m districtSubscriptionEmailMods) WithoutOrganization() DistrictSubscriptionEmailMod {
	return DistrictSubscriptionEmailModFunc(func(ctx context.Context, o *DistrictSubscriptionEmailTemplate) {
		o.r.Organization = nil
	})
}
