// Code generated by BobGen sqlite v0.41.1. 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/aarondl/opt/null"
	"github.com/aarondl/opt/omit"
	"github.com/aarondl/opt/omitnull"
	"github.com/jaswdr/faker/v2"
	models "github.com/safing/portmaster/base/database/storage/sqlite/models"
	"github.com/stephenafamo/bob"
)

type RecordMod interface {
	Apply(context.Context, *RecordTemplate)
}

type RecordModFunc func(context.Context, *RecordTemplate)

func (f RecordModFunc) Apply(ctx context.Context, n *RecordTemplate) {
	f(ctx, n)
}

type RecordModSlice []RecordMod

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

// RecordTemplate is an object representing the database table.
// all columns are optional and should be set by mods
type RecordTemplate struct {
	Key        func() string
	Format     func() null.Val[int64]
	Value      func() null.Val[[]byte]
	Created    func() int64
	Modified   func() int64
	Expires    func() int64
	Deleted    func() int64
	Secret     func() bool
	Crownjewel func() bool

	f *Factory

	alreadyPersisted bool
}

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

// setModelRels creates and sets the relationships on *models.Record
// according to the relationships in the template. Nothing is inserted into the db
func (t RecordTemplate) setModelRels(o *models.Record) {}

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

	if o.Key != nil {
		val := o.Key()
		m.Key = omit.From(val)
	}
	if o.Format != nil {
		val := o.Format()
		m.Format = omitnull.FromNull(val)
	}
	if o.Value != nil {
		val := o.Value()
		m.Value = omitnull.FromNull(val)
	}
	if o.Created != nil {
		val := o.Created()
		m.Created = omit.From(val)
	}
	if o.Modified != nil {
		val := o.Modified()
		m.Modified = omit.From(val)
	}
	if o.Expires != nil {
		val := o.Expires()
		m.Expires = omit.From(val)
	}
	if o.Deleted != nil {
		val := o.Deleted()
		m.Deleted = omit.From(val)
	}
	if o.Secret != nil {
		val := o.Secret()
		m.Secret = omit.From(val)
	}
	if o.Crownjewel != nil {
		val := o.Crownjewel()
		m.Crownjewel = omit.From(val)
	}

	return m
}

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

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

	return m
}

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

	if o.Key != nil {
		m.Key = o.Key()
	}
	if o.Format != nil {
		m.Format = o.Format()
	}
	if o.Value != nil {
		m.Value = o.Value()
	}
	if o.Created != nil {
		m.Created = o.Created()
	}
	if o.Modified != nil {
		m.Modified = o.Modified()
	}
	if o.Expires != nil {
		m.Expires = o.Expires()
	}
	if o.Deleted != nil {
		m.Deleted = o.Deleted()
	}
	if o.Secret != nil {
		m.Secret = o.Secret()
	}
	if o.Crownjewel != nil {
		m.Crownjewel = o.Crownjewel()
	}

	o.setModelRels(m)

	return m
}

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

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

	return m
}

func ensureCreatableRecord(m *models.RecordSetter) {
	if !(m.Key.IsValue()) {
		val := random_string(nil)
		m.Key = omit.From(val)
	}
	if !(m.Created.IsValue()) {
		val := random_int64(nil)
		m.Created = omit.From(val)
	}
	if !(m.Modified.IsValue()) {
		val := random_int64(nil)
		m.Modified = omit.From(val)
	}
}

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

	return err
}

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

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

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

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

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

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

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

	return m, nil
}

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

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

// Record has methods that act as mods for the RecordTemplate
var RecordMods recordMods

type recordMods struct{}

func (m recordMods) RandomizeAllColumns(f *faker.Faker) RecordMod {
	return RecordModSlice{
		RecordMods.RandomKey(f),
		RecordMods.RandomFormat(f),
		RecordMods.RandomValue(f),
		RecordMods.RandomCreated(f),
		RecordMods.RandomModified(f),
		RecordMods.RandomExpires(f),
		RecordMods.RandomDeleted(f),
		RecordMods.RandomSecret(f),
		RecordMods.RandomCrownjewel(f),
	}
}

// Set the model columns to this value
func (m recordMods) Key(val string) RecordMod {
	return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
		o.Key = func() string { return val }
	})
}

// Set the Column from the function
func (m recordMods) KeyFunc(f func() string) RecordMod {
	return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
		o.Key = f
	})
}

// Clear any values for the column
func (m recordMods) UnsetKey() RecordMod {
	return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
		o.Key = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m recordMods) RandomKey(f *faker.Faker) RecordMod {
	return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
		o.Key = func() string {
			return random_string(f)
		}
	})
}

// Set the model columns to this value
func (m recordMods) Format(val null.Val[int64]) RecordMod {
	return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
		o.Format = func() null.Val[int64] { return val }
	})
}

// Set the Column from the function
func (m recordMods) FormatFunc(f func() null.Val[int64]) RecordMod {
	return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
		o.Format = f
	})
}

