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

type PublicreportNuisanceImageModFunc func(context.Context, *PublicreportNuisanceImageTemplate)

func (f PublicreportNuisanceImageModFunc) Apply(ctx context.Context, n *PublicreportNuisanceImageTemplate) {
	f(ctx, n)
}

type PublicreportNuisanceImageModSlice []PublicreportNuisanceImageMod

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

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

	r publicreportNuisanceImageR
	f *Factory

	alreadyPersisted bool
}

type publicreportNuisanceImageR struct {
	Image    *publicreportNuisanceImageRImageR
	Nuisance *publicreportNuisanceImageRNuisanceR
}

type publicreportNuisanceImageRImageR struct {
	o *PublicreportImageTemplate
}
type publicreportNuisanceImageRNuisanceR struct {
	o *PublicreportNuisanceTemplate
}

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

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

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

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

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

	return m
}

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

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

	return m
}

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

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

	o.setModelRels(m)

	return m
}

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

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

	return m
}

func ensureCreatablePublicreportNuisanceImage(m *models.PublicreportNuisanceImageSetter) {
	if !(m.ImageID.IsValue()) {
		val := random_int32(nil)
		m.ImageID = omit.From(val)
	}
	if !(m.NuisanceID.IsValue()) {
		val := random_int32(nil)
		m.NuisanceID = omit.From(val)
	}
}

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

	return err
}

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

	if o.r.Image == nil {
		PublicreportNuisanceImageMods.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.Nuisance == nil {
		PublicreportNuisanceImageMods.WithNewNuisance().Apply(ctx, o)
	}

	var rel1 *models.PublicreportNuisance

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

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

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

	m.R.Image = rel0
	m.R.Nuisance = rel1

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

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

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

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

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

	return m, nil
}

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

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

// PublicreportNuisanceImage has methods that act as mods for the PublicreportNuisanceImageTemplate
var PublicreportNuisanceImageMods publicreportNuisanceImageMods

type publicreportNuisanceImageMods struct{}

func (m publicreportNuisanceImageMods) RandomizeAllColumns(f *faker.Faker) PublicreportNuisanceImageMod {
	return PublicreportNuisanceImageModSlice{
		PublicreportNuisanceImageMods.RandomImageID(f),
		PublicreportNuisanceImageMods.RandomNuisanceID(f),
	}
}

// Set the model columns to this value
func (m publicreportNuisanceImageMods) ImageID(val int32) PublicreportNuisanceImageMod {
	return PublicreportNuisanceImageModFunc(func(_ context.Context, o *PublicreportNuisanceImageTemplate) {
		o.ImageID = func() int32 { return val }
	})
}

// Set the Column from the function
func (m publicreportNuisanceImageMods) ImageIDFunc(f func() int32) PublicreportNuisanceImageMod {
	return PublicreportNuisanceImageModFunc(func(_ context.Context, o *PublicreportNuisanceImageTemplate) {
		o.ImageID = f
	})
}

