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

type ArcgisAddressMappingModFunc func(context.Context, *ArcgisAddressMappingTemplate)

func (f ArcgisAddressMappingModFunc) Apply(ctx context.Context, n *ArcgisAddressMappingTemplate) {
	f(ctx, n)
}

type ArcgisAddressMappingModSlice []ArcgisAddressMappingMod

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

// ArcgisAddressMappingTemplate is an object representing the database table.
// all columns are optional and should be set by mods
type ArcgisAddressMappingTemplate struct {
	Destination               func() enums.ArcgisMappingdestinationaddress
	LayerFeatureServiceItemID func() string
	LayerIndex                func() int32
	LayerFieldName            func() string
	OrganizationID            func() int32

	r arcgisAddressMappingR
	f *Factory

	alreadyPersisted bool
}

type arcgisAddressMappingR struct {
	LayerField   *arcgisAddressMappingRLayerFieldR
	Organization *arcgisAddressMappingROrganizationR
}

type arcgisAddressMappingRLayerFieldR struct {
	o *ArcgisLayerFieldTemplate
}
type arcgisAddressMappingROrganizationR struct {
	o *OrganizationTemplate
}

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

// setModelRels creates and sets the relationships on *models.ArcgisAddressMapping
// according to the relationships in the template. Nothing is inserted into the db
func (t ArcgisAddressMappingTemplate) setModelRels(o *models.ArcgisAddressMapping) {
	if t.r.LayerField != nil {
		rel := t.r.LayerField.o.Build()
		rel.R.AddressMappings = append(rel.R.AddressMappings, o)
		o.LayerFeatureServiceItemID = rel.LayerFeatureServiceItemID // h2
		o.LayerIndex = rel.LayerIndex                               // h2
		o.LayerFieldName = rel.Name                                 // h2
		o.R.LayerField = rel
	}

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

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

	if o.Destination != nil {
		val := o.Destination()
		m.Destination = omit.From(val)
	}
	if o.LayerFeatureServiceItemID != nil {
		val := o.LayerFeatureServiceItemID()
		m.LayerFeatureServiceItemID = omit.From(val)
	}
	if o.LayerIndex != nil {
		val := o.LayerIndex()
		m.LayerIndex = omit.From(val)
	}
	if o.LayerFieldName != nil {
		val := o.LayerFieldName()
		m.LayerFieldName = omit.From(val)
	}
	if o.OrganizationID != nil {
		val := o.OrganizationID()
		m.OrganizationID = omit.From(val)
	}

	return m
}

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

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

	return m
}

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

	if o.Destination != nil {
		m.Destination = o.Destination()
	}
	if o.LayerFeatureServiceItemID != nil {
		m.LayerFeatureServiceItemID = o.LayerFeatureServiceItemID()
	}
	if o.LayerIndex != nil {
		m.LayerIndex = o.LayerIndex()
	}
	if o.LayerFieldName != nil {
		m.LayerFieldName = o.LayerFieldName()
	}
	if o.OrganizationID != nil {
		m.OrganizationID = o.OrganizationID()
	}

	o.setModelRels(m)

	return m
}

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

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

	return m
}

func ensureCreatableArcgisAddressMapping(m *models.ArcgisAddressMappingSetter) {
	if !(m.Destination.IsValue()) {
		val := random_enums_ArcgisMappingdestinationaddress(nil)
		m.Destination = omit.From(val)
	}
	if !(m.LayerFeatureServiceItemID.IsValue()) {
		val := random_string(nil)
		m.LayerFeatureServiceItemID = omit.From(val)
	}
	if !(m.LayerIndex.IsValue()) {
		val := random_int32(nil)
		m.LayerIndex = omit.From(val)
	}
	if !(m.LayerFieldName.IsValue()) {
		val := random_string(nil)
		m.LayerFieldName = omit.From(val)
	}
	if !(m.OrganizationID.IsValue()) {
		val := random_int32(nil)
		m.OrganizationID = omit.From(val)
	}
}

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

	return err
}

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

	if o.r.LayerField == nil {
		ArcgisAddressMappingMods.WithNewLayerField().Apply(ctx, o)
	}

	var rel0 *models.ArcgisLayerField

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

	opt.LayerFeatureServiceItemID = omit.From(rel0.LayerFeatureServiceItemID)
	opt.LayerIndex = omit.From(rel0.LayerIndex)
	opt.LayerFieldName = omit.From(rel0.Name)

	if o.r.Organization == nil {
		ArcgisAddressMappingMods.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.ArcgisAddressMappings.Insert(opt).One(ctx, exec)
	if err != nil {
		return nil, err
	}

	m.R.LayerField = rel0
	m.R.Organization = rel1

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

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

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

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

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

	return m, nil
}

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

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

// ArcgisAddressMapping has methods that act as mods for the ArcgisAddressMappingTemplate
var ArcgisAddressMappingMods arcgisAddressMappingMods

