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

type DistrictSubscriptionPhoneModFunc func(context.Context, *DistrictSubscriptionPhoneTemplate)

func (f DistrictSubscriptionPhoneModFunc) Apply(ctx context.Context, n *DistrictSubscriptionPhoneTemplate) {
	f(ctx, n)
}

type DistrictSubscriptionPhoneModSlice []DistrictSubscriptionPhoneMod

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

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

	r districtSubscriptionPhoneR
	f *Factory

	alreadyPersisted bool
}

type districtSubscriptionPhoneR struct {
	Organization   *districtSubscriptionPhoneROrganizationR
	PhoneE164Phone *districtSubscriptionPhoneRPhoneE164PhoneR
}

type districtSubscriptionPhoneROrganizationR struct {
	o *OrganizationTemplate
}
type districtSubscriptionPhoneRPhoneE164PhoneR struct {
	o *CommsPhoneTemplate
}

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

// setModelRels creates and sets the relationships on *models.DistrictSubscriptionPhone
// according to the relationships in the template. Nothing is inserted into the db
func (t DistrictSubscriptionPhoneTemplate) setModelRels(o *models.DistrictSubscriptionPhone) {
	if t.r.Organization != nil {
		rel := t.r.Organization.o.Build()
		o.OrganizationID = rel.ID // h2
		o.R.Organization = rel
	}

	if t.r.PhoneE164Phone != nil {
		rel := t.r.PhoneE164Phone.o.Build()
		o.PhoneE164 = rel.E164 // h2
		o.R.PhoneE164Phone = rel
	}
}

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

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

	return m
}

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

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

	return m
}

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

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

	o.setModelRels(m)

	return m
}

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

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

	return m
}