// Clear any values for the column
func (m recordMods) UnsetFormat() RecordMod {
	return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
		o.Format = 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 recordMods) RandomFormat(f *faker.Faker) RecordMod {
	return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
		o.Format = func() null.Val[int64] {
			if f == nil {
				f = &defaultFaker
			}

			val := random_int64(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 recordMods) RandomFormatNotNull(f *faker.Faker) RecordMod {
	return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
		o.Format = func() null.Val[int64] {
			if f == nil {
				f = &defaultFaker
			}

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

// Set the model columns to this value
func (m recordMods) Value(val null.Val[[]byte]) RecordMod {
	return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
		o.Value = func() null.Val[[]byte] { return val }
	})
}

// Set the Column from the function
func (m recordMods) ValueFunc(f func() null.Val[[]byte]) RecordMod {
	return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
		o.Value = f
	})
}

// Clear any values for the column
func (m recordMods) UnsetValue() RecordMod {
	return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
		o.Value = 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 recordMods) RandomValue(f *faker.Faker) RecordMod {
	return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
		o.Value = func() null.Val[[]byte] {
			if f == nil {
				f = &defaultFaker
			}

			val := random___byte(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 recordMods) RandomValueNotNull(f *faker.Faker) RecordMod {
	return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
		o.Value = func() null.Val[[]byte] {
			if f == nil {
				f = &defaultFaker
			}

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

// Set the model columns to this value
func (m recordMods) Created(val int64) RecordMod {
	return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
		o.Created = func() int64 { return val }
	})
}

// Set the Column from the function
func (m recordMods) CreatedFunc(f func() int64) RecordMod {
	return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
		o.Created = f
	})
}

// Clear any values for the column
func (m recordMods) UnsetCreated() RecordMod {
	return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
		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 recordMods) RandomCreated(f *faker.Faker) RecordMod {
	return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
		o.Created = func() int64 {
			return random_int64(f)
		}
	})
}

// Set the model columns to this value
func (m recordMods) Modified(val int64) RecordMod {
	return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
		o.Modified = func() int64 { return val }
	})
}

// Set the Column from the function
func (m recordMods) ModifiedFunc(f func() int64) RecordMod {
	return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
		o.Modified = f
	})
}

// Clear any values for the column
func (m recordMods) UnsetModified() RecordMod {
	return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
		o.Modified = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m recordMods) RandomModified(f *faker.Faker) RecordMod {
	return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
		o.Modified = func() int64 {
			return random_int64(f)
		}
	})
}

// Set the model columns to this value
func (m recordMods) Expires(val int64) RecordMod {
	return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
		o.Expires = func() int64 { return val }
	})
}

// Set the Column from the function
func (m recordMods) ExpiresFunc(f func() int64) RecordMod {
	return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
		o.Expires = f
	})
}

// Clear any values for the column
func (m recordMods) UnsetExpires() RecordMod {
	return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
		o.Expires = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m recordMods) RandomExpires(f *faker.Faker) RecordMod {
	return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
		o.Expires = func() int64 {
			return random_int64(f)
		}
	})
}

// Set the model columns to this value
func (m recordMods) Deleted(val int64) RecordMod {
	return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
		o.Deleted = func() int64 { return val }
	})
}

// Set the Column from the function
func (m recordMods) DeletedFunc(f func() int64) RecordMod {
	return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
		o.Deleted = f
	})
}

// Clear any values for the column
func (m recordMods) UnsetDeleted() RecordMod {
	return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
		o.Deleted = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m recordMods) RandomDeleted(f *faker.Faker) RecordMod {
	return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
		o.Deleted = func() int64 {
			return random_int64(f)
		}
	})
}

// Set the model columns to this value
func (m recordMods) Secret(val bool) RecordMod {
	return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
		o.Secret = func() bool { return val }
	})
}

// Set the Column from the function
func (m recordMods) SecretFunc(f func() bool) RecordMod {
	return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
		o.Secret = f
	})
}

// Clear any values for the column
func (m recordMods) UnsetSecret() RecordMod {
	return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
		o.Secret = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m recordMods) RandomSecret(f *faker.Faker) RecordMod {
	return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
		o.Secret = func() bool {
			return random_bool(f)
		}
	})
}

// Set the model columns to this value
func (m recordMods) Crownjewel(val bool) RecordMod {
	return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
		o.Crownjewel = func() bool { return val }
	})
}

// Set the Column from the function
func (m recordMods) CrownjewelFunc(f func() bool) RecordMod {
	return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
		o.Crownjewel = f
	})
}

// Clear any values for the column
func (m recordMods) UnsetCrownjewel() RecordMod {
	return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
		o.Crownjewel = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m recordMods) RandomCrownjewel(f *faker.Faker) RecordMod {
	return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
		o.Crownjewel = func() bool {
			return random_bool(f)
		}
	})
}

func (m recordMods) WithParentsCascading() RecordMod {
	return RecordModFunc(func(ctx context.Context, o *RecordTemplate) {
		if isDone, _ := recordWithParentsCascadingCtx.Value(ctx); isDone {
			return
		}
		ctx = recordWithParentsCascadingCtx.WithValue(ctx, true)
	})
}
