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

type PublicreportImageExifModFunc func(context.Context, *PublicreportImageExifTemplate)

func (f PublicreportImageExifModFunc) Apply(ctx context.Context, n *PublicreportImageExifTemplate) {
	f(ctx, n)
}

type PublicreportImageExifModSlice []PublicreportImageExifMod

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

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

	r publicreportImageExifR
	f *Factory

	alreadyPersisted bool
}

type publicreportImageExifR struct {
	Image *publicreportImageExifRImageR
}

type publicreportImageExifRImageR struct {
	o *PublicreportImageTemplate
}

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

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

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

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

	return m
}

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

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

	return m
}

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

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

	o.setModelRels(m)

	return m
}

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

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

	return m
}

func ensureCreatablePublicreportImageExif(m *models.PublicreportImageExifSetter) {
	if !(m.ImageID.IsValue()) {
		val := random_int32(nil)
		m.ImageID = omit.From(val)
	}
	if !(m.Name.IsValue()) {
		val := random_string(nil)
		m.Name = omit.From(val)
	}
	if !(m.Value.IsValue()) {
		val := random_string(nil)
		m.Value = omit.From(val)
	}
}

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

	return err
}

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

	if o.r.Image == nil {
		PublicreportImageExifMods.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)

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

	m.R.Image = rel0

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

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

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

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

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

	return m, nil
}

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

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

// PublicreportImageExif has methods that act as mods for the PublicreportImageExifTemplate
var PublicreportImageExifMods publicreportImageExifMods

type publicreportImageExifMods struct{}

func (m publicreportImageExifMods) RandomizeAllColumns(f *faker.Faker) PublicreportImageExifMod {
	return PublicreportImageExifModSlice{
		PublicreportImageExifMods.RandomImageID(f),
		PublicreportImageExifMods.RandomName(f),
		PublicreportImageExifMods.RandomValue(f),
	}
}

// Set the model columns to this value
func (m publicreportImageExifMods) ImageID(val int32) PublicreportImageExifMod {
	return PublicreportImageExifModFunc(func(_ context.Context, o *PublicreportImageExifTemplate) {
		o.ImageID = func() int32 { return val }
	})
}

// Set the Column from the function
func (m publicreportImageExifMods) ImageIDFunc(f func() int32) PublicreportImageExifMod {
	return PublicreportImageExifModFunc(func(_ context.Context, o *PublicreportImageExifTemplate) {
		o.ImageID = f
	})
}

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

// Set the model columns to this value
func (m publicreportImageExifMods) Name(val string) PublicreportImageExifMod {
	return PublicreportImageExifModFunc(func(_ context.Context, o *PublicreportImageExifTemplate) {
		o.Name = func() string { return val }
	})
}

// Set the Column from the function
func (m publicreportImageExifMods) NameFunc(f func() string) PublicreportImageExifMod {
	return PublicreportImageExifModFunc(func(_ context.Context, o *PublicreportImageExifTemplate) {
		o.Name = f
	})
}

// Clear any values for the column
func (m publicreportImageExifMods) UnsetName() PublicreportImageExifMod {
	return PublicreportImageExifModFunc(func(_ context.Context, o *PublicreportImageExifTemplate) {
		o.Name = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m publicreportImageExifMods) RandomName(f *faker.Faker) PublicreportImageExifMod {
	return PublicreportImageExifModFunc(func(_ context.Context, o *PublicreportImageExifTemplate) {
		o.Name = func() string {
			return random_string(f)
		}
	})
}

// Set the model columns to this value
func (m publicreportImageExifMods) Value(val string) PublicreportImageExifMod {
	return PublicreportImageExifModFunc(func(_ context.Context, o *PublicreportImageExifTemplate) {
		o.Value = func() string { return val }
	})
}

// Set the Column from the function
func (m publicreportImageExifMods) ValueFunc(f func() string) PublicreportImageExifMod {
	return PublicreportImageExifModFunc(func(_ context.Context, o *PublicreportImageExifTemplate) {
		o.Value = f
	})
}

// Clear any values for the column
func (m publicreportImageExifMods) UnsetValue() PublicreportImageExifMod {
	return PublicreportImageExifModFunc(func(_ context.Context, o *PublicreportImageExifTemplate) {
		o.Value = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m publicreportImageExifMods) RandomValue(f *faker.Faker) PublicreportImageExifMod {
	return PublicreportImageExifModFunc(func(_ context.Context, o *PublicreportImageExifTemplate) {
		o.Value = func() string {
			return random_string(f)
		}
	})
}

func (m publicreportImageExifMods) WithParentsCascading() PublicreportImageExifMod {
	return PublicreportImageExifModFunc(func(ctx context.Context, o *PublicreportImageExifTemplate) {
		if isDone, _ := publicreportImageExifWithParentsCascadingCtx.Value(ctx); isDone {
			return
		}
		ctx = publicreportImageExifWithParentsCascadingCtx.WithValue(ctx, true)
		{

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

func (m publicreportImageExifMods) WithImage(rel *PublicreportImageTemplate) PublicreportImageExifMod {
	return PublicreportImageExifModFunc(func(ctx context.Context, o *PublicreportImageExifTemplate) {
		o.r.Image = &publicreportImageExifRImageR{
			o: rel,
		}
	})
}

func (m publicreportImageExifMods) WithNewImage(mods ...PublicreportImageMod) PublicreportImageExifMod {
	return PublicreportImageExifModFunc(func(ctx context.Context, o *PublicreportImageExifTemplate) {
		related := o.f.NewPublicreportImageWithContext(ctx, mods...)

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

func (m publicreportImageExifMods) WithExistingImage(em *models.PublicreportImage) PublicreportImageExifMod {
	return PublicreportImageExifModFunc(func(ctx context.Context, o *PublicreportImageExifTemplate) {
		o.r.Image = &publicreportImageExifRImageR{
			o: o.f.FromExistingPublicreportImage(em),
		}
	})
}

func (m publicreportImageExifMods) WithoutImage() PublicreportImageExifMod {
	return PublicreportImageExifModFunc(func(ctx context.Context, o *PublicreportImageExifTemplate) {
		o.r.Image = nil
	})
}
