// 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/null"
	"github.com/aarondl/opt/omit"
	"github.com/aarondl/opt/omitnull"
	"github.com/jaswdr/faker/v2"
)

type H3AggregationMod interface {
	Apply(context.Context, *H3AggregationTemplate)
}

type H3AggregationModFunc func(context.Context, *H3AggregationTemplate)

func (f H3AggregationModFunc) Apply(ctx context.Context, n *H3AggregationTemplate) {
	f(ctx, n)
}

type H3AggregationModSlice []H3AggregationMod

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

// H3AggregationTemplate is an object representing the database table.
// all columns are optional and should be set by mods
type H3AggregationTemplate struct {
	ID             func() int32
	Cell           func() string
	Count          func() int32
	Geometry       func() null.Val[string]
	OrganizationID func() int32
	Resolution     func() int32
	Type           func() enums.H3aggregationtype

	r h3AggregationR
	f *Factory

	alreadyPersisted bool
}

type h3AggregationR struct {
	Organization *h3AggregationROrganizationR
}

type h3AggregationROrganizationR struct {
	o *OrganizationTemplate
}

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

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

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

	if o.ID != nil {
		val := o.ID()
		m.ID = omit.From(val)
	}
	if o.Cell != nil {
		val := o.Cell()
		m.Cell = omit.From(val)
	}
	if o.Count != nil {
		val := o.Count()
		m.Count = omit.From(val)
	}
	if o.Geometry != nil {
		val := o.Geometry()
		m.Geometry = omitnull.FromNull(val)
	}
	if o.OrganizationID != nil {
		val := o.OrganizationID()
		m.OrganizationID = omit.From(val)
	}
	if o.Resolution != nil {
		val := o.Resolution()
		m.Resolution = omit.From(val)
	}
	if o.Type != nil {
		val := o.Type()
		m.Type = omit.From(val)
	}

	return m
}

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

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

	return m
}

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

	if o.ID != nil {
		m.ID = o.ID()
	}
	if o.Cell != nil {
		m.Cell = o.Cell()
	}
	if o.Count != nil {
		m.Count = o.Count()
	}
	if o.Geometry != nil {
		m.Geometry = o.Geometry()
	}
	if o.OrganizationID != nil {
		m.OrganizationID = o.OrganizationID()
	}
	if o.Resolution != nil {
		m.Resolution = o.Resolution()
	}
	if o.Type != nil {
		m.Type = o.Type()
	}

	o.setModelRels(m)

	return m
}

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

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

	return m
}

func ensureCreatableH3Aggregation(m *models.H3AggregationSetter) {
	if !(m.Cell.IsValue()) {
		val := random_string(nil)
		m.Cell = omit.From(val)
	}
	if !(m.Count.IsValue()) {
		val := random_int32(nil)
		m.Count = omit.From(val)
	}
	if !(m.OrganizationID.IsValue()) {
		val := random_int32(nil)
		m.OrganizationID = omit.From(val)
	}
	if !(m.Resolution.IsValue()) {
		val := random_int32(nil)
		m.Resolution = omit.From(val)
	}
	if !(m.Type.IsValue()) {
		val := random_enums_H3aggregationtype(nil)
		m.Type = omit.From(val)
	}
}

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

	return err
}

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

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

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

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

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

	return m, nil
}

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

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

// H3Aggregation has methods that act as mods for the H3AggregationTemplate
var H3AggregationMods h3AggregationMods

type h3AggregationMods struct{}

func (m h3AggregationMods) RandomizeAllColumns(f *faker.Faker) H3AggregationMod {
	return H3AggregationModSlice{
		H3AggregationMods.RandomID(f),
		H3AggregationMods.RandomCell(f),
		H3AggregationMods.RandomCount(f),
		H3AggregationMods.RandomGeometry(f),
		H3AggregationMods.RandomOrganizationID(f),
		H3AggregationMods.RandomResolution(f),
		H3AggregationMods.RandomType(f),
	}
}

// Set the model columns to this value
func (m h3AggregationMods) ID(val int32) H3AggregationMod {
	return H3AggregationModFunc(func(_ context.Context, o *H3AggregationTemplate) {
		o.ID = func() int32 { return val }
	})
}

