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

type ArcgisServiceMapModFunc func(context.Context, *ArcgisServiceMapTemplate)

func (f ArcgisServiceMapModFunc) Apply(ctx context.Context, n *ArcgisServiceMapTemplate) {
	f(ctx, n)
}

type ArcgisServiceMapModSlice []ArcgisServiceMapMod

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

// ArcgisServiceMapTemplate is an object representing the database table.
// all columns are optional and should be set by mods
type ArcgisServiceMapTemplate struct {
	AccountID func() string
	ArcgisID  func() string
	Name      func() string
	Title     func() string
	URL       func() string

	r arcgisServiceMapR
	f *Factory

	alreadyPersisted bool
}

type arcgisServiceMapR struct {
	Account *arcgisServiceMapRAccountR
}

type arcgisServiceMapRAccountR struct {
	o *ArcgisAccountTemplate
}

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

// setModelRels creates and sets the relationships on *models.ArcgisServiceMap
// according to the relationships in the template. Nothing is inserted into the db
func (t ArcgisServiceMapTemplate) setModelRels(o *models.ArcgisServiceMap) {
	if t.r.Account != nil {
		rel := t.r.Account.o.Build()
		rel.R.ServiceMaps = append(rel.R.ServiceMaps, o)
		o.AccountID = rel.ID // h2
		o.R.Account = rel
	}
}

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

	if o.AccountID != nil {
		val := o.AccountID()
		m.AccountID = omit.From(val)
	}
	if o.ArcgisID != nil {
		val := o.ArcgisID()
		m.ArcgisID = omit.From(val)
	}
	if o.Name != nil {
		val := o.Name()
		m.Name = omit.From(val)
	}
	if o.Title != nil {
		val := o.Title()
		m.Title = omit.From(val)
	}
	if o.URL != nil {
		val := o.URL()
		m.URL = omit.From(val)
	}

	return m
}

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

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

	return m
}

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

	if o.AccountID != nil {
		m.AccountID = o.AccountID()
	}
	if o.ArcgisID != nil {
		m.ArcgisID = o.ArcgisID()
	}
	if o.Name != nil {
		m.Name = o.Name()
	}
	if o.Title != nil {
		m.Title = o.Title()
	}
	if o.URL != nil {
		m.URL = o.URL()
	}

	o.setModelRels(m)

	return m
}

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

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

	return m
}

func ensureCreatableArcgisServiceMap(m *models.ArcgisServiceMapSetter) {
	if !(m.AccountID.IsValue()) {
		val := random_string(nil)
		m.AccountID = omit.From(val)
	}
	if !(m.ArcgisID.IsValue()) {
		val := random_string(nil)
		m.ArcgisID = omit.From(val)
	}
	if !(m.Name.IsValue()) {
		val := random_string(nil)
		m.Name = omit.From(val)
	}
	if !(m.Title.IsValue()) {
		val := random_string(nil)
		m.Title = omit.From(val)
	}
	if !(m.URL.IsValue()) {
		val := random_string(nil)
		m.URL = omit.From(val)
	}
}

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

	return err
}

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

	if o.r.Account == nil {
		ArcgisServiceMapMods.WithNewAccount().Apply(ctx, o)
	}

	var rel0 *models.ArcgisAccount

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

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

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

	m.R.Account = rel0

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

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

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

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

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

	return m, nil
}

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

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

// ArcgisServiceMap has methods that act as mods for the ArcgisServiceMapTemplate
var ArcgisServiceMapMods arcgisServiceMapMods

type arcgisServiceMapMods struct{}

func (m arcgisServiceMapMods) RandomizeAllColumns(f *faker.Faker) ArcgisServiceMapMod {
	return ArcgisServiceMapModSlice{
		ArcgisServiceMapMods.RandomAccountID(f),
		ArcgisServiceMapMods.RandomArcgisID(f),
		ArcgisServiceMapMods.RandomName(f),
		ArcgisServiceMapMods.RandomTitle(f),
		ArcgisServiceMapMods.RandomURL(f),
	}
}

// Set the model columns to this value
func (m arcgisServiceMapMods) AccountID(val string) ArcgisServiceMapMod {
	return ArcgisServiceMapModFunc(func(_ context.Context, o *ArcgisServiceMapTemplate) {
		o.AccountID = func() string { return val }
	})
}

// Set the Column from the function
func (m arcgisServiceMapMods) AccountIDFunc(f func() string) ArcgisServiceMapMod {
	return ArcgisServiceMapModFunc(func(_ context.Context, o *ArcgisServiceMapTemplate) {
		o.AccountID = f
	})
}

