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

type ParcelMod interface {
	Apply(context.Context, *ParcelTemplate)
}

type ParcelModFunc func(context.Context, *ParcelTemplate)

func (f ParcelModFunc) Apply(ctx context.Context, n *ParcelTemplate) {
	f(ctx, n)
}

type ParcelModSlice []ParcelMod

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

// ParcelTemplate is an object representing the database table.
// all columns are optional and should be set by mods
type ParcelTemplate struct {
	Apn         func() null.Val[string]
	Description func() null.Val[string]
	ID          func() int32
	Geometry    func() string

	r parcelR
	f *Factory

	alreadyPersisted bool
}

type parcelR struct {
	Sites []*parcelRSitesR
}

type parcelRSitesR struct {
	number int
	o      *SiteTemplate
}

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

// setModelRels creates and sets the relationships on *models.Parcel
// according to the relationships in the template. Nothing is inserted into the db
func (t ParcelTemplate) setModelRels(o *models.Parcel) {
	if t.r.Sites != nil {
		rel := models.SiteSlice{}
		for _, r := range t.r.Sites {
			related := r.o.BuildMany(r.number)
			for _, rel := range related {
				rel.ParcelID = o.ID // h2
				rel.R.Parcel = o
			}
			rel = append(rel, related...)
		}
		o.R.Sites = rel
	}
}

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

	if o.Apn != nil {
		val := o.Apn()
		m.Apn = omitnull.FromNull(val)
	}
	if o.Description != nil {
		val := o.Description()
		m.Description = omitnull.FromNull(val)
	}
	if o.ID != nil {
		val := o.ID()
		m.ID = omit.From(val)
	}
	if o.Geometry != nil {
		val := o.Geometry()
		m.Geometry = omit.From(val)
	}

	return m
}

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

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

	return m
}

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

	if o.Apn != nil {
		m.Apn = o.Apn()
	}
	if o.Description != nil {
		m.Description = o.Description()
	}
	if o.ID != nil {
		m.ID = o.ID()
	}
	if o.Geometry != nil {
		m.Geometry = o.Geometry()
	}

	o.setModelRels(m)

	return m
}

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

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

	return m
}

func ensureCreatableParcel(m *models.ParcelSetter) {
	if !(m.Geometry.IsValue()) {
		val := random_string(nil)
		m.Geometry = omit.From(val)
	}
}

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

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

				err = m.AttachSites(ctx, exec, rel0...)
				if err != nil {
					return err
				}
			}
		}
	}

	return err
}

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

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

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

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

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

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

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

	return m, nil
}

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

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

// Parcel has methods that act as mods for the ParcelTemplate
var ParcelMods parcelMods

type parcelMods struct{}

func (m parcelMods) RandomizeAllColumns(f *faker.Faker) ParcelMod {
	return ParcelModSlice{
		ParcelMods.RandomApn(f),
		ParcelMods.RandomDescription(f),
		ParcelMods.RandomID(f),
		ParcelMods.RandomGeometry(f),
	}
}

// Set the model columns to this value
func (m parcelMods) Apn(val null.Val[string]) ParcelMod {
	return ParcelModFunc(func(_ context.Context, o *ParcelTemplate) {
		o.Apn = func() null.Val[string] { return val }
	})
}

// Set the Column from the function
func (m parcelMods) ApnFunc(f func() null.Val[string]) ParcelMod {
	return ParcelModFunc(func(_ context.Context, o *ParcelTemplate) {
		o.Apn = f
	})
}