// Set the Column from the function
func (m h3AggregationMods) IDFunc(f func() int32) H3AggregationMod {
	return H3AggregationModFunc(func(_ context.Context, o *H3AggregationTemplate) {
		o.ID = f
	})
}

// Clear any values for the column
func (m h3AggregationMods) UnsetID() H3AggregationMod {
	return H3AggregationModFunc(func(_ context.Context, o *H3AggregationTemplate) {
		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 h3AggregationMods) RandomID(f *faker.Faker) H3AggregationMod {
	return H3AggregationModFunc(func(_ context.Context, o *H3AggregationTemplate) {
		o.ID = func() int32 {
			return random_int32(f)
		}
	})
}

// Set the model columns to this value
func (m h3AggregationMods) Cell(val string) H3AggregationMod {
	return H3AggregationModFunc(func(_ context.Context, o *H3AggregationTemplate) {
		o.Cell = func() string { return val }
	})
}

// Set the Column from the function
func (m h3AggregationMods) CellFunc(f func() string) H3AggregationMod {
	return H3AggregationModFunc(func(_ context.Context, o *H3AggregationTemplate) {
		o.Cell = f
	})
}

// Clear any values for the column
func (m h3AggregationMods) UnsetCell() H3AggregationMod {
	return H3AggregationModFunc(func(_ context.Context, o *H3AggregationTemplate) {
		o.Cell = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m h3AggregationMods) RandomCell(f *faker.Faker) H3AggregationMod {
	return H3AggregationModFunc(func(_ context.Context, o *H3AggregationTemplate) {
		o.Cell = func() string {
			return random_string(f)
		}
	})
}

// Set the model columns to this value
func (m h3AggregationMods) Count(val int32) H3AggregationMod {
	return H3AggregationModFunc(func(_ context.Context, o *H3AggregationTemplate) {
		o.Count = func() int32 { return val }
	})
}

// Set the Column from the function
func (m h3AggregationMods) CountFunc(f func() int32) H3AggregationMod {
	return H3AggregationModFunc(func(_ context.Context, o *H3AggregationTemplate) {
		o.Count = f
	})
}

// Clear any values for the column
func (m h3AggregationMods) UnsetCount() H3AggregationMod {
	return H3AggregationModFunc(func(_ context.Context, o *H3AggregationTemplate) {
		o.Count = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m h3AggregationMods) RandomCount(f *faker.Faker) H3AggregationMod {
	return H3AggregationModFunc(func(_ context.Context, o *H3AggregationTemplate) {
		o.Count = func() int32 {
			return random_int32(f)
		}
	})
}

// Set the model columns to this value
func (m h3AggregationMods) Geometry(val null.Val[string]) H3AggregationMod {
	return H3AggregationModFunc(func(_ context.Context, o *H3AggregationTemplate) {
		o.Geometry = func() null.Val[string] { return val }
	})
}

// Set the Column from the function
func (m h3AggregationMods) GeometryFunc(f func() null.Val[string]) H3AggregationMod {
	return H3AggregationModFunc(func(_ context.Context, o *H3AggregationTemplate) {
		o.Geometry = f
	})
}

// Clear any values for the column
func (m h3AggregationMods) UnsetGeometry() H3AggregationMod {
	return H3AggregationModFunc(func(_ context.Context, o *H3AggregationTemplate) {
		o.Geometry = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
// The generated value is sometimes null
func (m h3AggregationMods) RandomGeometry(f *faker.Faker) H3AggregationMod {
	return H3AggregationModFunc(func(_ context.Context, o *H3AggregationTemplate) {
		o.Geometry = func() null.Val[string] {
			if f == nil {
				f = &defaultFaker
			}

			val := random_string(f)
			return null.From(val)
		}
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
// The generated value is never null
func (m h3AggregationMods) RandomGeometryNotNull(f *faker.Faker) H3AggregationMod {
	return H3AggregationModFunc(func(_ context.Context, o *H3AggregationTemplate) {
		o.Geometry = func() null.Val[string] {
			if f == nil {
				f = &defaultFaker
			}

			val := random_string(f)
			return null.From(val)
		}
	})
}

// Set the model columns to this value
func (m h3AggregationMods) OrganizationID(val int32) H3AggregationMod {
	return H3AggregationModFunc(func(_ context.Context, o *H3AggregationTemplate) {
		o.OrganizationID = func() int32 { return val }
	})
}

// Set the Column from the function
func (m h3AggregationMods) OrganizationIDFunc(f func() int32) H3AggregationMod {
	return H3AggregationModFunc(func(_ context.Context, o *H3AggregationTemplate) {
		o.OrganizationID = f
	})
}

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

// Set the model columns to this value
func (m h3AggregationMods) Resolution(val int32) H3AggregationMod {
	return H3AggregationModFunc(func(_ context.Context, o *H3AggregationTemplate) {
		o.Resolution = func() int32 { return val }
	})
}

// Set the Column from the function
func (m h3AggregationMods) ResolutionFunc(f func() int32) H3AggregationMod {
	return H3AggregationModFunc(func(_ context.Context, o *H3AggregationTemplate) {
		o.Resolution = f
	})
}

// Clear any values for the column
func (m h3AggregationMods) UnsetResolution() H3AggregationMod {
	return H3AggregationModFunc(func(_ context.Context, o *H3AggregationTemplate) {
		o.Resolution = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m h3AggregationMods) RandomResolution(f *faker.Faker) H3AggregationMod {
	return H3AggregationModFunc(func(_ context.Context, o *H3AggregationTemplate) {
		o.Resolution = func() int32 {
			return random_int32(f)
		}
	})
}

// Set the model columns to this value
func (m h3AggregationMods) Type(val enums.H3aggregationtype) H3AggregationMod {
	return H3AggregationModFunc(func(_ context.Context, o *H3AggregationTemplate) {
		o.Type = func() enums.H3aggregationtype { return val }
	})
}

// Set the Column from the function
func (m h3AggregationMods) TypeFunc(f func() enums.H3aggregationtype) H3AggregationMod {
	return H3AggregationModFunc(func(_ context.Context, o *H3AggregationTemplate) {
		o.Type = f
	})
}

// Clear any values for the column
func (m h3AggregationMods) UnsetType() H3AggregationMod {
	return H3AggregationModFunc(func(_ context.Context, o *H3AggregationTemplate) {
		o.Type = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m h3AggregationMods) RandomType(f *faker.Faker) H3AggregationMod {
	return H3AggregationModFunc(func(_ context.Context, o *H3AggregationTemplate) {
		o.Type = func() enums.H3aggregationtype {
			return random_enums_H3aggregationtype(f)
		}
	})
}

func (m h3AggregationMods) WithParentsCascading() H3AggregationMod {
	return H3AggregationModFunc(func(ctx context.Context, o *H3AggregationTemplate) {
		if isDone, _ := h3AggregationWithParentsCascadingCtx.Value(ctx); isDone {
			return
		}
		ctx = h3AggregationWithParentsCascadingCtx.WithValue(ctx, true)
		{

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

func (m h3AggregationMods) WithOrganization(rel *OrganizationTemplate) H3AggregationMod {
	return H3AggregationModFunc(func(ctx context.Context, o *H3AggregationTemplate) {
		o.r.Organization = &h3AggregationROrganizationR{
			o: rel,
		}
	})
}

func (m h3AggregationMods) WithNewOrganization(mods ...OrganizationMod) H3AggregationMod {
	return H3AggregationModFunc(func(ctx context.Context, o *H3AggregationTemplate) {
		related := o.f.NewOrganizationWithContext(ctx, mods...)

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

func (m h3AggregationMods) WithExistingOrganization(em *models.Organization) H3AggregationMod {
	return H3AggregationModFunc(func(ctx context.Context, o *H3AggregationTemplate) {
		o.r.Organization = &h3AggregationROrganizationR{
			o: o.f.FromExistingOrganization(em),
		}
	})
}

func (m h3AggregationMods) WithoutOrganization() H3AggregationMod {
	return H3AggregationModFunc(func(ctx context.Context, o *H3AggregationTemplate) {
		o.r.Organization = nil
	})
}
