// 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/null"
	"github.com/aarondl/opt/omit"
	"github.com/aarondl/opt/omitnull"
	"github.com/jaswdr/faker/v2"
)

type FileuploadCSVMod interface {
	Apply(context.Context, *FileuploadCSVTemplate)
}

type FileuploadCSVModFunc func(context.Context, *FileuploadCSVTemplate)

func (f FileuploadCSVModFunc) Apply(ctx context.Context, n *FileuploadCSVTemplate) {
	f(ctx, n)
}

type FileuploadCSVModSlice []FileuploadCSVMod

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

// FileuploadCSVTemplate is an object representing the database table.
// all columns are optional and should be set by mods
type FileuploadCSVTemplate struct {
	Committed func() null.Val[time.Time]
	FileID    func() int32
	Rowcount  func() int32
	Type      func() enums.FileuploadCsvtype

	r fileuploadCSVR
	f *Factory

	alreadyPersisted bool
}

type fileuploadCSVR struct {
	File             *fileuploadCSVRFileR
	CSVFileErrorCSVS []*fileuploadCSVRCSVFileErrorCSVSR
	CSVFilePools     []*fileuploadCSVRCSVFilePoolsR
}

type fileuploadCSVRFileR struct {
	o *FileuploadFileTemplate
}
type fileuploadCSVRCSVFileErrorCSVSR struct {
	number int
	o      *FileuploadErrorCSVTemplate
}
type fileuploadCSVRCSVFilePoolsR struct {
	number int
	o      *FileuploadPoolTemplate
}

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

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

	if t.r.CSVFileErrorCSVS != nil {
		rel := models.FileuploadErrorCSVSlice{}
		for _, r := range t.r.CSVFileErrorCSVS {
			related := r.o.BuildMany(r.number)
			for _, rel := range related {
				rel.CSVFileID = o.FileID // h2
				rel.R.CSVFileCSV = o
			}
			rel = append(rel, related...)
		}
		o.R.CSVFileErrorCSVS = rel
	}

	if t.r.CSVFilePools != nil {
		rel := models.FileuploadPoolSlice{}
		for _, r := range t.r.CSVFilePools {
			related := r.o.BuildMany(r.number)
			for _, rel := range related {
				rel.CSVFile = o.FileID // h2
				rel.R.CSVFileCSV = o
			}
			rel = append(rel, related...)
		}
		o.R.CSVFilePools = rel
	}
}

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

	if o.Committed != nil {
		val := o.Committed()
		m.Committed = omitnull.FromNull(val)
	}
	if o.FileID != nil {
		val := o.FileID()
		m.FileID = omit.From(val)
	}
	if o.Rowcount != nil {
		val := o.Rowcount()
		m.Rowcount = omit.From(val)
	}
	if o.Type != nil {
		val := o.Type()
		m.Type = omit.From(val)
	}

	return m
}

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

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

	return m
}

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

	if o.Committed != nil {
		m.Committed = o.Committed()
	}
	if o.FileID != nil {
		m.FileID = o.FileID()
	}
	if o.Rowcount != nil {
		m.Rowcount = o.Rowcount()
	}
	if o.Type != nil {
		m.Type = o.Type()
	}

	o.setModelRels(m)

	return m
}

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

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

	return m
}

func ensureCreatableFileuploadCSV(m *models.FileuploadCSVSetter) {
	if !(m.FileID.IsValue()) {
		val := random_int32(nil)
		m.FileID = omit.From(val)
	}
	if !(m.Rowcount.IsValue()) {
		val := random_int32(nil)
		m.Rowcount = omit.From(val)
	}
	if !(m.Type.IsValue()) {
		val := random_enums_FileuploadCsvtype(nil)
		m.Type = omit.From(val)
	}
}

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

	isCSVFileErrorCSVSDone, _ := fileuploadCSVRelCSVFileErrorCSVSCtx.Value(ctx)
	if !isCSVFileErrorCSVSDone && o.r.CSVFileErrorCSVS != nil {
		ctx = fileuploadCSVRelCSVFileErrorCSVSCtx.WithValue(ctx, true)
		for _, r := range o.r.CSVFileErrorCSVS {
			if r.o.alreadyPersisted {
				m.R.CSVFileErrorCSVS = append(m.R.CSVFileErrorCSVS, r.o.Build())
			} else {
				rel1, err := r.o.CreateMany(ctx, exec, r.number)
				if err != nil {
					return err
				}

				err = m.AttachCSVFileErrorCSVS(ctx, exec, rel1...)
				if err != nil {
					return err
				}
			}
		}
	}

	isCSVFilePoolsDone, _ := fileuploadCSVRelCSVFilePoolsCtx.Value(ctx)
	if !isCSVFilePoolsDone && o.r.CSVFilePools != nil {
		ctx = fileuploadCSVRelCSVFilePoolsCtx.WithValue(ctx, true)
		for _, r := range o.r.CSVFilePools {
			if r.o.alreadyPersisted {
				m.R.CSVFilePools = append(m.R.CSVFilePools, r.o.Build())
			} else {
				rel2, err := r.o.CreateMany(ctx, exec, r.number)
				if err != nil {
					return err
				}

				err = m.AttachCSVFilePools(ctx, exec, rel2...)
				if err != nil {
					return err
				}
			}
		}
	}

	return err
}

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

	if o.r.File == nil {
		FileuploadCSVMods.WithNewFile().Apply(ctx, o)
	}

	var rel0 *models.FileuploadFile

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

	opt.FileID = omit.From(rel0.ID)

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

	m.R.File = rel0

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

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

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

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

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

	return m, nil
}

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

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

