// 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"
	models "github.com/Gleipnir-Technology/nidus-sync/db/models"
	"github.com/aarondl/opt/omit"
	"github.com/jaswdr/faker/v2"
)

type SessionMod interface {
	Apply(context.Context, *SessionTemplate)
}

type SessionModFunc func(context.Context, *SessionTemplate)

func (f SessionModFunc) Apply(ctx context.Context, n *SessionTemplate) {
	f(ctx, n)
}

type SessionModSlice []SessionMod

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

// SessionTemplate is an object representing the database table.
// all columns are optional and should be set by mods
type SessionTemplate struct {
	Token  func() string
	Data   func() []byte
	Expiry func() time.Time

	f *Factory

	alreadyPersisted bool
}

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

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

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

	if o.Token != nil {
		val := o.Token()
		m.Token = omit.From(val)
	}
	if o.Data != nil {
		val := o.Data()
		m.Data = omit.From(val)
	}
	if o.Expiry != nil {
		val := o.Expiry()
		m.Expiry = omit.From(val)
	}

	return m
}

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

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

	return m
}

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

	if o.Token != nil {
		m.Token = o.Token()
	}
	if o.Data != nil {
		m.Data = o.Data()
	}
	if o.Expiry != nil {
		m.Expiry = o.Expiry()
	}

	o.setModelRels(m)

	return m
}

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

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

	return m
}

func ensureCreatableSession(m *models.SessionSetter) {
	if !(m.Token.IsValue()) {
		val := random_string(nil)
		m.Token = omit.From(val)
	}
	if !(m.Data.IsValue()) {
		val := random___byte(nil)
		m.Data = omit.From(val)
	}
	if !(m.Expiry.IsValue()) {
		val := random_time_Time(nil)
		m.Expiry = omit.From(val)
	}
}

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

	return err
}

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

	m, err := models.Sessions.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 session and inserts it into the database
// Relations objects are also inserted and placed in the .R field
// panics if an error occurs
func (o *SessionTemplate) MustCreate(ctx context.Context, exec bob.Executor) *models.Session {
	m, err := o.Create(ctx, exec)
	if err != nil {
		panic(err)
	}
	return m
}

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

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

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

	return m, nil
}

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

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

// Session has methods that act as mods for the SessionTemplate
var SessionMods sessionMods

type sessionMods struct{}

func (m sessionMods) RandomizeAllColumns(f *faker.Faker) SessionMod {
	return SessionModSlice{
		SessionMods.RandomToken(f),
		SessionMods.RandomData(f),
		SessionMods.RandomExpiry(f),
	}
}

// Set the model columns to this value
func (m sessionMods) Token(val string) SessionMod {
	return SessionModFunc(func(_ context.Context, o *SessionTemplate) {
		o.Token = func() string { return val }
	})
}

// Set the Column from the function
func (m sessionMods) TokenFunc(f func() string) SessionMod {
	return SessionModFunc(func(_ context.Context, o *SessionTemplate) {
		o.Token = f
	})
}

// Clear any values for the column
func (m sessionMods) UnsetToken() SessionMod {
	return SessionModFunc(func(_ context.Context, o *SessionTemplate) {
		o.Token = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m sessionMods) RandomToken(f *faker.Faker) SessionMod {
	return SessionModFunc(func(_ context.Context, o *SessionTemplate) {
		o.Token = func() string {
			return random_string(f)
		}
	})
}

// Set the model columns to this value
func (m sessionMods) Data(val []byte) SessionMod {
	return SessionModFunc(func(_ context.Context, o *SessionTemplate) {
		o.Data = func() []byte { return val }
	})
}

// Set the Column from the function
func (m sessionMods) DataFunc(f func() []byte) SessionMod {
	return SessionModFunc(func(_ context.Context, o *SessionTemplate) {
		o.Data = f
	})
}

// Clear any values for the column
func (m sessionMods) UnsetData() SessionMod {
	return SessionModFunc(func(_ context.Context, o *SessionTemplate) {
		o.Data = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m sessionMods) RandomData(f *faker.Faker) SessionMod {
	return SessionModFunc(func(_ context.Context, o *SessionTemplate) {
		o.Data = func() []byte {
			return random___byte(f)
		}
	})
}

// Set the model columns to this value
func (m sessionMods) Expiry(val time.Time) SessionMod {
	return SessionModFunc(func(_ context.Context, o *SessionTemplate) {
		o.Expiry = func() time.Time { return val }
	})
}

// Set the Column from the function
func (m sessionMods) ExpiryFunc(f func() time.Time) SessionMod {
	return SessionModFunc(func(_ context.Context, o *SessionTemplate) {
		o.Expiry = f
	})
}

// Clear any values for the column
func (m sessionMods) UnsetExpiry() SessionMod {
	return SessionModFunc(func(_ context.Context, o *SessionTemplate) {
		o.Expiry = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m sessionMods) RandomExpiry(f *faker.Faker) SessionMod {
	return SessionModFunc(func(_ context.Context, o *SessionTemplate) {
		o.Expiry = func() time.Time {
			return random_time_Time(f)
		}
	})
}

func (m sessionMods) WithParentsCascading() SessionMod {
	return SessionModFunc(func(ctx context.Context, o *SessionTemplate) {
		if isDone, _ := sessionWithParentsCascadingCtx.Value(ctx); isDone {
			return
		}
		ctx = sessionWithParentsCascadingCtx.WithValue(ctx, true)
	})
}
