// 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"
	"time"

	"github.com/Gleipnir-Technology/bob"
	enums "github.com/Gleipnir-Technology/nidus-sync/db/enums"
	models "github.com/Gleipnir-Technology/nidus-sync/db/models"
	"github.com/aarondl/opt/omit"
	"github.com/google/uuid"
	"github.com/jaswdr/faker/v2"
)

type NoteImageDatumMod interface {
	Apply(context.Context, *NoteImageDatumTemplate)
}

type NoteImageDatumModFunc func(context.Context, *NoteImageDatumTemplate)

func (f NoteImageDatumModFunc) Apply(ctx context.Context, n *NoteImageDatumTemplate) {
	f(ctx, n)
}

type NoteImageDatumModSlice []NoteImageDatumMod

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

// NoteImageDatumTemplate is an object representing the database table.
// all columns are optional and should be set by mods
type NoteImageDatumTemplate struct {
	Created          func() time.Time
	NoteImageVersion func() int32
	NoteImageUUID    func() uuid.UUID
	Type             func() enums.Audiodatatype

	r noteImageDatumR
	f *Factory

	alreadyPersisted bool
}

type noteImageDatumR struct {
	NoteImage *noteImageDatumRNoteImageR
}

type noteImageDatumRNoteImageR struct {
	o *NoteImageTemplate
}

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

// setModelRels creates and sets the relationships on *models.NoteImageDatum
// according to the relationships in the template. Nothing is inserted into the db
func (t NoteImageDatumTemplate) setModelRels(o *models.NoteImageDatum) {
	if t.r.NoteImage != nil {
		rel := t.r.NoteImage.o.Build()
		rel.R.NoteImageData = append(rel.R.NoteImageData, o)
		o.NoteImageVersion = rel.Version // h2
		o.NoteImageUUID = rel.UUID       // h2
		o.R.NoteImage = rel
	}
}

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

	if o.Created != nil {
		val := o.Created()
		m.Created = omit.From(val)
	}
	if o.NoteImageVersion != nil {
		val := o.NoteImageVersion()
		m.NoteImageVersion = omit.From(val)
	}
	if o.NoteImageUUID != nil {
		val := o.NoteImageUUID()
		m.NoteImageUUID = omit.From(val)
	}
	if o.Type != nil {
		val := o.Type()
		m.Type = omit.From(val)
	}

	return m
}

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

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

	return m
}

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

	if o.Created != nil {
		m.Created = o.Created()
	}
	if o.NoteImageVersion != nil {
		m.NoteImageVersion = o.NoteImageVersion()
	}
	if o.NoteImageUUID != nil {
		m.NoteImageUUID = o.NoteImageUUID()
	}
	if o.Type != nil {
		m.Type = o.Type()
	}

	o.setModelRels(m)

	return m
}

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

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

	return m
}

func ensureCreatableNoteImageDatum(m *models.NoteImageDatumSetter) {
	if !(m.Created.IsValue()) {
		val := random_time_Time(nil)
		m.Created = omit.From(val)
	}
	if !(m.NoteImageVersion.IsValue()) {
		val := random_int32(nil)
		m.NoteImageVersion = omit.From(val)
	}
	if !(m.NoteImageUUID.IsValue()) {
		val := random_uuid_UUID(nil)
		m.NoteImageUUID = omit.From(val)
	}
	if !(m.Type.IsValue()) {
		val := random_enums_Audiodatatype(nil)
		m.Type = omit.From(val)
	}
}

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

	return err
}

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

	if o.r.NoteImage == nil {
		NoteImageDatumMods.WithNewNoteImage().Apply(ctx, o)
	}

	var rel0 *models.NoteImage

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

	opt.NoteImageVersion = omit.From(rel0.Version)
	opt.NoteImageUUID = omit.From(rel0.UUID)

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

	m.R.NoteImage = rel0

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

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

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

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

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

	return m, nil
}

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

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

// NoteImageDatum has methods that act as mods for the NoteImageDatumTemplate
var NoteImageDatumMods noteImageDatumMods

type noteImageDatumMods struct{}

func (m noteImageDatumMods) RandomizeAllColumns(f *faker.Faker) NoteImageDatumMod {
	return NoteImageDatumModSlice{
		NoteImageDatumMods.RandomCreated(f),
		NoteImageDatumMods.RandomNoteImageVersion(f),
		NoteImageDatumMods.RandomNoteImageUUID(f),
		NoteImageDatumMods.RandomType(f),
	}
}

// Set the model columns to this value
func (m noteImageDatumMods) Created(val time.Time) NoteImageDatumMod {
	return NoteImageDatumModFunc(func(_ context.Context, o *NoteImageDatumTemplate) {
		o.Created = func() time.Time { return val }
	})
}

// Set the Column from the function
func (m noteImageDatumMods) CreatedFunc(f func() time.Time) NoteImageDatumMod {
	return NoteImageDatumModFunc(func(_ context.Context, o *NoteImageDatumTemplate) {
		o.Created = f
	})
}

