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

type ArcgisParcelMappingModFunc func(context.Context, *ArcgisParcelMappingTemplate)

func (f ArcgisParcelMappingModFunc) Apply(ctx context.Context, n *ArcgisParcelMappingTemplate) {
	f(ctx, n)
}

type ArcgisParcelMappingModSlice []ArcgisParcelMappingMod

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

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

	r arcgisParcelMappingR
	f *Factory

	alreadyPersisted bool
}

type arcgisParcelMappingR struct {
	LayerField   *arcgisParcelMappingRLayerFieldR
	Organization *arcgisParcelMappingROrganizationR
}

type arcgisParcelMappingRLayerFieldR struct {
	o *ArcgisLayerFieldTemplate
}
type arcgisParcelMappingROrganizationR struct {
	o *OrganizationTemplate
}

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

// setModelRels creates and sets the relationships on *models.ArcgisParcelMapping
// according to the relationships in the template. Nothing is inserted into the db
func (t ArcgisParcelMappingTemplate) setModelRels(o *models.ArcgisParcelMapping) {
	if t.r.LayerField != nil {
		rel := t.r.LayerField.o.Build()
		rel.R.ParcelMappings = append(rel.R.ParcelMappings, 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.ParcelMappings = append(rel.R.ParcelMappings, o)
		o.OrganizationID = rel.ID // h2
		o.R.Organization = rel
	}
}

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

	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.ArcgisParcelMappingSetter
// this does nothing with the relationship templates
func (o ArcgisParcelMappingTemplate) BuildManySetter(number int) []*models.ArcgisParcelMappingSetter {
	m := make([]*models.ArcgisParcelMappingSetter, number)

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

	return m
}

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

	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.ArcgisParcelMappingSlice
// Related objects are also created and placed in the .R field
// NOTE: Objects are not inserted into the database. Use ArcgisParcelMappingTemplate.CreateMany
func (o ArcgisParcelMappingTemplate) BuildMany(number int) models.ArcgisParcelMappingSlice {
	m := make(models.ArcgisParcelMappingSlice, number)

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

	return m
}

func ensureCreatableArcgisParcelMapping(m *models.ArcgisParcelMappingSetter) {
	if !(m.Destination.IsValue()) {
		val := random_enums_ArcgisMappingdestinationparcel(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.ArcgisParcelMapping
// according to the relationships in the template.
// any required relationship should have already exist on the model
func (o *ArcgisParcelMappingTemplate) insertOptRels(ctx context.Context, exec bob.Executor, m *models.ArcgisParcelMapping) error {
	var err error

	return err
}

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

	if o.r.LayerField == nil {
		ArcgisParcelMappingMods.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 {
		ArcgisParcelMappingMods.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.ArcgisParcelMappings.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 arcgisParcelMapping and inserts it into the database
// Relations objects are also inserted and placed in the .R field
// panics if an error occurs
func (o *ArcgisParcelMappingTemplate) MustCreate(ctx context.Context, exec bob.Executor) *models.ArcgisParcelMapping {
	m, err := o.Create(ctx, exec)
	if err != nil {
		panic(err)
	}
	return m
}

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

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

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

	return m, nil
}

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

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

// ArcgisParcelMapping has methods that act as mods for the ArcgisParcelMappingTemplate
var ArcgisParcelMappingMods arcgisParcelMappingMods

type arcgisParcelMappingMods struct{}

func (m arcgisParcelMappingMods) RandomizeAllColumns(f *faker.Faker) ArcgisParcelMappingMod {
	return ArcgisParcelMappingModSlice{
		ArcgisParcelMappingMods.RandomDestination(f),
		ArcgisParcelMappingMods.RandomLayerFeatureServiceItemID(f),
		ArcgisParcelMappingMods.RandomLayerIndex(f),
		ArcgisParcelMappingMods.RandomLayerFieldName(f),
		ArcgisParcelMappingMods.RandomOrganizationID(f),
	}
}

// Set the model columns to this value
func (m arcgisParcelMappingMods) Destination(val enums.ArcgisMappingdestinationparcel) ArcgisParcelMappingMod {
	return ArcgisParcelMappingModFunc(func(_ context.Context, o *ArcgisParcelMappingTemplate) {
		o.Destination = func() enums.ArcgisMappingdestinationparcel { return val }
	})
}

// Set the Column from the function
func (m arcgisParcelMappingMods) DestinationFunc(f func() enums.ArcgisMappingdestinationparcel) ArcgisParcelMappingMod {
	return ArcgisParcelMappingModFunc(func(_ context.Context, o *ArcgisParcelMappingTemplate) {
		o.Destination = f
	})
}

// Clear any values for the column
func (m arcgisParcelMappingMods) UnsetDestination() ArcgisParcelMappingMod {
	return ArcgisParcelMappingModFunc(func(_ context.Context, o *ArcgisParcelMappingTemplate) {
		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 arcgisParcelMappingMods) RandomDestination(f *faker.Faker) ArcgisParcelMappingMod {
	return ArcgisParcelMappingModFunc(func(_ context.Context, o *ArcgisParcelMappingTemplate) {
		o.Destination = func() enums.ArcgisMappingdestinationparcel {
			return random_enums_ArcgisMappingdestinationparcel(f)
		}
	})
}

// Set the model columns to this value
func (m arcgisParcelMappingMods) LayerFeatureServiceItemID(val string) ArcgisParcelMappingMod {
	return ArcgisParcelMappingModFunc(func(_ context.Context, o *ArcgisParcelMappingTemplate) {
		o.LayerFeatureServiceItemID = func() string { return val }
	})
}

// Set the Column from the function
func (m arcgisParcelMappingMods) LayerFeatureServiceItemIDFunc(f func() string) ArcgisParcelMappingMod {
	return ArcgisParcelMappingModFunc(func(_ context.Context, o *ArcgisParcelMappingTemplate) {
		o.LayerFeatureServiceItemID = f
	})
}

// Clear any values for the column
func (m arcgisParcelMappingMods) UnsetLayerFeatureServiceItemID() ArcgisParcelMappingMod {
	return ArcgisParcelMappingModFunc(func(_ context.Context, o *ArcgisParcelMappingTemplate) {
		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 arcgisParcelMappingMods) RandomLayerFeatureServiceItemID(f *faker.Faker) ArcgisParcelMappingMod {
	return ArcgisParcelMappingModFunc(func(_ context.Context, o *ArcgisParcelMappingTemplate) {
		o.LayerFeatureServiceItemID = func() string {
			return random_string(f)
		}
	})
}

// Set the model columns to this value
func (m arcgisParcelMappingMods) LayerIndex(val int32) ArcgisParcelMappingMod {
	return ArcgisParcelMappingModFunc(func(_ context.Context, o *ArcgisParcelMappingTemplate) {
		o.LayerIndex = func() int32 { return val }
	})
}

// Set the Column from the function
func (m arcgisParcelMappingMods) LayerIndexFunc(f func() int32) ArcgisParcelMappingMod {
	return ArcgisParcelMappingModFunc(func(_ context.Context, o *ArcgisParcelMappingTemplate) {
		o.LayerIndex = f
	})
}

// Clear any values for the column
func (m arcgisParcelMappingMods) UnsetLayerIndex() ArcgisParcelMappingMod {
	return ArcgisParcelMappingModFunc(func(_ context.Context, o *ArcgisParcelMappingTemplate) {
		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 arcgisParcelMappingMods) RandomLayerIndex(f *faker.Faker) ArcgisParcelMappingMod {
	return ArcgisParcelMappingModFunc(func(_ context.Context, o *ArcgisParcelMappingTemplate) {
		o.LayerIndex = func() int32 {
			return random_int32(f)
		}
	})
}

// Set the model columns to this value
func (m arcgisParcelMappingMods) LayerFieldName(val string) ArcgisParcelMappingMod {
	return ArcgisParcelMappingModFunc(func(_ context.Context, o *ArcgisParcelMappingTemplate) {
		o.LayerFieldName = func() string { return val }
	})
}

// Set the Column from the function
func (m arcgisParcelMappingMods) LayerFieldNameFunc(f func() string) ArcgisParcelMappingMod {
	return ArcgisParcelMappingModFunc(func(_ context.Context, o *ArcgisParcelMappingTemplate) {
		o.LayerFieldName = f
	})
}

// Clear any values for the column
func (m arcgisParcelMappingMods) UnsetLayerFieldName() ArcgisParcelMappingMod {
	return ArcgisParcelMappingModFunc(func(_ context.Context, o *ArcgisParcelMappingTemplate) {
		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 arcgisParcelMappingMods) RandomLayerFieldName(f *faker.Faker) ArcgisParcelMappingMod {
	return ArcgisParcelMappingModFunc(func(_ context.Context, o *ArcgisParcelMappingTemplate) {
		o.LayerFieldName = func() string {
			return random_string(f)
		}
	})
}

// Set the model columns to this value
func (m arcgisParcelMappingMods) OrganizationID(val int32) ArcgisParcelMappingMod {
	return ArcgisParcelMappingModFunc(func(_ context.Context, o *ArcgisParcelMappingTemplate) {
		o.OrganizationID = func() int32 { return val }
	})
}

// Set the Column from the function
func (m arcgisParcelMappingMods) OrganizationIDFunc(f func() int32) ArcgisParcelMappingMod {
	return ArcgisParcelMappingModFunc(func(_ context.Context, o *ArcgisParcelMappingTemplate) {
		o.OrganizationID = f
	})
}

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

func (m arcgisParcelMappingMods) WithParentsCascading() ArcgisParcelMappingMod {
	return ArcgisParcelMappingModFunc(func(ctx context.Context, o *ArcgisParcelMappingTemplate) {
		if isDone, _ := arcgisParcelMappingWithParentsCascadingCtx.Value(ctx); isDone {
			return
		}
		ctx = arcgisParcelMappingWithParentsCascadingCtx.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 arcgisParcelMappingMods) WithLayerField(rel *ArcgisLayerFieldTemplate) ArcgisParcelMappingMod {
	return ArcgisParcelMappingModFunc(func(ctx context.Context, o *ArcgisParcelMappingTemplate) {
		o.r.LayerField = &arcgisParcelMappingRLayerFieldR{
			o: rel,
		}
	})
}

func (m arcgisParcelMappingMods) WithNewLayerField(mods ...ArcgisLayerFieldMod) ArcgisParcelMappingMod {
	return ArcgisParcelMappingModFunc(func(ctx context.Context, o *ArcgisParcelMappingTemplate) {
		related := o.f.NewArcgisLayerFieldWithContext(ctx, mods...)

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

func (m arcgisParcelMappingMods) WithExistingLayerField(em *models.ArcgisLayerField) ArcgisParcelMappingMod {
	return ArcgisParcelMappingModFunc(func(ctx context.Context, o *ArcgisParcelMappingTemplate) {
		o.r.LayerField = &arcgisParcelMappingRLayerFieldR{
			o: o.f.FromExistingArcgisLayerField(em),
		}
	})
}

func (m arcgisParcelMappingMods) WithoutLayerField() ArcgisParcelMappingMod {
	return ArcgisParcelMappingModFunc(func(ctx context.Context, o *ArcgisParcelMappingTemplate) {
		o.r.LayerField = nil
	})
}

func (m arcgisParcelMappingMods) WithOrganization(rel *OrganizationTemplate) ArcgisParcelMappingMod {
	return ArcgisParcelMappingModFunc(func(ctx context.Context, o *ArcgisParcelMappingTemplate) {
		o.r.Organization = &arcgisParcelMappingROrganizationR{
			o: rel,
		}
	})
}

func (m arcgisParcelMappingMods) WithNewOrganization(mods ...OrganizationMod) ArcgisParcelMappingMod {
	return ArcgisParcelMappingModFunc(func(ctx context.Context, o *ArcgisParcelMappingTemplate) {
		related := o.f.NewOrganizationWithContext(ctx, mods...)

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

func (m arcgisParcelMappingMods) WithExistingOrganization(em *models.Organization) ArcgisParcelMappingMod {
	return ArcgisParcelMappingModFunc(func(ctx context.Context, o *ArcgisParcelMappingTemplate) {
		o.r.Organization = &arcgisParcelMappingROrganizationR{
			o: o.f.FromExistingOrganization(em),
		}
	})
}

func (m arcgisParcelMappingMods) WithoutOrganization() ArcgisParcelMappingMod {
	return ArcgisParcelMappingModFunc(func(ctx context.Context, o *ArcgisParcelMappingTemplate) {
		o.r.Organization = nil
	})
}
