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

type PublicreportPoolImageModFunc func(context.Context, *PublicreportPoolImageTemplate)

func (f PublicreportPoolImageModFunc) Apply(ctx context.Context, n *PublicreportPoolImageTemplate) {
	f(ctx, n)
}

type PublicreportPoolImageModSlice []PublicreportPoolImageMod

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

// PublicreportPoolImageTemplate is an object representing the database table.
// all columns are optional and should be set by mods
type PublicreportPoolImageTemplate struct {
	ImageID func() int32
	PoolID  func() int32

	r publicreportPoolImageR
	f *Factory

	alreadyPersisted bool
}

type publicreportPoolImageR struct {
	Image *publicreportPoolImageRImageR
	Pool  *publicreportPoolImageRPoolR
}

type publicreportPoolImageRImageR struct {
	o *PublicreportImageTemplate
}
type publicreportPoolImageRPoolR struct {
	o *PublicreportPoolTemplate
}

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

// setModelRels creates and sets the relationships on *models.PublicreportPoolImage
// according to the relationships in the template. Nothing is inserted into the db
func (t PublicreportPoolImageTemplate) setModelRels(o *models.PublicreportPoolImage) {
	if t.r.Image != nil {
		rel := t.r.Image.o.Build()
		o.ImageID = rel.ID // h2
		o.R.Image = rel
	}

	if t.r.Pool != nil {
		rel := t.r.Pool.o.Build()
		o.PoolID = rel.ID // h2
		o.R.Pool = rel
	}
}

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

	if o.ImageID != nil {
		val := o.ImageID()
		m.ImageID = omit.From(val)
	}
	if o.PoolID != nil {
		val := o.PoolID()
		m.PoolID = omit.From(val)
	}

	return m
}

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

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

	return m
}

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

	if o.ImageID != nil {
		m.ImageID = o.ImageID()
	}
	if o.PoolID != nil {
		m.PoolID = o.PoolID()
	}

	o.setModelRels(m)

	return m
}

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

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

	return m
}

func ensureCreatablePublicreportPoolImage(m *models.PublicreportPoolImageSetter) {
	if !(m.ImageID.IsValue()) {
		val := random_int32(nil)
		m.ImageID = omit.From(val)
	}
	if !(m.PoolID.IsValue()) {
		val := random_int32(nil)
		m.PoolID = omit.From(val)
	}
}

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

	return err
}

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

	if o.r.Image == nil {
		PublicreportPoolImageMods.WithNewImage().Apply(ctx, o)
	}

	var rel0 *models.PublicreportImage

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

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

	if o.r.Pool == nil {
		PublicreportPoolImageMods.WithNewPool().Apply(ctx, o)
	}

	var rel1 *models.PublicreportPool

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

	opt.PoolID = omit.From(rel1.ID)

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

	m.R.Image = rel0
	m.R.Pool = rel1

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

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

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

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

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

	return m, nil
}

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

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

// PublicreportPoolImage has methods that act as mods for the PublicreportPoolImageTemplate
var PublicreportPoolImageMods publicreportPoolImageMods

type publicreportPoolImageMods struct{}

func (m publicreportPoolImageMods) RandomizeAllColumns(f *faker.Faker) PublicreportPoolImageMod {
	return PublicreportPoolImageModSlice{
		PublicreportPoolImageMods.RandomImageID(f),
		PublicreportPoolImageMods.RandomPoolID(f),
	}
}

// Set the model columns to this value
func (m publicreportPoolImageMods) ImageID(val int32) PublicreportPoolImageMod {
	return PublicreportPoolImageModFunc(func(_ context.Context, o *PublicreportPoolImageTemplate) {
		o.ImageID = func() int32 { return val }
	})
}

// Set the Column from the function
func (m publicreportPoolImageMods) ImageIDFunc(f func() int32) PublicreportPoolImageMod {
	return PublicreportPoolImageModFunc(func(_ context.Context, o *PublicreportPoolImageTemplate) {
		o.ImageID = f
	})
}

