// 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"
	"time"

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

type FieldseekerSyncModFunc func(context.Context, *FieldseekerSyncTemplate)

func (f FieldseekerSyncModFunc) Apply(ctx context.Context, n *FieldseekerSyncTemplate) {
	f(ctx, n)
}

type FieldseekerSyncModSlice []FieldseekerSyncMod

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

// FieldseekerSyncTemplate is an object representing the database table.
// all columns are optional and should be set by mods
type FieldseekerSyncTemplate struct {
	ID               func() int32
	Created          func() time.Time
	RecordsCreated   func() int32
	RecordsUpdated   func() int32
	RecordsUnchanged func() int32
	OrganizationID   func() int32

	r fieldseekerSyncR
	f *Factory

	alreadyPersisted bool
}

type fieldseekerSyncR struct {
	Organization *fieldseekerSyncROrganizationR
}

type fieldseekerSyncROrganizationR struct {
	o *OrganizationTemplate
}

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

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

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

	if o.ID != nil {
		val := o.ID()
		m.ID = omit.From(val)
	}
	if o.Created != nil {
		val := o.Created()
		m.Created = omit.From(val)
	}
	if o.RecordsCreated != nil {
		val := o.RecordsCreated()
		m.RecordsCreated = omit.From(val)
	}
	if o.RecordsUpdated != nil {
		val := o.RecordsUpdated()
		m.RecordsUpdated = omit.From(val)
	}
	if o.RecordsUnchanged != nil {
		val := o.RecordsUnchanged()
		m.RecordsUnchanged = omit.From(val)
	}
	if o.OrganizationID != nil {
		val := o.OrganizationID()
		m.OrganizationID = omit.From(val)
	}

	return m
}

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

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

	return m
}

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

	if o.ID != nil {
		m.ID = o.ID()
	}
	if o.Created != nil {
		m.Created = o.Created()
	}
	if o.RecordsCreated != nil {
		m.RecordsCreated = o.RecordsCreated()
	}
	if o.RecordsUpdated != nil {
		m.RecordsUpdated = o.RecordsUpdated()
	}
	if o.RecordsUnchanged != nil {
		m.RecordsUnchanged = o.RecordsUnchanged()
	}
	if o.OrganizationID != nil {
		m.OrganizationID = o.OrganizationID()
	}

	o.setModelRels(m)

	return m
}

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

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

	return m
}