type arcgisAddressMappingMods struct{}

func (m arcgisAddressMappingMods) RandomizeAllColumns(f *faker.Faker) ArcgisAddressMappingMod {
	return ArcgisAddressMappingModSlice{
		ArcgisAddressMappingMods.RandomDestination(f),
		ArcgisAddressMappingMods.RandomLayerFeatureServiceItemID(f),
		ArcgisAddressMappingMods.RandomLayerIndex(f),
		ArcgisAddressMappingMods.RandomLayerFieldName(f),
		ArcgisAddressMappingMods.RandomOrganizationID(f),
	}
}

// Set the model columns to this value
func (m arcgisAddressMappingMods) Destination(val enums.ArcgisMappingdestinationaddress) ArcgisAddressMappingMod {
	return ArcgisAddressMappingModFunc(func(_ context.Context, o *ArcgisAddressMappingTemplate) {
		o.Destination = func() enums.ArcgisMappingdestinationaddress { return val }
	})
}

// Set the Column from the function
func (m arcgisAddressMappingMods) DestinationFunc(f func() enums.ArcgisMappingdestinationaddress) ArcgisAddressMappingMod {
	return ArcgisAddressMappingModFunc(func(_ context.Context, o *ArcgisAddressMappingTemplate) {
		o.Destination = f
	})
}