// Clear any values for the column
func (m publicreportPoolImageMods) UnsetImageID() PublicreportPoolImageMod {
	return PublicreportPoolImageModFunc(func(_ context.Context, o *PublicreportPoolImageTemplate) {
		o.ImageID = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m publicreportPoolImageMods) RandomImageID(f *faker.Faker) PublicreportPoolImageMod {
	return PublicreportPoolImageModFunc(func(_ context.Context, o *PublicreportPoolImageTemplate) {
		o.ImageID = func() int32 {
			return random_int32(f)
		}
	})
}

// Set the model columns to this value
func (m publicreportPoolImageMods) PoolID(val int32) PublicreportPoolImageMod {
	return PublicreportPoolImageModFunc(func(_ context.Context, o *PublicreportPoolImageTemplate) {
		o.PoolID = func() int32 { return val }
	})
}

// Set the Column from the function
func (m publicreportPoolImageMods) PoolIDFunc(f func() int32) PublicreportPoolImageMod {
	return PublicreportPoolImageModFunc(func(_ context.Context, o *PublicreportPoolImageTemplate) {
		o.PoolID = f
	})
}

// Clear any values for the column
func (m publicreportPoolImageMods) UnsetPoolID() PublicreportPoolImageMod {
	return PublicreportPoolImageModFunc(func(_ context.Context, o *PublicreportPoolImageTemplate) {
		o.PoolID = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m publicreportPoolImageMods) RandomPoolID(f *faker.Faker) PublicreportPoolImageMod {
	return PublicreportPoolImageModFunc(func(_ context.Context, o *PublicreportPoolImageTemplate) {
		o.PoolID = func() int32 {
			return random_int32(f)
		}
	})
}

func (m publicreportPoolImageMods) WithParentsCascading() PublicreportPoolImageMod {
	return PublicreportPoolImageModFunc(func(ctx context.Context, o *PublicreportPoolImageTemplate) {
		if isDone, _ := publicreportPoolImageWithParentsCascadingCtx.Value(ctx); isDone {
			return
		}
		ctx = publicreportPoolImageWithParentsCascadingCtx.WithValue(ctx, true)
		{

			related := o.f.NewPublicreportImageWithContext(ctx, PublicreportImageMods.WithParentsCascading())
			m.WithImage(related).Apply(ctx, o)
		}
		{

			related := o.f.NewPublicreportPoolWithContext(ctx, PublicreportPoolMods.WithParentsCascading())
			m.WithPool(related).Apply(ctx, o)
		}
	})
}

func (m publicreportPoolImageMods) WithImage(rel *PublicreportImageTemplate) PublicreportPoolImageMod {
	return PublicreportPoolImageModFunc(func(ctx context.Context, o *PublicreportPoolImageTemplate) {
		o.r.Image = &publicreportPoolImageRImageR{
			o: rel,
		}
	})
}

func (m publicreportPoolImageMods) WithNewImage(mods ...PublicreportImageMod) PublicreportPoolImageMod {
	return PublicreportPoolImageModFunc(func(ctx context.Context, o *PublicreportPoolImageTemplate) {
		related := o.f.NewPublicreportImageWithContext(ctx, mods...)

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

func (m publicreportPoolImageMods) WithExistingImage(em *models.PublicreportImage) PublicreportPoolImageMod {
	return PublicreportPoolImageModFunc(func(ctx context.Context, o *PublicreportPoolImageTemplate) {
		o.r.Image = &publicreportPoolImageRImageR{
			o: o.f.FromExistingPublicreportImage(em),
		}
	})
}

func (m publicreportPoolImageMods) WithoutImage() PublicreportPoolImageMod {
	return PublicreportPoolImageModFunc(func(ctx context.Context, o *PublicreportPoolImageTemplate) {
		o.r.Image = nil
	})
}

func (m publicreportPoolImageMods) WithPool(rel *PublicreportPoolTemplate) PublicreportPoolImageMod {
	return PublicreportPoolImageModFunc(func(ctx context.Context, o *PublicreportPoolImageTemplate) {
		o.r.Pool = &publicreportPoolImageRPoolR{
			o: rel,
		}
	})
}

func (m publicreportPoolImageMods) WithNewPool(mods ...PublicreportPoolMod) PublicreportPoolImageMod {
	return PublicreportPoolImageModFunc(func(ctx context.Context, o *PublicreportPoolImageTemplate) {
		related := o.f.NewPublicreportPoolWithContext(ctx, mods...)

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

func (m publicreportPoolImageMods) WithExistingPool(em *models.PublicreportPool) PublicreportPoolImageMod {
	return PublicreportPoolImageModFunc(func(ctx context.Context, o *PublicreportPoolImageTemplate) {
		o.r.Pool = &publicreportPoolImageRPoolR{
			o: o.f.FromExistingPublicreportPool(em),
		}
	})
}

func (m publicreportPoolImageMods) WithoutPool() PublicreportPoolImageMod {
	return PublicreportPoolImageModFunc(func(ctx context.Context, o *PublicreportPoolImageTemplate) {
		o.r.Pool = nil
	})
}