// Clear any values for the column
func (m noteImageDatumMods) UnsetCreated() NoteImageDatumMod {
	return NoteImageDatumModFunc(func(_ context.Context, o *NoteImageDatumTemplate) {
		o.Created = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m noteImageDatumMods) RandomCreated(f *faker.Faker) NoteImageDatumMod {
	return NoteImageDatumModFunc(func(_ context.Context, o *NoteImageDatumTemplate) {
		o.Created = func() time.Time {
			return random_time_Time(f)
		}
	})
}

// Set the model columns to this value
func (m noteImageDatumMods) NoteImageVersion(val int32) NoteImageDatumMod {
	return NoteImageDatumModFunc(func(_ context.Context, o *NoteImageDatumTemplate) {
		o.NoteImageVersion = func() int32 { return val }
	})
}

// Set the Column from the function
func (m noteImageDatumMods) NoteImageVersionFunc(f func() int32) NoteImageDatumMod {
	return NoteImageDatumModFunc(func(_ context.Context, o *NoteImageDatumTemplate) {
		o.NoteImageVersion = f
	})
}

// Clear any values for the column
func (m noteImageDatumMods) UnsetNoteImageVersion() NoteImageDatumMod {
	return NoteImageDatumModFunc(func(_ context.Context, o *NoteImageDatumTemplate) {
		o.NoteImageVersion = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m noteImageDatumMods) RandomNoteImageVersion(f *faker.Faker) NoteImageDatumMod {
	return NoteImageDatumModFunc(func(_ context.Context, o *NoteImageDatumTemplate) {
		o.NoteImageVersion = func() int32 {
			return random_int32(f)
		}
	})
}

// Set the model columns to this value
func (m noteImageDatumMods) NoteImageUUID(val uuid.UUID) NoteImageDatumMod {
	return NoteImageDatumModFunc(func(_ context.Context, o *NoteImageDatumTemplate) {
		o.NoteImageUUID = func() uuid.UUID { return val }
	})
}

// Set the Column from the function
func (m noteImageDatumMods) NoteImageUUIDFunc(f func() uuid.UUID) NoteImageDatumMod {
	return NoteImageDatumModFunc(func(_ context.Context, o *NoteImageDatumTemplate) {
		o.NoteImageUUID = f
	})
}

// Clear any values for the column
func (m noteImageDatumMods) UnsetNoteImageUUID() NoteImageDatumMod {
	return NoteImageDatumModFunc(func(_ context.Context, o *NoteImageDatumTemplate) {
		o.NoteImageUUID = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m noteImageDatumMods) RandomNoteImageUUID(f *faker.Faker) NoteImageDatumMod {
	return NoteImageDatumModFunc(func(_ context.Context, o *NoteImageDatumTemplate) {
		o.NoteImageUUID = func() uuid.UUID {
			return random_uuid_UUID(f)
		}
	})
}

// Set the model columns to this value
func (m noteImageDatumMods) Type(val enums.Audiodatatype) NoteImageDatumMod {
	return NoteImageDatumModFunc(func(_ context.Context, o *NoteImageDatumTemplate) {
		o.Type = func() enums.Audiodatatype { return val }
	})
}

// Set the Column from the function
func (m noteImageDatumMods) TypeFunc(f func() enums.Audiodatatype) NoteImageDatumMod {
	return NoteImageDatumModFunc(func(_ context.Context, o *NoteImageDatumTemplate) {
		o.Type = f
	})
}

// Clear any values for the column
func (m noteImageDatumMods) UnsetType() NoteImageDatumMod {
	return NoteImageDatumModFunc(func(_ context.Context, o *NoteImageDatumTemplate) {
		o.Type = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m noteImageDatumMods) RandomType(f *faker.Faker) NoteImageDatumMod {
	return NoteImageDatumModFunc(func(_ context.Context, o *NoteImageDatumTemplate) {
		o.Type = func() enums.Audiodatatype {
			return random_enums_Audiodatatype(f)
		}
	})
}

func (m noteImageDatumMods) WithParentsCascading() NoteImageDatumMod {
	return NoteImageDatumModFunc(func(ctx context.Context, o *NoteImageDatumTemplate) {
		if isDone, _ := noteImageDatumWithParentsCascadingCtx.Value(ctx); isDone {
			return
		}
		ctx = noteImageDatumWithParentsCascadingCtx.WithValue(ctx, true)
		{

			related := o.f.NewNoteImageWithContext(ctx, NoteImageMods.WithParentsCascading())
			m.WithNoteImage(related).Apply(ctx, o)
		}
	})
}

func (m noteImageDatumMods) WithNoteImage(rel *NoteImageTemplate) NoteImageDatumMod {
	return NoteImageDatumModFunc(func(ctx context.Context, o *NoteImageDatumTemplate) {
		o.r.NoteImage = &noteImageDatumRNoteImageR{
			o: rel,
		}
	})
}

func (m noteImageDatumMods) WithNewNoteImage(mods ...NoteImageMod) NoteImageDatumMod {
	return NoteImageDatumModFunc(func(ctx context.Context, o *NoteImageDatumTemplate) {
		related := o.f.NewNoteImageWithContext(ctx, mods...)

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

func (m noteImageDatumMods) WithExistingNoteImage(em *models.NoteImage) NoteImageDatumMod {
	return NoteImageDatumModFunc(func(ctx context.Context, o *NoteImageDatumTemplate) {
		o.r.NoteImage = &noteImageDatumRNoteImageR{
			o: o.f.FromExistingNoteImage(em),
		}
	})
}

func (m noteImageDatumMods) WithoutNoteImage() NoteImageDatumMod {
	return NoteImageDatumModFunc(func(ctx context.Context, o *NoteImageDatumTemplate) {
		o.r.NoteImage = nil
	})
}
