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

type NoteAudioDatumModFunc func(context.Context, *NoteAudioDatumTemplate)

func (f NoteAudioDatumModFunc) Apply(ctx context.Context, n *NoteAudioDatumTemplate) {
	f(ctx, n)
}

type NoteAudioDatumModSlice []NoteAudioDatumMod

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

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

	r noteAudioDatumR
	f *Factory

	alreadyPersisted bool
}

type noteAudioDatumR struct {
	NoteAudio *noteAudioDatumRNoteAudioR
}

type noteAudioDatumRNoteAudioR struct {
	o *NoteAudioTemplate
}

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

// setModelRels creates and sets the relationships on *models.NoteAudioDatum
// according to the relationships in the template. Nothing is inserted into the db
func (t NoteAudioDatumTemplate) setModelRels(o *models.NoteAudioDatum) {
	if t.r.NoteAudio != nil {
		rel := t.r.NoteAudio.o.Build()
		rel.R.NoteAudioData = append(rel.R.NoteAudioData, o)
		o.NoteAudioVersion = rel.Version // h2
		o.NoteAudioUUID = rel.UUID       // h2
		o.R.NoteAudio = rel
	}
}

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

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

	return m
}

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

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

	return m
}

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

	if o.Created != nil {
		m.Created = o.Created()
	}
	if o.NoteAudioVersion != nil {
		m.NoteAudioVersion = o.NoteAudioVersion()
	}
	if o.NoteAudioUUID != nil {
		m.NoteAudioUUID = o.NoteAudioUUID()
	}
	if o.Type != nil {
		m.Type = o.Type()
	}

	o.setModelRels(m)

	return m
}

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

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

	return m
}