func ensureCreatableDistrictSubscriptionPhone(m *models.DistrictSubscriptionPhoneSetter) {
	if !(m.OrganizationID.IsValue()) {
		val := random_int32(nil)
		m.OrganizationID = 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.DistrictSubscriptionPhone
// according to the relationships in the template.
// any required relationship should have already exist on the model
func (o *DistrictSubscriptionPhoneTemplate) insertOptRels(ctx context.Context, exec bob.Executor, m *models.DistrictSubscriptionPhone) error {
	var err error

	return err
}

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

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

	var rel0 *models.Organization

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

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

	if o.r.PhoneE164Phone == nil {
		DistrictSubscriptionPhoneMods.WithNewPhoneE164Phone().Apply(ctx, o)
	}

	var rel1 *models.CommsPhone

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

	opt.PhoneE164 = omit.From(rel1.E164)

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

	m.R.Organization = rel0
	m.R.PhoneE164Phone = rel1

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

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

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

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

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

	return m, nil
}

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

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

// DistrictSubscriptionPhone has methods that act as mods for the DistrictSubscriptionPhoneTemplate
var DistrictSubscriptionPhoneMods districtSubscriptionPhoneMods

type districtSubscriptionPhoneMods struct{}

func (m districtSubscriptionPhoneMods) RandomizeAllColumns(f *faker.Faker) DistrictSubscriptionPhoneMod {
	return DistrictSubscriptionPhoneModSlice{
		DistrictSubscriptionPhoneMods.RandomOrganizationID(f),
		DistrictSubscriptionPhoneMods.RandomPhoneE164(f),
	}
}

// Set the model columns to this value
func (m districtSubscriptionPhoneMods) OrganizationID(val int32) DistrictSubscriptionPhoneMod {
	return DistrictSubscriptionPhoneModFunc(func(_ context.Context, o *DistrictSubscriptionPhoneTemplate) {
		o.OrganizationID = func() int32 { return val }
	})
}

// Set the Column from the function
func (m districtSubscriptionPhoneMods) OrganizationIDFunc(f func() int32) DistrictSubscriptionPhoneMod {
	return DistrictSubscriptionPhoneModFunc(func(_ context.Context, o *DistrictSubscriptionPhoneTemplate) {
		o.OrganizationID = f
	})
}

// Clear any values for the column
func (m districtSubscriptionPhoneMods) UnsetOrganizationID() DistrictSubscriptionPhoneMod {
	return DistrictSubscriptionPhoneModFunc(func(_ context.Context, o *DistrictSubscriptionPhoneTemplate) {
		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 districtSubscriptionPhoneMods) RandomOrganizationID(f *faker.Faker) DistrictSubscriptionPhoneMod {
	return DistrictSubscriptionPhoneModFunc(func(_ context.Context, o *DistrictSubscriptionPhoneTemplate) {
		o.OrganizationID = func() int32 {
			return random_int32(f)
		}
	})
}

// Set the model columns to this value
func (m districtSubscriptionPhoneMods) PhoneE164(val string) DistrictSubscriptionPhoneMod {
	return DistrictSubscriptionPhoneModFunc(func(_ context.Context, o *DistrictSubscriptionPhoneTemplate) {
		o.PhoneE164 = func() string { return val }
	})
}

// Set the Column from the function
func (m districtSubscriptionPhoneMods) PhoneE164Func(f func() string) DistrictSubscriptionPhoneMod {
	return DistrictSubscriptionPhoneModFunc(func(_ context.Context, o *DistrictSubscriptionPhoneTemplate) {
		o.PhoneE164 = f
	})
}

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

func (m districtSubscriptionPhoneMods) WithParentsCascading() DistrictSubscriptionPhoneMod {
	return DistrictSubscriptionPhoneModFunc(func(ctx context.Context, o *DistrictSubscriptionPhoneTemplate) {
		if isDone, _ := districtSubscriptionPhoneWithParentsCascadingCtx.Value(ctx); isDone {
			return
		}
		ctx = districtSubscriptionPhoneWithParentsCascadingCtx.WithValue(ctx, true)
		{

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

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

func (m districtSubscriptionPhoneMods) WithOrganization(rel *OrganizationTemplate) DistrictSubscriptionPhoneMod {
	return DistrictSubscriptionPhoneModFunc(func(ctx context.Context, o *DistrictSubscriptionPhoneTemplate) {
		o.r.Organization = &districtSubscriptionPhoneROrganizationR{
			o: rel,
		}
	})
}

func (m districtSubscriptionPhoneMods) WithNewOrganization(mods ...OrganizationMod) DistrictSubscriptionPhoneMod {
	return DistrictSubscriptionPhoneModFunc(func(ctx context.Context, o *DistrictSubscriptionPhoneTemplate) {
		related := o.f.NewOrganizationWithContext(ctx, mods...)

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

func (m districtSubscriptionPhoneMods) WithExistingOrganization(em *models.Organization) DistrictSubscriptionPhoneMod {
	return DistrictSubscriptionPhoneModFunc(func(ctx context.Context, o *DistrictSubscriptionPhoneTemplate) {
		o.r.Organization = &districtSubscriptionPhoneROrganizationR{
			o: o.f.FromExistingOrganization(em),
		}
	})
}

func (m districtSubscriptionPhoneMods) WithoutOrganization() DistrictSubscriptionPhoneMod {
	return DistrictSubscriptionPhoneModFunc(func(ctx context.Context, o *DistrictSubscriptionPhoneTemplate) {
		o.r.Organization = nil
	})
}

func (m districtSubscriptionPhoneMods) WithPhoneE164Phone(rel *CommsPhoneTemplate) DistrictSubscriptionPhoneMod {
	return DistrictSubscriptionPhoneModFunc(func(ctx context.Context, o *DistrictSubscriptionPhoneTemplate) {
		o.r.PhoneE164Phone = &districtSubscriptionPhoneRPhoneE164PhoneR{
			o: rel,
		}
	})
}

func (m districtSubscriptionPhoneMods) WithNewPhoneE164Phone(mods ...CommsPhoneMod) DistrictSubscriptionPhoneMod {
	return DistrictSubscriptionPhoneModFunc(func(ctx context.Context, o *DistrictSubscriptionPhoneTemplate) {
		related := o.f.NewCommsPhoneWithContext(ctx, mods...)

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

func (m districtSubscriptionPhoneMods) WithExistingPhoneE164Phone(em *models.CommsPhone) DistrictSubscriptionPhoneMod {
	return DistrictSubscriptionPhoneModFunc(func(ctx context.Context, o *DistrictSubscriptionPhoneTemplate) {
		o.r.PhoneE164Phone = &districtSubscriptionPhoneRPhoneE164PhoneR{
			o: o.f.FromExistingCommsPhone(em),
		}
	})
}

func (m districtSubscriptionPhoneMods) WithoutPhoneE164Phone() DistrictSubscriptionPhoneMod {
	return DistrictSubscriptionPhoneModFunc(func(ctx context.Context, o *DistrictSubscriptionPhoneTemplate) {
		o.r.PhoneE164Phone = nil
	})
}