// Clear any values for the column
func (m arcgisAddressMappingMods) UnsetDestination() ArcgisAddressMappingMod {
	return ArcgisAddressMappingModFunc(func(_ context.Context, o *ArcgisAddressMappingTemplate) {
		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 arcgisAddressMappingMods) RandomDestination(f *faker.Faker) ArcgisAddressMappingMod {
	return ArcgisAddressMappingModFunc(func(_ context.Context, o *ArcgisAddressMappingTemplate) {
		o.Destination = func() enums.ArcgisMappingdestinationaddress {
			return random_enums_ArcgisMappingdestinationaddress(f)
		}
	})
}

// Set the model columns to this value
func (m arcgisAddressMappingMods) LayerFeatureServiceItemID(val string) ArcgisAddressMappingMod {
	return ArcgisAddressMappingModFunc(func(_ context.Context, o *ArcgisAddressMappingTemplate) {
		o.LayerFeatureServiceItemID = func() string { return val }
	})
}

// Set the Column from the function
func (m arcgisAddressMappingMods) LayerFeatureServiceItemIDFunc(f func() string) ArcgisAddressMappingMod {
	return ArcgisAddressMappingModFunc(func(_ context.Context, o *ArcgisAddressMappingTemplate) {
		o.LayerFeatureServiceItemID = f
	})
}

// Clear any values for the column
func (m arcgisAddressMappingMods) UnsetLayerFeatureServiceItemID() ArcgisAddressMappingMod {
	return ArcgisAddressMappingModFunc(func(_ context.Context, o *ArcgisAddressMappingTemplate) {
		o.LayerFeatureServiceItemID = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m arcgisAddressMappingMods) RandomLayerFeatureServiceItemID(f *faker.Faker) ArcgisAddressMappingMod {
	return ArcgisAddressMappingModFunc(func(_ context.Context, o *ArcgisAddressMappingTemplate) {
		o.LayerFeatureServiceItemID = func() string {
			return random_string(f)
		}
	})
}

// Set the model columns to this value
func (m arcgisAddressMappingMods) LayerIndex(val int32) ArcgisAddressMappingMod {
	return ArcgisAddressMappingModFunc(func(_ context.Context, o *ArcgisAddressMappingTemplate) {
		o.LayerIndex = func() int32 { return val }
	})
}

// Set the Column from the function
func (m arcgisAddressMappingMods) LayerIndexFunc(f func() int32) ArcgisAddressMappingMod {
	return ArcgisAddressMappingModFunc(func(_ context.Context, o *ArcgisAddressMappingTemplate) {
		o.LayerIndex = f
	})
}

// Clear any values for the column
func (m arcgisAddressMappingMods) UnsetLayerIndex() ArcgisAddressMappingMod {
	return ArcgisAddressMappingModFunc(func(_ context.Context, o *ArcgisAddressMappingTemplate) {
		o.LayerIndex = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m arcgisAddressMappingMods) RandomLayerIndex(f *faker.Faker) ArcgisAddressMappingMod {
	return ArcgisAddressMappingModFunc(func(_ context.Context, o *ArcgisAddressMappingTemplate) {
		o.LayerIndex = func() int32 {
			return random_int32(f)
		}
	})
}

// Set the model columns to this value
func (m arcgisAddressMappingMods) LayerFieldName(val string) ArcgisAddressMappingMod {
	return ArcgisAddressMappingModFunc(func(_ context.Context, o *ArcgisAddressMappingTemplate) {
		o.LayerFieldName = func() string { return val }
	})
}

// Set the Column from the function
func (m arcgisAddressMappingMods) LayerFieldNameFunc(f func() string) ArcgisAddressMappingMod {
	return ArcgisAddressMappingModFunc(func(_ context.Context, o *ArcgisAddressMappingTemplate) {
		o.LayerFieldName = f
	})
}

// Clear any values for the column
func (m arcgisAddressMappingMods) UnsetLayerFieldName() ArcgisAddressMappingMod {
	return ArcgisAddressMappingModFunc(func(_ context.Context, o *ArcgisAddressMappingTemplate) {
		o.LayerFieldName = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m arcgisAddressMappingMods) RandomLayerFieldName(f *faker.Faker) ArcgisAddressMappingMod {
	return ArcgisAddressMappingModFunc(func(_ context.Context, o *ArcgisAddressMappingTemplate) {
		o.LayerFieldName = func() string {
			return random_string(f)
		}
	})
}

// Set the model columns to this value
func (m arcgisAddressMappingMods) OrganizationID(val int32) ArcgisAddressMappingMod {
	return ArcgisAddressMappingModFunc(func(_ context.Context, o *ArcgisAddressMappingTemplate) {
		o.OrganizationID = func() int32 { return val }
	})
}

// Set the Column from the function
func (m arcgisAddressMappingMods) OrganizationIDFunc(f func() int32) ArcgisAddressMappingMod {
	return ArcgisAddressMappingModFunc(func(_ context.Context, o *ArcgisAddressMappingTemplate) {
		o.OrganizationID = f
	})
}

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

func (m arcgisAddressMappingMods) WithParentsCascading() ArcgisAddressMappingMod {
	return ArcgisAddressMappingModFunc(func(ctx context.Context, o *ArcgisAddressMappingTemplate) {
		if isDone, _ := arcgisAddressMappingWithParentsCascadingCtx.Value(ctx); isDone {
			return
		}
		ctx = arcgisAddressMappingWithParentsCascadingCtx.WithValue(ctx, true)
		{

			related := o.f.NewArcgisLayerFieldWithContext(ctx, ArcgisLayerFieldMods.WithParentsCascading())
			m.WithLayerField(related).Apply(ctx, o)
		}
		{

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

func (m arcgisAddressMappingMods) WithLayerField(rel *ArcgisLayerFieldTemplate) ArcgisAddressMappingMod {
	return ArcgisAddressMappingModFunc(func(ctx context.Context, o *ArcgisAddressMappingTemplate) {
		o.r.LayerField = &arcgisAddressMappingRLayerFieldR{
			o: rel,
		}
	})
}

func (m arcgisAddressMappingMods) WithNewLayerField(mods ...ArcgisLayerFieldMod) ArcgisAddressMappingMod {
	return ArcgisAddressMappingModFunc(func(ctx context.Context, o *ArcgisAddressMappingTemplate) {
		related := o.f.NewArcgisLayerFieldWithContext(ctx, mods...)

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

func (m arcgisAddressMappingMods) WithExistingLayerField(em *models.ArcgisLayerField) ArcgisAddressMappingMod {
	return ArcgisAddressMappingModFunc(func(ctx context.Context, o *ArcgisAddressMappingTemplate) {
		o.r.LayerField = &arcgisAddressMappingRLayerFieldR{
			o: o.f.FromExistingArcgisLayerField(em),
		}
	})
}

func (m arcgisAddressMappingMods) WithoutLayerField() ArcgisAddressMappingMod {
	return ArcgisAddressMappingModFunc(func(ctx context.Context, o *ArcgisAddressMappingTemplate) {
		o.r.LayerField = nil
	})
}

func (m arcgisAddressMappingMods) WithOrganization(rel *OrganizationTemplate) ArcgisAddressMappingMod {
	return ArcgisAddressMappingModFunc(func(ctx context.Context, o *ArcgisAddressMappingTemplate) {
		o.r.Organization = &arcgisAddressMappingROrganizationR{
			o: rel,
		}
	})
}

func (m arcgisAddressMappingMods) WithNewOrganization(mods ...OrganizationMod) ArcgisAddressMappingMod {
	return ArcgisAddressMappingModFunc(func(ctx context.Context, o *ArcgisAddressMappingTemplate) {
		related := o.f.NewOrganizationWithContext(ctx, mods...)

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

func (m arcgisAddressMappingMods) WithExistingOrganization(em *models.Organization) ArcgisAddressMappingMod {
	return ArcgisAddressMappingModFunc(func(ctx context.Context, o *ArcgisAddressMappingTemplate) {
		o.r.Organization = &arcgisAddressMappingROrganizationR{
			o: o.f.FromExistingOrganization(em),
		}
	})
}

func (m arcgisAddressMappingMods) WithoutOrganization() ArcgisAddressMappingMod {
	return ArcgisAddressMappingModFunc(func(ctx context.Context, o *ArcgisAddressMappingTemplate) {
		o.r.Organization = nil
	})
}