// Clear any values for the column
func (m arcgisServiceMapMods) UnsetAccountID() ArcgisServiceMapMod {
	return ArcgisServiceMapModFunc(func(_ context.Context, o *ArcgisServiceMapTemplate) {
		o.AccountID = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m arcgisServiceMapMods) RandomAccountID(f *faker.Faker) ArcgisServiceMapMod {
	return ArcgisServiceMapModFunc(func(_ context.Context, o *ArcgisServiceMapTemplate) {
		o.AccountID = func() string {
			return random_string(f)
		}
	})
}

// Set the model columns to this value
func (m arcgisServiceMapMods) ArcgisID(val string) ArcgisServiceMapMod {
	return ArcgisServiceMapModFunc(func(_ context.Context, o *ArcgisServiceMapTemplate) {
		o.ArcgisID = func() string { return val }
	})
}

// Set the Column from the function
func (m arcgisServiceMapMods) ArcgisIDFunc(f func() string) ArcgisServiceMapMod {
	return ArcgisServiceMapModFunc(func(_ context.Context, o *ArcgisServiceMapTemplate) {
		o.ArcgisID = f
	})
}

// Clear any values for the column
func (m arcgisServiceMapMods) UnsetArcgisID() ArcgisServiceMapMod {
	return ArcgisServiceMapModFunc(func(_ context.Context, o *ArcgisServiceMapTemplate) {
		o.ArcgisID = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m arcgisServiceMapMods) RandomArcgisID(f *faker.Faker) ArcgisServiceMapMod {
	return ArcgisServiceMapModFunc(func(_ context.Context, o *ArcgisServiceMapTemplate) {
		o.ArcgisID = func() string {
			return random_string(f)
		}
	})
}

// Set the model columns to this value
func (m arcgisServiceMapMods) Name(val string) ArcgisServiceMapMod {
	return ArcgisServiceMapModFunc(func(_ context.Context, o *ArcgisServiceMapTemplate) {
		o.Name = func() string { return val }
	})
}

// Set the Column from the function
func (m arcgisServiceMapMods) NameFunc(f func() string) ArcgisServiceMapMod {
	return ArcgisServiceMapModFunc(func(_ context.Context, o *ArcgisServiceMapTemplate) {
		o.Name = f
	})
}

// Clear any values for the column
func (m arcgisServiceMapMods) UnsetName() ArcgisServiceMapMod {
	return ArcgisServiceMapModFunc(func(_ context.Context, o *ArcgisServiceMapTemplate) {
		o.Name = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m arcgisServiceMapMods) RandomName(f *faker.Faker) ArcgisServiceMapMod {
	return ArcgisServiceMapModFunc(func(_ context.Context, o *ArcgisServiceMapTemplate) {
		o.Name = func() string {
			return random_string(f)
		}
	})
}

// Set the model columns to this value
func (m arcgisServiceMapMods) Title(val string) ArcgisServiceMapMod {
	return ArcgisServiceMapModFunc(func(_ context.Context, o *ArcgisServiceMapTemplate) {
		o.Title = func() string { return val }
	})
}

// Set the Column from the function
func (m arcgisServiceMapMods) TitleFunc(f func() string) ArcgisServiceMapMod {
	return ArcgisServiceMapModFunc(func(_ context.Context, o *ArcgisServiceMapTemplate) {
		o.Title = f
	})
}

// Clear any values for the column
func (m arcgisServiceMapMods) UnsetTitle() ArcgisServiceMapMod {
	return ArcgisServiceMapModFunc(func(_ context.Context, o *ArcgisServiceMapTemplate) {
		o.Title = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m arcgisServiceMapMods) RandomTitle(f *faker.Faker) ArcgisServiceMapMod {
	return ArcgisServiceMapModFunc(func(_ context.Context, o *ArcgisServiceMapTemplate) {
		o.Title = func() string {
			return random_string(f)
		}
	})
}

// Set the model columns to this value
func (m arcgisServiceMapMods) URL(val string) ArcgisServiceMapMod {
	return ArcgisServiceMapModFunc(func(_ context.Context, o *ArcgisServiceMapTemplate) {
		o.URL = func() string { return val }
	})
}

// Set the Column from the function
func (m arcgisServiceMapMods) URLFunc(f func() string) ArcgisServiceMapMod {
	return ArcgisServiceMapModFunc(func(_ context.Context, o *ArcgisServiceMapTemplate) {
		o.URL = f
	})
}

// Clear any values for the column
func (m arcgisServiceMapMods) UnsetURL() ArcgisServiceMapMod {
	return ArcgisServiceMapModFunc(func(_ context.Context, o *ArcgisServiceMapTemplate) {
		o.URL = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m arcgisServiceMapMods) RandomURL(f *faker.Faker) ArcgisServiceMapMod {
	return ArcgisServiceMapModFunc(func(_ context.Context, o *ArcgisServiceMapTemplate) {
		o.URL = func() string {
			return random_string(f)
		}
	})
}

func (m arcgisServiceMapMods) WithParentsCascading() ArcgisServiceMapMod {
	return ArcgisServiceMapModFunc(func(ctx context.Context, o *ArcgisServiceMapTemplate) {
		if isDone, _ := arcgisServiceMapWithParentsCascadingCtx.Value(ctx); isDone {
			return
		}
		ctx = arcgisServiceMapWithParentsCascadingCtx.WithValue(ctx, true)
		{

			related := o.f.NewArcgisAccountWithContext(ctx, ArcgisAccountMods.WithParentsCascading())
			m.WithAccount(related).Apply(ctx, o)
		}
	})
}

func (m arcgisServiceMapMods) WithAccount(rel *ArcgisAccountTemplate) ArcgisServiceMapMod {
	return ArcgisServiceMapModFunc(func(ctx context.Context, o *ArcgisServiceMapTemplate) {
		o.r.Account = &arcgisServiceMapRAccountR{
			o: rel,
		}
	})
}

func (m arcgisServiceMapMods) WithNewAccount(mods ...ArcgisAccountMod) ArcgisServiceMapMod {
	return ArcgisServiceMapModFunc(func(ctx context.Context, o *ArcgisServiceMapTemplate) {
		related := o.f.NewArcgisAccountWithContext(ctx, mods...)

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

func (m arcgisServiceMapMods) WithExistingAccount(em *models.ArcgisAccount) ArcgisServiceMapMod {
	return ArcgisServiceMapModFunc(func(ctx context.Context, o *ArcgisServiceMapTemplate) {
		o.r.Account = &arcgisServiceMapRAccountR{
			o: o.f.FromExistingArcgisAccount(em),
		}
	})
}

func (m arcgisServiceMapMods) WithoutAccount() ArcgisServiceMapMod {
	return ArcgisServiceMapModFunc(func(ctx context.Context, o *ArcgisServiceMapTemplate) {
		o.r.Account = nil
	})
}