func ensureCreatableNoteAudioDatum(m *models.NoteAudioDatumSetter) {
	if !(m.Created.IsValue()) {
		val := random_time_Time(nil)
		m.Created = omit.From(val)
	}
	if !(m.NoteAudioVersion.IsValue()) {
		val := random_int32(nil)
		m.NoteAudioVersion = omit.From(val)
	}
	if !(m.NoteAudioUUID.IsValue()) {
		val := random_uuid_UUID(nil)
		m.NoteAudioUUID = 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.NoteAudioDatum
// according to the relationships in the template.
// any required relationship should have already exist on the model
func (o *NoteAudioDatumTemplate) insertOptRels(ctx context.Context, exec bob.Executor, m *models.NoteAudioDatum) error {
	var err error

	return err
}

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

	if o.r.NoteAudio == nil {
		NoteAudioDatumMods.WithNewNoteAudio().Apply(ctx, o)
	}

	var rel0 *models.NoteAudio

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

	opt.NoteAudioVersion = omit.From(rel0.Version)
	opt.NoteAudioUUID = omit.From(rel0.UUID)

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

	m.R.NoteAudio = rel0

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

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

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

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

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

	return m, nil
}

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

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

// NoteAudioDatum has methods that act as mods for the NoteAudioDatumTemplate
var NoteAudioDatumMods noteAudioDatumMods

type noteAudioDatumMods struct{}

func (m noteAudioDatumMods) RandomizeAllColumns(f *faker.Faker) NoteAudioDatumMod {
	return NoteAudioDatumModSlice{
		NoteAudioDatumMods.RandomCreated(f),
		NoteAudioDatumMods.RandomNoteAudioVersion(f),
		NoteAudioDatumMods.RandomNoteAudioUUID(f),
		NoteAudioDatumMods.RandomType(f),
	}
}

// Set the model columns to this value
func (m noteAudioDatumMods) Created(val time.Time) NoteAudioDatumMod {
	return NoteAudioDatumModFunc(func(_ context.Context, o *NoteAudioDatumTemplate) {
		o.Created = func() time.Time { return val }
	})
}

// Set the Column from the function
func (m noteAudioDatumMods) CreatedFunc(f func() time.Time) NoteAudioDatumMod {
	return NoteAudioDatumModFunc(func(_ context.Context, o *NoteAudioDatumTemplate) {
		o.Created = f
	})
}

// Clear any values for the column
func (m noteAudioDatumMods) UnsetCreated() NoteAudioDatumMod {
	return NoteAudioDatumModFunc(func(_ context.Context, o *NoteAudioDatumTemplate) {
		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 noteAudioDatumMods) RandomCreated(f *faker.Faker) NoteAudioDatumMod {
	return NoteAudioDatumModFunc(func(_ context.Context, o *NoteAudioDatumTemplate) {
		o.Created = func() time.Time {
			return random_time_Time(f)
		}
	})
}

// Set the model columns to this value
func (m noteAudioDatumMods) NoteAudioVersion(val int32) NoteAudioDatumMod {
	return NoteAudioDatumModFunc(func(_ context.Context, o *NoteAudioDatumTemplate) {
		o.NoteAudioVersion = func() int32 { return val }
	})
}

// Set the Column from the function
func (m noteAudioDatumMods) NoteAudioVersionFunc(f func() int32) NoteAudioDatumMod {
	return NoteAudioDatumModFunc(func(_ context.Context, o *NoteAudioDatumTemplate) {
		o.NoteAudioVersion = f
	})
}

// Clear any values for the column
func (m noteAudioDatumMods) UnsetNoteAudioVersion() NoteAudioDatumMod {
	return NoteAudioDatumModFunc(func(_ context.Context, o *NoteAudioDatumTemplate) {
		o.NoteAudioVersion = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m noteAudioDatumMods) RandomNoteAudioVersion(f *faker.Faker) NoteAudioDatumMod {
	return NoteAudioDatumModFunc(func(_ context.Context, o *NoteAudioDatumTemplate) {
		o.NoteAudioVersion = func() int32 {
			return random_int32(f)
		}
	})
}

// Set the model columns to this value
func (m noteAudioDatumMods) NoteAudioUUID(val uuid.UUID) NoteAudioDatumMod {
	return NoteAudioDatumModFunc(func(_ context.Context, o *NoteAudioDatumTemplate) {
		o.NoteAudioUUID = func() uuid.UUID { return val }
	})
}

// Set the Column from the function
func (m noteAudioDatumMods) NoteAudioUUIDFunc(f func() uuid.UUID) NoteAudioDatumMod {
	return NoteAudioDatumModFunc(func(_ context.Context, o *NoteAudioDatumTemplate) {
		o.NoteAudioUUID = f
	})
}

// Clear any values for the column
func (m noteAudioDatumMods) UnsetNoteAudioUUID() NoteAudioDatumMod {
	return NoteAudioDatumModFunc(func(_ context.Context, o *NoteAudioDatumTemplate) {
		o.NoteAudioUUID = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m noteAudioDatumMods) RandomNoteAudioUUID(f *faker.Faker) NoteAudioDatumMod {
	return NoteAudioDatumModFunc(func(_ context.Context, o *NoteAudioDatumTemplate) {
		o.NoteAudioUUID = func() uuid.UUID {
			return random_uuid_UUID(f)
		}
	})
}

// Set the model columns to this value
func (m noteAudioDatumMods) Type(val enums.Audiodatatype) NoteAudioDatumMod {
	return NoteAudioDatumModFunc(func(_ context.Context, o *NoteAudioDatumTemplate) {
		o.Type = func() enums.Audiodatatype { return val }
	})
}

// Set the Column from the function
func (m noteAudioDatumMods) TypeFunc(f func() enums.Audiodatatype) NoteAudioDatumMod {
	return NoteAudioDatumModFunc(func(_ context.Context, o *NoteAudioDatumTemplate) {
		o.Type = f
	})
}

// Clear any values for the column
func (m noteAudioDatumMods) UnsetType() NoteAudioDatumMod {
	return NoteAudioDatumModFunc(func(_ context.Context, o *NoteAudioDatumTemplate) {
		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 noteAudioDatumMods) RandomType(f *faker.Faker) NoteAudioDatumMod {
	return NoteAudioDatumModFunc(func(_ context.Context, o *NoteAudioDatumTemplate) {
		o.Type = func() enums.Audiodatatype {
			return random_enums_Audiodatatype(f)
		}
	})
}

func (m noteAudioDatumMods) WithParentsCascading() NoteAudioDatumMod {
	return NoteAudioDatumModFunc(func(ctx context.Context, o *NoteAudioDatumTemplate) {
		if isDone, _ := noteAudioDatumWithParentsCascadingCtx.Value(ctx); isDone {
			return
		}
		ctx = noteAudioDatumWithParentsCascadingCtx.WithValue(ctx, true)
		{

			related := o.f.NewNoteAudioWithContext(ctx, NoteAudioMods.WithParentsCascading())
			m.WithNoteAudio(related).Apply(ctx, o)
		}
	})
}

func (m noteAudioDatumMods) WithNoteAudio(rel *NoteAudioTemplate) NoteAudioDatumMod {
	return NoteAudioDatumModFunc(func(ctx context.Context, o *NoteAudioDatumTemplate) {
		o.r.NoteAudio = &noteAudioDatumRNoteAudioR{
			o: rel,
		}
	})
}

func (m noteAudioDatumMods) WithNewNoteAudio(mods ...NoteAudioMod) NoteAudioDatumMod {
	return NoteAudioDatumModFunc(func(ctx context.Context, o *NoteAudioDatumTemplate) {
		related := o.f.NewNoteAudioWithContext(ctx, mods...)

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

func (m noteAudioDatumMods) WithExistingNoteAudio(em *models.NoteAudio) NoteAudioDatumMod {
	return NoteAudioDatumModFunc(func(ctx context.Context, o *NoteAudioDatumTemplate) {
		o.r.NoteAudio = &noteAudioDatumRNoteAudioR{
			o: o.f.FromExistingNoteAudio(em),
		}
	})
}

func (m noteAudioDatumMods) WithoutNoteAudio() NoteAudioDatumMod {
	return NoteAudioDatumModFunc(func(ctx context.Context, o *NoteAudioDatumTemplate) {
		o.r.NoteAudio = nil
	})
}
