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

type ArcgisLayerModFunc func(context.Context, *ArcgisLayerTemplate)

func (f ArcgisLayerModFunc) Apply(ctx context.Context, n *ArcgisLayerTemplate) {
	f(ctx, n)
}

type ArcgisLayerModSlice []ArcgisLayerMod

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

// ArcgisLayerTemplate is an object representing the database table.
// all columns are optional and should be set by mods
type ArcgisLayerTemplate struct {
	Extent               func() string
	FeatureServiceItemID func() string
	Index                func() int32

	r arcgisLayerR
	f *Factory

	alreadyPersisted bool
}

type arcgisLayerR struct {
	FeatureServiceItemServiceFeature *arcgisLayerRFeatureServiceItemServiceFeatureR
	LayerFields                      []*arcgisLayerRLayerFieldsR
}

type arcgisLayerRFeatureServiceItemServiceFeatureR struct {
	o *ArcgisServiceFeatureTemplate
}
type arcgisLayerRLayerFieldsR struct {
	number int
	o      *ArcgisLayerFieldTemplate
}

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

// setModelRels creates and sets the relationships on *models.ArcgisLayer
// according to the relationships in the template. Nothing is inserted into the db
func (t ArcgisLayerTemplate) setModelRels(o *models.ArcgisLayer) {
	if t.r.FeatureServiceItemServiceFeature != nil {
		rel := t.r.FeatureServiceItemServiceFeature.o.Build()
		rel.R.FeatureServiceItemLayers = append(rel.R.FeatureServiceItemLayers, o)
		o.FeatureServiceItemID = rel.ItemID // h2
		o.R.FeatureServiceItemServiceFeature = rel
	}

	if t.r.LayerFields != nil {
		rel := models.ArcgisLayerFieldSlice{}
		for _, r := range t.r.LayerFields {
			related := r.o.BuildMany(r.number)
			for _, rel := range related {
				rel.LayerFeatureServiceItemID = o.FeatureServiceItemID // h2
				rel.LayerIndex = o.Index                               // h2
				rel.R.Layer = o
			}
			rel = append(rel, related...)
		}
		o.R.LayerFields = rel
	}
}

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

	if o.Extent != nil {
		val := o.Extent()
		m.Extent = omit.From(val)
	}
	if o.FeatureServiceItemID != nil {
		val := o.FeatureServiceItemID()
		m.FeatureServiceItemID = omit.From(val)
	}
	if o.Index != nil {
		val := o.Index()
		m.Index = omit.From(val)
	}

	return m
}

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

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

	return m
}

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

	if o.Extent != nil {
		m.Extent = o.Extent()
	}
	if o.FeatureServiceItemID != nil {
		m.FeatureServiceItemID = o.FeatureServiceItemID()
	}
	if o.Index != nil {
		m.Index = o.Index()
	}

	o.setModelRels(m)

	return m
}

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

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

	return m
}

func ensureCreatableArcgisLayer(m *models.ArcgisLayerSetter) {
	if !(m.Extent.IsValue()) {
		val := random_string(nil)
		m.Extent = omit.From(val)
	}
	if !(m.FeatureServiceItemID.IsValue()) {
		val := random_string(nil)
		m.FeatureServiceItemID = omit.From(val)
	}
	if !(m.Index.IsValue()) {
		val := random_int32(nil)
		m.Index = omit.From(val)
	}
}

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

	isLayerFieldsDone, _ := arcgisLayerRelLayerFieldsCtx.Value(ctx)
	if !isLayerFieldsDone && o.r.LayerFields != nil {
		ctx = arcgisLayerRelLayerFieldsCtx.WithValue(ctx, true)
		for _, r := range o.r.LayerFields {
			if r.o.alreadyPersisted {
				m.R.LayerFields = append(m.R.LayerFields, r.o.Build())
			} else {
				rel1, err := r.o.CreateMany(ctx, exec, r.number)
				if err != nil {
					return err
				}

				err = m.AttachLayerFields(ctx, exec, rel1...)
				if err != nil {
					return err
				}
			}
		}
	}

	return err
}

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

	if o.r.FeatureServiceItemServiceFeature == nil {
		ArcgisLayerMods.WithNewFeatureServiceItemServiceFeature().Apply(ctx, o)
	}

	var rel0 *models.ArcgisServiceFeature

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

	opt.FeatureServiceItemID = omit.From(rel0.ItemID)

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

	m.R.FeatureServiceItemServiceFeature = rel0

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

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

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

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

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

	return m, nil
}

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

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