func ensureCreatableFieldseekerSync(m *models.FieldseekerSyncSetter) {
	if !(m.RecordsCreated.IsValue()) {
		val := random_int32(nil)
		m.RecordsCreated = omit.From(val)
	}
	if !(m.RecordsUpdated.IsValue()) {
		val := random_int32(nil)
		m.RecordsUpdated = omit.From(val)
	}
	if !(m.RecordsUnchanged.IsValue()) {
		val := random_int32(nil)
		m.RecordsUnchanged = 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.FieldseekerSync
// according to the relationships in the template.
// any required relationship should have already exist on the model
func (o *FieldseekerSyncTemplate) insertOptRels(ctx context.Context, exec bob.Executor, m *models.FieldseekerSync) error {
	var err error

	return err
}

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

	if o.r.Organization == nil {
		FieldseekerSyncMods.WithNewOrganization().Apply(ctx, o)
	}

	var rel0 *models.Organization

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

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

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

	m.R.Organization = rel0

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

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

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

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

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

	return m, nil
}

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

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

// FieldseekerSync has methods that act as mods for the FieldseekerSyncTemplate
var FieldseekerSyncMods fieldseekerSyncMods

type fieldseekerSyncMods struct{}

func (m fieldseekerSyncMods) RandomizeAllColumns(f *faker.Faker) FieldseekerSyncMod {
	return FieldseekerSyncModSlice{
		FieldseekerSyncMods.RandomID(f),
		FieldseekerSyncMods.RandomCreated(f),
		FieldseekerSyncMods.RandomRecordsCreated(f),
		FieldseekerSyncMods.RandomRecordsUpdated(f),
		FieldseekerSyncMods.RandomRecordsUnchanged(f),
		FieldseekerSyncMods.RandomOrganizationID(f),
	}
}

// Set the model columns to this value
func (m fieldseekerSyncMods) ID(val int32) FieldseekerSyncMod {
	return FieldseekerSyncModFunc(func(_ context.Context, o *FieldseekerSyncTemplate) {
		o.ID = func() int32 { return val }
	})
}

// Set the Column from the function
func (m fieldseekerSyncMods) IDFunc(f func() int32) FieldseekerSyncMod {
	return FieldseekerSyncModFunc(func(_ context.Context, o *FieldseekerSyncTemplate) {
		o.ID = f
	})
}

// Clear any values for the column
func (m fieldseekerSyncMods) UnsetID() FieldseekerSyncMod {
	return FieldseekerSyncModFunc(func(_ context.Context, o *FieldseekerSyncTemplate) {
		o.ID = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m fieldseekerSyncMods) RandomID(f *faker.Faker) FieldseekerSyncMod {
	return FieldseekerSyncModFunc(func(_ context.Context, o *FieldseekerSyncTemplate) {
		o.ID = func() int32 {
			return random_int32(f)
		}
	})
}

// Set the model columns to this value
func (m fieldseekerSyncMods) Created(val time.Time) FieldseekerSyncMod {
	return FieldseekerSyncModFunc(func(_ context.Context, o *FieldseekerSyncTemplate) {
		o.Created = func() time.Time { return val }
	})
}

// Set the Column from the function
func (m fieldseekerSyncMods) CreatedFunc(f func() time.Time) FieldseekerSyncMod {
	return FieldseekerSyncModFunc(func(_ context.Context, o *FieldseekerSyncTemplate) {
		o.Created = f
	})
}

// Clear any values for the column
func (m fieldseekerSyncMods) UnsetCreated() FieldseekerSyncMod {
	return FieldseekerSyncModFunc(func(_ context.Context, o *FieldseekerSyncTemplate) {
		o.Created = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m fieldseekerSyncMods) RandomCreated(f *faker.Faker) FieldseekerSyncMod {
	return FieldseekerSyncModFunc(func(_ context.Context, o *FieldseekerSyncTemplate) {
		o.Created = func() time.Time {
			return random_time_Time(f)
		}
	})
}

// Set the model columns to this value
func (m fieldseekerSyncMods) RecordsCreated(val int32) FieldseekerSyncMod {
	return FieldseekerSyncModFunc(func(_ context.Context, o *FieldseekerSyncTemplate) {
		o.RecordsCreated = func() int32 { return val }
	})
}

// Set the Column from the function
func (m fieldseekerSyncMods) RecordsCreatedFunc(f func() int32) FieldseekerSyncMod {
	return FieldseekerSyncModFunc(func(_ context.Context, o *FieldseekerSyncTemplate) {
		o.RecordsCreated = f
	})
}

// Clear any values for the column
func (m fieldseekerSyncMods) UnsetRecordsCreated() FieldseekerSyncMod {
	return FieldseekerSyncModFunc(func(_ context.Context, o *FieldseekerSyncTemplate) {
		o.RecordsCreated = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m fieldseekerSyncMods) RandomRecordsCreated(f *faker.Faker) FieldseekerSyncMod {
	return FieldseekerSyncModFunc(func(_ context.Context, o *FieldseekerSyncTemplate) {
		o.RecordsCreated = func() int32 {
			return random_int32(f)
		}
	})
}

// Set the model columns to this value
func (m fieldseekerSyncMods) RecordsUpdated(val int32) FieldseekerSyncMod {
	return FieldseekerSyncModFunc(func(_ context.Context, o *FieldseekerSyncTemplate) {
		o.RecordsUpdated = func() int32 { return val }
	})
}

// Set the Column from the function
func (m fieldseekerSyncMods) RecordsUpdatedFunc(f func() int32) FieldseekerSyncMod {
	return FieldseekerSyncModFunc(func(_ context.Context, o *FieldseekerSyncTemplate) {
		o.RecordsUpdated = f
	})
}

// Clear any values for the column
func (m fieldseekerSyncMods) UnsetRecordsUpdated() FieldseekerSyncMod {
	return FieldseekerSyncModFunc(func(_ context.Context, o *FieldseekerSyncTemplate) {
		o.RecordsUpdated = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m fieldseekerSyncMods) RandomRecordsUpdated(f *faker.Faker) FieldseekerSyncMod {
	return FieldseekerSyncModFunc(func(_ context.Context, o *FieldseekerSyncTemplate) {
		o.RecordsUpdated = func() int32 {
			return random_int32(f)
		}
	})
}

// Set the model columns to this value
func (m fieldseekerSyncMods) RecordsUnchanged(val int32) FieldseekerSyncMod {
	return FieldseekerSyncModFunc(func(_ context.Context, o *FieldseekerSyncTemplate) {
		o.RecordsUnchanged = func() int32 { return val }
	})
}

// Set the Column from the function
func (m fieldseekerSyncMods) RecordsUnchangedFunc(f func() int32) FieldseekerSyncMod {
	return FieldseekerSyncModFunc(func(_ context.Context, o *FieldseekerSyncTemplate) {
		o.RecordsUnchanged = f
	})
}

// Clear any values for the column
func (m fieldseekerSyncMods) UnsetRecordsUnchanged() FieldseekerSyncMod {
	return FieldseekerSyncModFunc(func(_ context.Context, o *FieldseekerSyncTemplate) {
		o.RecordsUnchanged = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m fieldseekerSyncMods) RandomRecordsUnchanged(f *faker.Faker) FieldseekerSyncMod {
	return FieldseekerSyncModFunc(func(_ context.Context, o *FieldseekerSyncTemplate) {
		o.RecordsUnchanged = func() int32 {
			return random_int32(f)
		}
	})
}

// Set the model columns to this value
func (m fieldseekerSyncMods) OrganizationID(val int32) FieldseekerSyncMod {
	return FieldseekerSyncModFunc(func(_ context.Context, o *FieldseekerSyncTemplate) {
		o.OrganizationID = func() int32 { return val }
	})
}

// Set the Column from the function
func (m fieldseekerSyncMods) OrganizationIDFunc(f func() int32) FieldseekerSyncMod {
	return FieldseekerSyncModFunc(func(_ context.Context, o *FieldseekerSyncTemplate) {
		o.OrganizationID = f
	})
}

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

func (m fieldseekerSyncMods) WithParentsCascading() FieldseekerSyncMod {
	return FieldseekerSyncModFunc(func(ctx context.Context, o *FieldseekerSyncTemplate) {
		if isDone, _ := fieldseekerSyncWithParentsCascadingCtx.Value(ctx); isDone {
			return
		}
		ctx = fieldseekerSyncWithParentsCascadingCtx.WithValue(ctx, true)
		{

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

func (m fieldseekerSyncMods) WithOrganization(rel *OrganizationTemplate) FieldseekerSyncMod {
	return FieldseekerSyncModFunc(func(ctx context.Context, o *FieldseekerSyncTemplate) {
		o.r.Organization = &fieldseekerSyncROrganizationR{
			o: rel,
		}
	})
}

func (m fieldseekerSyncMods) WithNewOrganization(mods ...OrganizationMod) FieldseekerSyncMod {
	return FieldseekerSyncModFunc(func(ctx context.Context, o *FieldseekerSyncTemplate) {
		related := o.f.NewOrganizationWithContext(ctx, mods...)

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

func (m fieldseekerSyncMods) WithExistingOrganization(em *models.Organization) FieldseekerSyncMod {
	return FieldseekerSyncModFunc(func(ctx context.Context, o *FieldseekerSyncTemplate) {
		o.r.Organization = &fieldseekerSyncROrganizationR{
			o: o.f.FromExistingOrganization(em),
		}
	})
}

func (m fieldseekerSyncMods) WithoutOrganization() FieldseekerSyncMod {
	return FieldseekerSyncModFunc(func(ctx context.Context, o *FieldseekerSyncTemplate) {
		o.r.Organization = nil
	})
}
