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

type PublicreportQuickImageModFunc func(context.Context, *PublicreportQuickImageTemplate)

func (f PublicreportQuickImageModFunc) Apply(ctx context.Context, n *PublicreportQuickImageTemplate) {
	f(ctx, n)
}

type PublicreportQuickImageModSlice []PublicreportQuickImageMod

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

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

	r publicreportQuickImageR
	f *Factory

	alreadyPersisted bool
}

type publicreportQuickImageR struct {
	Image *publicreportQuickImageRImageR
	Quick *publicreportQuickImageRQuickR
}

type publicreportQuickImageRImageR struct {
	o *PublicreportImageTemplate
}
type publicreportQuickImageRQuickR struct {
	o *PublicreportQuickTemplate
}

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

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

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

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

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

	return m
}

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

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

	return m
}

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

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

	o.setModelRels(m)

	return m
}

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

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

	return m
}

func ensureCreatablePublicreportQuickImage(m *models.PublicreportQuickImageSetter) {
	if !(m.ImageID.IsValue()) {
		val := random_int32(nil)
		m.ImageID = omit.From(val)
	}
	if !(m.QuickID.IsValue()) {
		val := random_int32(nil)
		m.QuickID = omit.From(val)
	}
}

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

	return err
}

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

	if o.r.Image == nil {
		PublicreportQuickImageMods.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.Quick == nil {
		PublicreportQuickImageMods.WithNewQuick().Apply(ctx, o)
	}

	var rel1 *models.PublicreportQuick

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

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

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

	m.R.Image = rel0
	m.R.Quick = rel1

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

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

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

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

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

	return m, nil
}

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

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

// PublicreportQuickImage has methods that act as mods for the PublicreportQuickImageTemplate
var PublicreportQuickImageMods publicreportQuickImageMods

type publicreportQuickImageMods struct{}

func (m publicreportQuickImageMods) RandomizeAllColumns(f *faker.Faker) PublicreportQuickImageMod {
	return PublicreportQuickImageModSlice{
		PublicreportQuickImageMods.RandomImageID(f),
		PublicreportQuickImageMods.RandomQuickID(f),
	}
}

// Set the model columns to this value
func (m publicreportQuickImageMods) ImageID(val int32) PublicreportQuickImageMod {
	return PublicreportQuickImageModFunc(func(_ context.Context, o *PublicreportQuickImageTemplate) {
		o.ImageID = func() int32 { return val }
	})
}

// Set the Column from the function
func (m publicreportQuickImageMods) ImageIDFunc(f func() int32) PublicreportQuickImageMod {
	return PublicreportQuickImageModFunc(func(_ context.Context, o *PublicreportQuickImageTemplate) {
		o.ImageID = f
	})
}

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

// Set the model columns to this value
func (m publicreportQuickImageMods) QuickID(val int32) PublicreportQuickImageMod {
	return PublicreportQuickImageModFunc(func(_ context.Context, o *PublicreportQuickImageTemplate) {
		o.QuickID = func() int32 { return val }
	})
}

// Set the Column from the function
func (m publicreportQuickImageMods) QuickIDFunc(f func() int32) PublicreportQuickImageMod {
	return PublicreportQuickImageModFunc(func(_ context.Context, o *PublicreportQuickImageTemplate) {
		o.QuickID = f
	})
}

// Clear any values for the column
func (m publicreportQuickImageMods) UnsetQuickID() PublicreportQuickImageMod {
	return PublicreportQuickImageModFunc(func(_ context.Context, o *PublicreportQuickImageTemplate) {
		o.QuickID = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m publicreportQuickImageMods) RandomQuickID(f *faker.Faker) PublicreportQuickImageMod {
	return PublicreportQuickImageModFunc(func(_ context.Context, o *PublicreportQuickImageTemplate) {
		o.QuickID = func() int32 {
			return random_int32(f)
		}
	})
}

func (m publicreportQuickImageMods) WithParentsCascading() PublicreportQuickImageMod {
	return PublicreportQuickImageModFunc(func(ctx context.Context, o *PublicreportQuickImageTemplate) {
		if isDone, _ := publicreportQuickImageWithParentsCascadingCtx.Value(ctx); isDone {
			return
		}
		ctx = publicreportQuickImageWithParentsCascadingCtx.WithValue(ctx, true)
		{

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

			related := o.f.NewPublicreportQuickWithContext(ctx, PublicreportQuickMods.WithParentsCascading())
			m.WithQuick(related).Apply(ctx, o)
		}
	})
}

func (m publicreportQuickImageMods) WithImage(rel *PublicreportImageTemplate) PublicreportQuickImageMod {
	return PublicreportQuickImageModFunc(func(ctx context.Context, o *PublicreportQuickImageTemplate) {
		o.r.Image = &publicreportQuickImageRImageR{
			o: rel,
		}
	})
}

func (m publicreportQuickImageMods) WithNewImage(mods ...PublicreportImageMod) PublicreportQuickImageMod {
	return PublicreportQuickImageModFunc(func(ctx context.Context, o *PublicreportQuickImageTemplate) {
		related := o.f.NewPublicreportImageWithContext(ctx, mods...)

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

func (m publicreportQuickImageMods) WithExistingImage(em *models.PublicreportImage) PublicreportQuickImageMod {
	return PublicreportQuickImageModFunc(func(ctx context.Context, o *PublicreportQuickImageTemplate) {
		o.r.Image = &publicreportQuickImageRImageR{
			o: o.f.FromExistingPublicreportImage(em),
		}
	})
}

func (m publicreportQuickImageMods) WithoutImage() PublicreportQuickImageMod {
	return PublicreportQuickImageModFunc(func(ctx context.Context, o *PublicreportQuickImageTemplate) {
		o.r.Image = nil
	})
}

func (m publicreportQuickImageMods) WithQuick(rel *PublicreportQuickTemplate) PublicreportQuickImageMod {
	return PublicreportQuickImageModFunc(func(ctx context.Context, o *PublicreportQuickImageTemplate) {
		o.r.Quick = &publicreportQuickImageRQuickR{
			o: rel,
		}
	})
}

func (m publicreportQuickImageMods) WithNewQuick(mods ...PublicreportQuickMod) PublicreportQuickImageMod {
	return PublicreportQuickImageModFunc(func(ctx context.Context, o *PublicreportQuickImageTemplate) {
		related := o.f.NewPublicreportQuickWithContext(ctx, mods...)

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

func (m publicreportQuickImageMods) WithExistingQuick(em *models.PublicreportQuick) PublicreportQuickImageMod {
	return PublicreportQuickImageModFunc(func(ctx context.Context, o *PublicreportQuickImageTemplate) {
		o.r.Quick = &publicreportQuickImageRQuickR{
			o: o.f.FromExistingPublicreportQuick(em),
		}
	})
}

func (m publicreportQuickImageMods) WithoutQuick() PublicreportQuickImageMod {
	return PublicreportQuickImageModFunc(func(ctx context.Context, o *PublicreportQuickImageTemplate) {
		o.r.Quick = nil
	})
}