// ArcgisLayer has methods that act as mods for the ArcgisLayerTemplate
var ArcgisLayerMods arcgisLayerMods

type arcgisLayerMods struct{}

func (m arcgisLayerMods) RandomizeAllColumns(f *faker.Faker) ArcgisLayerMod {
	return ArcgisLayerModSlice{
		ArcgisLayerMods.RandomExtent(f),
		ArcgisLayerMods.RandomFeatureServiceItemID(f),
		ArcgisLayerMods.RandomIndex(f),
	}
}

// Set the model columns to this value
func (m arcgisLayerMods) Extent(val string) ArcgisLayerMod {
	return ArcgisLayerModFunc(func(_ context.Context, o *ArcgisLayerTemplate) {
		o.Extent = func() string { return val }
	})
}

// Set the Column from the function
func (m arcgisLayerMods) ExtentFunc(f func() string) ArcgisLayerMod {
	return ArcgisLayerModFunc(func(_ context.Context, o *ArcgisLayerTemplate) {
		o.Extent = f
	})
}

// Clear any values for the column
func (m arcgisLayerMods) UnsetExtent() ArcgisLayerMod {
	return ArcgisLayerModFunc(func(_ context.Context, o *ArcgisLayerTemplate) {
		o.Extent = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m arcgisLayerMods) RandomExtent(f *faker.Faker) ArcgisLayerMod {
	return ArcgisLayerModFunc(func(_ context.Context, o *ArcgisLayerTemplate) {
		o.Extent = func() string {
			return random_string(f)
		}
	})
}

// Set the model columns to this value
func (m arcgisLayerMods) FeatureServiceItemID(val string) ArcgisLayerMod {
	return ArcgisLayerModFunc(func(_ context.Context, o *ArcgisLayerTemplate) {
		o.FeatureServiceItemID = func() string { return val }
	})
}

// Set the Column from the function
func (m arcgisLayerMods) FeatureServiceItemIDFunc(f func() string) ArcgisLayerMod {
	return ArcgisLayerModFunc(func(_ context.Context, o *ArcgisLayerTemplate) {
		o.FeatureServiceItemID = f
	})
}

// Clear any values for the column
func (m arcgisLayerMods) UnsetFeatureServiceItemID() ArcgisLayerMod {
	return ArcgisLayerModFunc(func(_ context.Context, o *ArcgisLayerTemplate) {
		o.FeatureServiceItemID = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m arcgisLayerMods) RandomFeatureServiceItemID(f *faker.Faker) ArcgisLayerMod {
	return ArcgisLayerModFunc(func(_ context.Context, o *ArcgisLayerTemplate) {
		o.FeatureServiceItemID = func() string {
			return random_string(f)
		}
	})
}

// Set the model columns to this value
func (m arcgisLayerMods) Index(val int32) ArcgisLayerMod {
	return ArcgisLayerModFunc(func(_ context.Context, o *ArcgisLayerTemplate) {
		o.Index = func() int32 { return val }
	})
}

// Set the Column from the function
func (m arcgisLayerMods) IndexFunc(f func() int32) ArcgisLayerMod {
	return ArcgisLayerModFunc(func(_ context.Context, o *ArcgisLayerTemplate) {
		o.Index = f
	})
}

// Clear any values for the column
func (m arcgisLayerMods) UnsetIndex() ArcgisLayerMod {
	return ArcgisLayerModFunc(func(_ context.Context, o *ArcgisLayerTemplate) {
		o.Index = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m arcgisLayerMods) RandomIndex(f *faker.Faker) ArcgisLayerMod {
	return ArcgisLayerModFunc(func(_ context.Context, o *ArcgisLayerTemplate) {
		o.Index = func() int32 {
			return random_int32(f)
		}
	})
}

func (m arcgisLayerMods) WithParentsCascading() ArcgisLayerMod {
	return ArcgisLayerModFunc(func(ctx context.Context, o *ArcgisLayerTemplate) {
		if isDone, _ := arcgisLayerWithParentsCascadingCtx.Value(ctx); isDone {
			return
		}
		ctx = arcgisLayerWithParentsCascadingCtx.WithValue(ctx, true)
		{

			related := o.f.NewArcgisServiceFeatureWithContext(ctx, ArcgisServiceFeatureMods.WithParentsCascading())
			m.WithFeatureServiceItemServiceFeature(related).Apply(ctx, o)
		}
	})
}

func (m arcgisLayerMods) WithFeatureServiceItemServiceFeature(rel *ArcgisServiceFeatureTemplate) ArcgisLayerMod {
	return ArcgisLayerModFunc(func(ctx context.Context, o *ArcgisLayerTemplate) {
		o.r.FeatureServiceItemServiceFeature = &arcgisLayerRFeatureServiceItemServiceFeatureR{
			o: rel,
		}
	})
}

func (m arcgisLayerMods) WithNewFeatureServiceItemServiceFeature(mods ...ArcgisServiceFeatureMod) ArcgisLayerMod {
	return ArcgisLayerModFunc(func(ctx context.Context, o *ArcgisLayerTemplate) {
		related := o.f.NewArcgisServiceFeatureWithContext(ctx, mods...)

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

func (m arcgisLayerMods) WithExistingFeatureServiceItemServiceFeature(em *models.ArcgisServiceFeature) ArcgisLayerMod {
	return ArcgisLayerModFunc(func(ctx context.Context, o *ArcgisLayerTemplate) {
		o.r.FeatureServiceItemServiceFeature = &arcgisLayerRFeatureServiceItemServiceFeatureR{
			o: o.f.FromExistingArcgisServiceFeature(em),
		}
	})
}

func (m arcgisLayerMods) WithoutFeatureServiceItemServiceFeature() ArcgisLayerMod {
	return ArcgisLayerModFunc(func(ctx context.Context, o *ArcgisLayerTemplate) {
		o.r.FeatureServiceItemServiceFeature = nil
	})
}

func (m arcgisLayerMods) WithLayerFields(number int, related *ArcgisLayerFieldTemplate) ArcgisLayerMod {
	return ArcgisLayerModFunc(func(ctx context.Context, o *ArcgisLayerTemplate) {
		o.r.LayerFields = []*arcgisLayerRLayerFieldsR{{
			number: number,
			o:      related,
		}}
	})
}

func (m arcgisLayerMods) WithNewLayerFields(number int, mods ...ArcgisLayerFieldMod) ArcgisLayerMod {
	return ArcgisLayerModFunc(func(ctx context.Context, o *ArcgisLayerTemplate) {
		related := o.f.NewArcgisLayerFieldWithContext(ctx, mods...)
		m.WithLayerFields(number, related).Apply(ctx, o)
	})
}

func (m arcgisLayerMods) AddLayerFields(number int, related *ArcgisLayerFieldTemplate) ArcgisLayerMod {
	return ArcgisLayerModFunc(func(ctx context.Context, o *ArcgisLayerTemplate) {
		o.r.LayerFields = append(o.r.LayerFields, &arcgisLayerRLayerFieldsR{
			number: number,
			o:      related,
		})
	})
}

func (m arcgisLayerMods) AddNewLayerFields(number int, mods ...ArcgisLayerFieldMod) ArcgisLayerMod {
	return ArcgisLayerModFunc(func(ctx context.Context, o *ArcgisLayerTemplate) {
		related := o.f.NewArcgisLayerFieldWithContext(ctx, mods...)
		m.AddLayerFields(number, related).Apply(ctx, o)
	})
}

func (m arcgisLayerMods) AddExistingLayerFields(existingModels ...*models.ArcgisLayerField) ArcgisLayerMod {
	return ArcgisLayerModFunc(func(ctx context.Context, o *ArcgisLayerTemplate) {
		for _, em := range existingModels {
			o.r.LayerFields = append(o.r.LayerFields, &arcgisLayerRLayerFieldsR{
				o: o.f.FromExistingArcgisLayerField(em),
			})
		}
	})
}

func (m arcgisLayerMods) WithoutLayerFields() ArcgisLayerMod {
	return ArcgisLayerModFunc(func(ctx context.Context, o *ArcgisLayerTemplate) {
		o.r.LayerFields = nil
	})
}
