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

type ArcgisUserPrivilegeModFunc func(context.Context, *ArcgisUserPrivilegeTemplate)

func (f ArcgisUserPrivilegeModFunc) Apply(ctx context.Context, n *ArcgisUserPrivilegeTemplate) {
	f(ctx, n)
}

type ArcgisUserPrivilegeModSlice []ArcgisUserPrivilegeMod

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

// ArcgisUserPrivilegeTemplate is an object representing the database table.
// all columns are optional and should be set by mods
type ArcgisUserPrivilegeTemplate struct {
	UserID    func() string
	Privilege func() string

	r arcgisUserPrivilegeR
	f *Factory

	alreadyPersisted bool
}

type arcgisUserPrivilegeR struct {
	UserUser *arcgisUserPrivilegeRUserUserR
}

type arcgisUserPrivilegeRUserUserR struct {
	o *ArcgisUserTemplate
}

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

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

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

	if o.UserID != nil {
		val := o.UserID()
		m.UserID = omit.From(val)
	}
	if o.Privilege != nil {
		val := o.Privilege()
		m.Privilege = omit.From(val)
	}

	return m
}

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

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

	return m
}

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

	if o.UserID != nil {
		m.UserID = o.UserID()
	}
	if o.Privilege != nil {
		m.Privilege = o.Privilege()
	}

	o.setModelRels(m)

	return m
}

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

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

	return m
}

func ensureCreatableArcgisUserPrivilege(m *models.ArcgisUserPrivilegeSetter) {
	if !(m.UserID.IsValue()) {
		val := random_string(nil)
		m.UserID = omit.From(val)
	}
	if !(m.Privilege.IsValue()) {
		val := random_string(nil)
		m.Privilege = omit.From(val)
	}
}

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

	return err
}

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

	if o.r.UserUser == nil {
		ArcgisUserPrivilegeMods.WithNewUserUser().Apply(ctx, o)
	}

	var rel0 *models.ArcgisUser

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

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

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

	m.R.UserUser = rel0

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

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

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

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

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

	return m, nil
}

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

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

// ArcgisUserPrivilege has methods that act as mods for the ArcgisUserPrivilegeTemplate
var ArcgisUserPrivilegeMods arcgisUserPrivilegeMods

type arcgisUserPrivilegeMods struct{}

func (m arcgisUserPrivilegeMods) RandomizeAllColumns(f *faker.Faker) ArcgisUserPrivilegeMod {
	return ArcgisUserPrivilegeModSlice{
		ArcgisUserPrivilegeMods.RandomUserID(f),
		ArcgisUserPrivilegeMods.RandomPrivilege(f),
	}
}

// Set the model columns to this value
func (m arcgisUserPrivilegeMods) UserID(val string) ArcgisUserPrivilegeMod {
	return ArcgisUserPrivilegeModFunc(func(_ context.Context, o *ArcgisUserPrivilegeTemplate) {
		o.UserID = func() string { return val }
	})
}

// Set the Column from the function
func (m arcgisUserPrivilegeMods) UserIDFunc(f func() string) ArcgisUserPrivilegeMod {
	return ArcgisUserPrivilegeModFunc(func(_ context.Context, o *ArcgisUserPrivilegeTemplate) {
		o.UserID = f
	})
}

// Clear any values for the column
func (m arcgisUserPrivilegeMods) UnsetUserID() ArcgisUserPrivilegeMod {
	return ArcgisUserPrivilegeModFunc(func(_ context.Context, o *ArcgisUserPrivilegeTemplate) {
		o.UserID = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m arcgisUserPrivilegeMods) RandomUserID(f *faker.Faker) ArcgisUserPrivilegeMod {
	return ArcgisUserPrivilegeModFunc(func(_ context.Context, o *ArcgisUserPrivilegeTemplate) {
		o.UserID = func() string {
			return random_string(f)
		}
	})
}

// Set the model columns to this value
func (m arcgisUserPrivilegeMods) Privilege(val string) ArcgisUserPrivilegeMod {
	return ArcgisUserPrivilegeModFunc(func(_ context.Context, o *ArcgisUserPrivilegeTemplate) {
		o.Privilege = func() string { return val }
	})
}

// Set the Column from the function
func (m arcgisUserPrivilegeMods) PrivilegeFunc(f func() string) ArcgisUserPrivilegeMod {
	return ArcgisUserPrivilegeModFunc(func(_ context.Context, o *ArcgisUserPrivilegeTemplate) {
		o.Privilege = f
	})
}

// Clear any values for the column
func (m arcgisUserPrivilegeMods) UnsetPrivilege() ArcgisUserPrivilegeMod {
	return ArcgisUserPrivilegeModFunc(func(_ context.Context, o *ArcgisUserPrivilegeTemplate) {
		o.Privilege = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m arcgisUserPrivilegeMods) RandomPrivilege(f *faker.Faker) ArcgisUserPrivilegeMod {
	return ArcgisUserPrivilegeModFunc(func(_ context.Context, o *ArcgisUserPrivilegeTemplate) {
		o.Privilege = func() string {
			return random_string(f)
		}
	})
}

func (m arcgisUserPrivilegeMods) WithParentsCascading() ArcgisUserPrivilegeMod {
	return ArcgisUserPrivilegeModFunc(func(ctx context.Context, o *ArcgisUserPrivilegeTemplate) {
		if isDone, _ := arcgisUserPrivilegeWithParentsCascadingCtx.Value(ctx); isDone {
			return
		}
		ctx = arcgisUserPrivilegeWithParentsCascadingCtx.WithValue(ctx, true)
		{

			related := o.f.NewArcgisUserWithContext(ctx, ArcgisUserMods.WithParentsCascading())
			m.WithUserUser(related).Apply(ctx, o)
		}
	})
}

func (m arcgisUserPrivilegeMods) WithUserUser(rel *ArcgisUserTemplate) ArcgisUserPrivilegeMod {
	return ArcgisUserPrivilegeModFunc(func(ctx context.Context, o *ArcgisUserPrivilegeTemplate) {
		o.r.UserUser = &arcgisUserPrivilegeRUserUserR{
			o: rel,
		}
	})
}

func (m arcgisUserPrivilegeMods) WithNewUserUser(mods ...ArcgisUserMod) ArcgisUserPrivilegeMod {
	return ArcgisUserPrivilegeModFunc(func(ctx context.Context, o *ArcgisUserPrivilegeTemplate) {
		related := o.f.NewArcgisUserWithContext(ctx, mods...)

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

func (m arcgisUserPrivilegeMods) WithExistingUserUser(em *models.ArcgisUser) ArcgisUserPrivilegeMod {
	return ArcgisUserPrivilegeModFunc(func(ctx context.Context, o *ArcgisUserPrivilegeTemplate) {
		o.r.UserUser = &arcgisUserPrivilegeRUserUserR{
			o: o.f.FromExistingArcgisUser(em),
		}
	})
}

func (m arcgisUserPrivilegeMods) WithoutUserUser() ArcgisUserPrivilegeMod {
	return ArcgisUserPrivilegeModFunc(func(ctx context.Context, o *ArcgisUserPrivilegeTemplate) {
		o.r.UserUser = nil
	})
}