// Clear any values for the column
func (m publicreportNuisanceImageMods) UnsetImageID() PublicreportNuisanceImageMod {
	return PublicreportNuisanceImageModFunc(func(_ context.Context, o *PublicreportNuisanceImageTemplate) {
		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 publicreportNuisanceImageMods) RandomImageID(f *faker.Faker) PublicreportNuisanceImageMod {
	return PublicreportNuisanceImageModFunc(func(_ context.Context, o *PublicreportNuisanceImageTemplate) {
		o.ImageID = func() int32 {
			return random_int32(f)
		}
	})
}

// Set the model columns to this value
func (m publicreportNuisanceImageMods) NuisanceID(val int32) PublicreportNuisanceImageMod {
	return PublicreportNuisanceImageModFunc(func(_ context.Context, o *PublicreportNuisanceImageTemplate) {
		o.NuisanceID = func() int32 { return val }
	})
}

// Set the Column from the function
func (m publicreportNuisanceImageMods) NuisanceIDFunc(f func() int32) PublicreportNuisanceImageMod {
	return PublicreportNuisanceImageModFunc(func(_ context.Context, o *PublicreportNuisanceImageTemplate) {
		o.NuisanceID = f
	})
}

// Clear any values for the column
func (m publicreportNuisanceImageMods) UnsetNuisanceID() PublicreportNuisanceImageMod {
	return PublicreportNuisanceImageModFunc(func(_ context.Context, o *PublicreportNuisanceImageTemplate) {
		o.NuisanceID = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m publicreportNuisanceImageMods) RandomNuisanceID(f *faker.Faker) PublicreportNuisanceImageMod {
	return PublicreportNuisanceImageModFunc(func(_ context.Context, o *PublicreportNuisanceImageTemplate) {
		o.NuisanceID = func() int32 {
			return random_int32(f)
		}
	})
}

func (m publicreportNuisanceImageMods) WithParentsCascading() PublicreportNuisanceImageMod {
	return PublicreportNuisanceImageModFunc(func(ctx context.Context, o *PublicreportNuisanceImageTemplate) {
		if isDone, _ := publicreportNuisanceImageWithParentsCascadingCtx.Value(ctx); isDone {
			return
		}
		ctx = publicreportNuisanceImageWithParentsCascadingCtx.WithValue(ctx, true)
		{

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

			related := o.f.NewPublicreportNuisanceWithContext(ctx, PublicreportNuisanceMods.WithParentsCascading())
			m.WithNuisance(related).Apply(ctx, o)
		}
	})
}

func (m publicreportNuisanceImageMods) WithImage(rel *PublicreportImageTemplate) PublicreportNuisanceImageMod {
	return PublicreportNuisanceImageModFunc(func(ctx context.Context, o *PublicreportNuisanceImageTemplate) {
		o.r.Image = &publicreportNuisanceImageRImageR{
			o: rel,
		}
	})
}

func (m publicreportNuisanceImageMods) WithNewImage(mods ...PublicreportImageMod) PublicreportNuisanceImageMod {
	return PublicreportNuisanceImageModFunc(func(ctx context.Context, o *PublicreportNuisanceImageTemplate) {
		related := o.f.NewPublicreportImageWithContext(ctx, mods...)

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

func (m publicreportNuisanceImageMods) WithExistingImage(em *models.PublicreportImage) PublicreportNuisanceImageMod {
	return PublicreportNuisanceImageModFunc(func(ctx context.Context, o *PublicreportNuisanceImageTemplate) {
		o.r.Image = &publicreportNuisanceImageRImageR{
			o: o.f.FromExistingPublicreportImage(em),
		}
	})
}

func (m publicreportNuisanceImageMods) WithoutImage() PublicreportNuisanceImageMod {
	return PublicreportNuisanceImageModFunc(func(ctx context.Context, o *PublicreportNuisanceImageTemplate) {
		o.r.Image = nil
	})
}

func (m publicreportNuisanceImageMods) WithNuisance(rel *PublicreportNuisanceTemplate) PublicreportNuisanceImageMod {
	return PublicreportNuisanceImageModFunc(func(ctx context.Context, o *PublicreportNuisanceImageTemplate) {
		o.r.Nuisance = &publicreportNuisanceImageRNuisanceR{
			o: rel,
		}
	})
}

func (m publicreportNuisanceImageMods) WithNewNuisance(mods ...PublicreportNuisanceMod) PublicreportNuisanceImageMod {
	return PublicreportNuisanceImageModFunc(func(ctx context.Context, o *PublicreportNuisanceImageTemplate) {
		related := o.f.NewPublicreportNuisanceWithContext(ctx, mods...)

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

func (m publicreportNuisanceImageMods) WithExistingNuisance(em *models.PublicreportNuisance) PublicreportNuisanceImageMod {
	return PublicreportNuisanceImageModFunc(func(ctx context.Context, o *PublicreportNuisanceImageTemplate) {
		o.r.Nuisance = &publicreportNuisanceImageRNuisanceR{
			o: o.f.FromExistingPublicreportNuisance(em),
		}
	})
}

func (m publicreportNuisanceImageMods) WithoutNuisance() PublicreportNuisanceImageMod {
	return PublicreportNuisanceImageModFunc(func(ctx context.Context, o *PublicreportNuisanceImageTemplate) {
		o.r.Nuisance = nil
	})
}