// Clear any values for the column
func (m parcelMods) UnsetApn() ParcelMod {
	return ParcelModFunc(func(_ context.Context, o *ParcelTemplate) {
		o.Apn = 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 parcelMods) RandomApn(f *faker.Faker) ParcelMod {
	return ParcelModFunc(func(_ context.Context, o *ParcelTemplate) {
		o.Apn = 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 parcelMods) RandomApnNotNull(f *faker.Faker) ParcelMod {
	return ParcelModFunc(func(_ context.Context, o *ParcelTemplate) {
		o.Apn = 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 parcelMods) Description(val null.Val[string]) ParcelMod {
	return ParcelModFunc(func(_ context.Context, o *ParcelTemplate) {
		o.Description = func() null.Val[string] { return val }
	})
}

// Set the Column from the function
func (m parcelMods) DescriptionFunc(f func() null.Val[string]) ParcelMod {
	return ParcelModFunc(func(_ context.Context, o *ParcelTemplate) {
		o.Description = f
	})
}

// Clear any values for the column
func (m parcelMods) UnsetDescription() ParcelMod {
	return ParcelModFunc(func(_ context.Context, o *ParcelTemplate) {
		o.Description = 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 parcelMods) RandomDescription(f *faker.Faker) ParcelMod {
	return ParcelModFunc(func(_ context.Context, o *ParcelTemplate) {
		o.Description = 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 parcelMods) RandomDescriptionNotNull(f *faker.Faker) ParcelMod {
	return ParcelModFunc(func(_ context.Context, o *ParcelTemplate) {
		o.Description = 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 parcelMods) ID(val int32) ParcelMod {
	return ParcelModFunc(func(_ context.Context, o *ParcelTemplate) {
		o.ID = func() int32 { return val }
	})
}

// Set the Column from the function
func (m parcelMods) IDFunc(f func() int32) ParcelMod {
	return ParcelModFunc(func(_ context.Context, o *ParcelTemplate) {
		o.ID = f
	})
}

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

// Set the model columns to this value
func (m parcelMods) Geometry(val string) ParcelMod {
	return ParcelModFunc(func(_ context.Context, o *ParcelTemplate) {
		o.Geometry = func() string { return val }
	})
}

// Set the Column from the function
func (m parcelMods) GeometryFunc(f func() string) ParcelMod {
	return ParcelModFunc(func(_ context.Context, o *ParcelTemplate) {
		o.Geometry = f
	})
}

// Clear any values for the column
func (m parcelMods) UnsetGeometry() ParcelMod {
	return ParcelModFunc(func(_ context.Context, o *ParcelTemplate) {
		o.Geometry = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m parcelMods) RandomGeometry(f *faker.Faker) ParcelMod {
	return ParcelModFunc(func(_ context.Context, o *ParcelTemplate) {
		o.Geometry = func() string {
			return random_string(f)
		}
	})
}

func (m parcelMods) WithParentsCascading() ParcelMod {
	return ParcelModFunc(func(ctx context.Context, o *ParcelTemplate) {
		if isDone, _ := parcelWithParentsCascadingCtx.Value(ctx); isDone {
			return
		}
		ctx = parcelWithParentsCascadingCtx.WithValue(ctx, true)
	})
}

func (m parcelMods) WithSites(number int, related *SiteTemplate) ParcelMod {
	return ParcelModFunc(func(ctx context.Context, o *ParcelTemplate) {
		o.r.Sites = []*parcelRSitesR{{
			number: number,
			o:      related,
		}}
	})
}

func (m parcelMods) WithNewSites(number int, mods ...SiteMod) ParcelMod {
	return ParcelModFunc(func(ctx context.Context, o *ParcelTemplate) {
		related := o.f.NewSiteWithContext(ctx, mods...)
		m.WithSites(number, related).Apply(ctx, o)
	})
}

func (m parcelMods) AddSites(number int, related *SiteTemplate) ParcelMod {
	return ParcelModFunc(func(ctx context.Context, o *ParcelTemplate) {
		o.r.Sites = append(o.r.Sites, &parcelRSitesR{
			number: number,
			o:      related,
		})
	})
}

func (m parcelMods) AddNewSites(number int, mods ...SiteMod) ParcelMod {
	return ParcelModFunc(func(ctx context.Context, o *ParcelTemplate) {
		related := o.f.NewSiteWithContext(ctx, mods...)
		m.AddSites(number, related).Apply(ctx, o)
	})
}

func (m parcelMods) AddExistingSites(existingModels ...*models.Site) ParcelMod {
	return ParcelModFunc(func(ctx context.Context, o *ParcelTemplate) {
		for _, em := range existingModels {
			o.r.Sites = append(o.r.Sites, &parcelRSitesR{
				o: o.f.FromExistingSite(em),
			})
		}
	})
}

func (m parcelMods) WithoutSites() ParcelMod {
	return ParcelModFunc(func(ctx context.Context, o *ParcelTemplate) {
		o.r.Sites = nil
	})
}