// FileuploadCSV has methods that act as mods for the FileuploadCSVTemplate
var FileuploadCSVMods fileuploadCSVMods

type fileuploadCSVMods struct{}

func (m fileuploadCSVMods) RandomizeAllColumns(f *faker.Faker) FileuploadCSVMod {
	return FileuploadCSVModSlice{
		FileuploadCSVMods.RandomCommitted(f),
		FileuploadCSVMods.RandomFileID(f),
		FileuploadCSVMods.RandomRowcount(f),
		FileuploadCSVMods.RandomType(f),
	}
}

// Set the model columns to this value
func (m fileuploadCSVMods) Committed(val null.Val[time.Time]) FileuploadCSVMod {
	return FileuploadCSVModFunc(func(_ context.Context, o *FileuploadCSVTemplate) {
		o.Committed = func() null.Val[time.Time] { return val }
	})
}

// Set the Column from the function
func (m fileuploadCSVMods) CommittedFunc(f func() null.Val[time.Time]) FileuploadCSVMod {
	return FileuploadCSVModFunc(func(_ context.Context, o *FileuploadCSVTemplate) {
		o.Committed = f
	})
}

// Clear any values for the column
func (m fileuploadCSVMods) UnsetCommitted() FileuploadCSVMod {
	return FileuploadCSVModFunc(func(_ context.Context, o *FileuploadCSVTemplate) {
		o.Committed = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
// The generated value is sometimes null
func (m fileuploadCSVMods) RandomCommitted(f *faker.Faker) FileuploadCSVMod {
	return FileuploadCSVModFunc(func(_ context.Context, o *FileuploadCSVTemplate) {
		o.Committed = func() null.Val[time.Time] {
			if f == nil {
				f = &defaultFaker
			}

			val := random_time_Time(f)
			return null.From(val)
		}
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
// The generated value is never null
func (m fileuploadCSVMods) RandomCommittedNotNull(f *faker.Faker) FileuploadCSVMod {
	return FileuploadCSVModFunc(func(_ context.Context, o *FileuploadCSVTemplate) {
		o.Committed = func() null.Val[time.Time] {
			if f == nil {
				f = &defaultFaker
			}

			val := random_time_Time(f)
			return null.From(val)
		}
	})
}

// Set the model columns to this value
func (m fileuploadCSVMods) FileID(val int32) FileuploadCSVMod {
	return FileuploadCSVModFunc(func(_ context.Context, o *FileuploadCSVTemplate) {
		o.FileID = func() int32 { return val }
	})
}

// Set the Column from the function
func (m fileuploadCSVMods) FileIDFunc(f func() int32) FileuploadCSVMod {
	return FileuploadCSVModFunc(func(_ context.Context, o *FileuploadCSVTemplate) {
		o.FileID = f
	})
}

// Clear any values for the column
func (m fileuploadCSVMods) UnsetFileID() FileuploadCSVMod {
	return FileuploadCSVModFunc(func(_ context.Context, o *FileuploadCSVTemplate) {
		o.FileID = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m fileuploadCSVMods) RandomFileID(f *faker.Faker) FileuploadCSVMod {
	return FileuploadCSVModFunc(func(_ context.Context, o *FileuploadCSVTemplate) {
		o.FileID = func() int32 {
			return random_int32(f)
		}
	})
}

// Set the model columns to this value
func (m fileuploadCSVMods) Rowcount(val int32) FileuploadCSVMod {
	return FileuploadCSVModFunc(func(_ context.Context, o *FileuploadCSVTemplate) {
		o.Rowcount = func() int32 { return val }
	})
}

// Set the Column from the function
func (m fileuploadCSVMods) RowcountFunc(f func() int32) FileuploadCSVMod {
	return FileuploadCSVModFunc(func(_ context.Context, o *FileuploadCSVTemplate) {
		o.Rowcount = f
	})
}

// Clear any values for the column
func (m fileuploadCSVMods) UnsetRowcount() FileuploadCSVMod {
	return FileuploadCSVModFunc(func(_ context.Context, o *FileuploadCSVTemplate) {
		o.Rowcount = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m fileuploadCSVMods) RandomRowcount(f *faker.Faker) FileuploadCSVMod {
	return FileuploadCSVModFunc(func(_ context.Context, o *FileuploadCSVTemplate) {
		o.Rowcount = func() int32 {
			return random_int32(f)
		}
	})
}

// Set the model columns to this value
func (m fileuploadCSVMods) Type(val enums.FileuploadCsvtype) FileuploadCSVMod {
	return FileuploadCSVModFunc(func(_ context.Context, o *FileuploadCSVTemplate) {
		o.Type = func() enums.FileuploadCsvtype { return val }
	})
}

// Set the Column from the function
func (m fileuploadCSVMods) TypeFunc(f func() enums.FileuploadCsvtype) FileuploadCSVMod {
	return FileuploadCSVModFunc(func(_ context.Context, o *FileuploadCSVTemplate) {
		o.Type = f
	})
}

// Clear any values for the column
func (m fileuploadCSVMods) UnsetType() FileuploadCSVMod {
	return FileuploadCSVModFunc(func(_ context.Context, o *FileuploadCSVTemplate) {
		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 fileuploadCSVMods) RandomType(f *faker.Faker) FileuploadCSVMod {
	return FileuploadCSVModFunc(func(_ context.Context, o *FileuploadCSVTemplate) {
		o.Type = func() enums.FileuploadCsvtype {
			return random_enums_FileuploadCsvtype(f)
		}
	})
}

func (m fileuploadCSVMods) WithParentsCascading() FileuploadCSVMod {
	return FileuploadCSVModFunc(func(ctx context.Context, o *FileuploadCSVTemplate) {
		if isDone, _ := fileuploadCSVWithParentsCascadingCtx.Value(ctx); isDone {
			return
		}
		ctx = fileuploadCSVWithParentsCascadingCtx.WithValue(ctx, true)
		{

			related := o.f.NewFileuploadFileWithContext(ctx, FileuploadFileMods.WithParentsCascading())
			m.WithFile(related).Apply(ctx, o)
		}
	})
}

func (m fileuploadCSVMods) WithFile(rel *FileuploadFileTemplate) FileuploadCSVMod {
	return FileuploadCSVModFunc(func(ctx context.Context, o *FileuploadCSVTemplate) {
		o.r.File = &fileuploadCSVRFileR{
			o: rel,
		}
	})
}

func (m fileuploadCSVMods) WithNewFile(mods ...FileuploadFileMod) FileuploadCSVMod {
	return FileuploadCSVModFunc(func(ctx context.Context, o *FileuploadCSVTemplate) {
		related := o.f.NewFileuploadFileWithContext(ctx, mods...)

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

func (m fileuploadCSVMods) WithExistingFile(em *models.FileuploadFile) FileuploadCSVMod {
	return FileuploadCSVModFunc(func(ctx context.Context, o *FileuploadCSVTemplate) {
		o.r.File = &fileuploadCSVRFileR{
			o: o.f.FromExistingFileuploadFile(em),
		}
	})
}

func (m fileuploadCSVMods) WithoutFile() FileuploadCSVMod {
	return FileuploadCSVModFunc(func(ctx context.Context, o *FileuploadCSVTemplate) {
		o.r.File = nil
	})
}

func (m fileuploadCSVMods) WithCSVFileErrorCSVS(number int, related *FileuploadErrorCSVTemplate) FileuploadCSVMod {
	return FileuploadCSVModFunc(func(ctx context.Context, o *FileuploadCSVTemplate) {
		o.r.CSVFileErrorCSVS = []*fileuploadCSVRCSVFileErrorCSVSR{{
			number: number,
			o:      related,
		}}
	})
}

func (m fileuploadCSVMods) WithNewCSVFileErrorCSVS(number int, mods ...FileuploadErrorCSVMod) FileuploadCSVMod {
	return FileuploadCSVModFunc(func(ctx context.Context, o *FileuploadCSVTemplate) {
		related := o.f.NewFileuploadErrorCSVWithContext(ctx, mods...)
		m.WithCSVFileErrorCSVS(number, related).Apply(ctx, o)
	})
}

func (m fileuploadCSVMods) AddCSVFileErrorCSVS(number int, related *FileuploadErrorCSVTemplate) FileuploadCSVMod {
	return FileuploadCSVModFunc(func(ctx context.Context, o *FileuploadCSVTemplate) {
		o.r.CSVFileErrorCSVS = append(o.r.CSVFileErrorCSVS, &fileuploadCSVRCSVFileErrorCSVSR{
			number: number,
			o:      related,
		})
	})
}

func (m fileuploadCSVMods) AddNewCSVFileErrorCSVS(number int, mods ...FileuploadErrorCSVMod) FileuploadCSVMod {
	return FileuploadCSVModFunc(func(ctx context.Context, o *FileuploadCSVTemplate) {
		related := o.f.NewFileuploadErrorCSVWithContext(ctx, mods...)
		m.AddCSVFileErrorCSVS(number, related).Apply(ctx, o)
	})
}

func (m fileuploadCSVMods) AddExistingCSVFileErrorCSVS(existingModels ...*models.FileuploadErrorCSV) FileuploadCSVMod {
	return FileuploadCSVModFunc(func(ctx context.Context, o *FileuploadCSVTemplate) {
		for _, em := range existingModels {
			o.r.CSVFileErrorCSVS = append(o.r.CSVFileErrorCSVS, &fileuploadCSVRCSVFileErrorCSVSR{
				o: o.f.FromExistingFileuploadErrorCSV(em),
			})
		}
	})
}

func (m fileuploadCSVMods) WithoutCSVFileErrorCSVS() FileuploadCSVMod {
	return FileuploadCSVModFunc(func(ctx context.Context, o *FileuploadCSVTemplate) {
		o.r.CSVFileErrorCSVS = nil
	})
}

func (m fileuploadCSVMods) WithCSVFilePools(number int, related *FileuploadPoolTemplate) FileuploadCSVMod {
	return FileuploadCSVModFunc(func(ctx context.Context, o *FileuploadCSVTemplate) {
		o.r.CSVFilePools = []*fileuploadCSVRCSVFilePoolsR{{
			number: number,
			o:      related,
		}}
	})
}

func (m fileuploadCSVMods) WithNewCSVFilePools(number int, mods ...FileuploadPoolMod) FileuploadCSVMod {
	return FileuploadCSVModFunc(func(ctx context.Context, o *FileuploadCSVTemplate) {
		related := o.f.NewFileuploadPoolWithContext(ctx, mods...)
		m.WithCSVFilePools(number, related).Apply(ctx, o)
	})
}

func (m fileuploadCSVMods) AddCSVFilePools(number int, related *FileuploadPoolTemplate) FileuploadCSVMod {
	return FileuploadCSVModFunc(func(ctx context.Context, o *FileuploadCSVTemplate) {
		o.r.CSVFilePools = append(o.r.CSVFilePools, &fileuploadCSVRCSVFilePoolsR{
			number: number,
			o:      related,
		})
	})
}

func (m fileuploadCSVMods) AddNewCSVFilePools(number int, mods ...FileuploadPoolMod) FileuploadCSVMod {
	return FileuploadCSVModFunc(func(ctx context.Context, o *FileuploadCSVTemplate) {
		related := o.f.NewFileuploadPoolWithContext(ctx, mods...)
		m.AddCSVFilePools(number, related).Apply(ctx, o)
	})
}

func (m fileuploadCSVMods) AddExistingCSVFilePools(existingModels ...*models.FileuploadPool) FileuploadCSVMod {
	return FileuploadCSVModFunc(func(ctx context.Context, o *FileuploadCSVTemplate) {
		for _, em := range existingModels {
			o.r.CSVFilePools = append(o.r.CSVFilePools, &fileuploadCSVRCSVFilePoolsR{
				o: o.f.FromExistingFileuploadPool(em),
			})
		}
	})
}

func (m fileuploadCSVMods) WithoutCSVFilePools() FileuploadCSVMod {
	return FileuploadCSVModFunc(func(ctx context.Context, o *FileuploadCSVTemplate) {
		o.r.CSVFilePools = nil
	})
}
