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

type NotificationModFunc func(context.Context, *NotificationTemplate)

func (f NotificationModFunc) Apply(ctx context.Context, n *NotificationTemplate) {
	f(ctx, n)
}

type NotificationModSlice []NotificationMod

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

// NotificationTemplate is an object representing the database table.
// all columns are optional and should be set by mods
type NotificationTemplate struct {
	ID         func() int32
	Created    func() time.Time
	Link       func() string
	Message    func() string
	Type       func() enums.Notificationtype
	UserID     func() int32
	ResolvedAt func() null.Val[time.Time]

	r notificationR
	f *Factory

	alreadyPersisted bool
}

type notificationR struct {
	UserUser *notificationRUserUserR
}

type notificationRUserUserR struct {
	o *UserTemplate
}

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

// setModelRels creates and sets the relationships on *models.Notification
// according to the relationships in the template. Nothing is inserted into the db
func (t NotificationTemplate) setModelRels(o *models.Notification) {
	if t.r.UserUser != nil {
		rel := t.r.UserUser.o.Build()
		rel.R.UserNotifications = append(rel.R.UserNotifications, o)
		o.UserID = rel.ID // h2
		o.R.UserUser = rel
	}
}

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

	if o.ID != nil {
		val := o.ID()
		m.ID = omit.From(val)
	}
	if o.Created != nil {
		val := o.Created()
		m.Created = omit.From(val)
	}
	if o.Link != nil {
		val := o.Link()
		m.Link = omit.From(val)
	}
	if o.Message != nil {
		val := o.Message()
		m.Message = omit.From(val)
	}
	if o.Type != nil {
		val := o.Type()
		m.Type = omit.From(val)
	}
	if o.UserID != nil {
		val := o.UserID()
		m.UserID = omit.From(val)
	}
	if o.ResolvedAt != nil {
		val := o.ResolvedAt()
		m.ResolvedAt = omitnull.FromNull(val)
	}

	return m
}

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

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

	return m
}

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

	if o.ID != nil {
		m.ID = o.ID()
	}
	if o.Created != nil {
		m.Created = o.Created()
	}
	if o.Link != nil {
		m.Link = o.Link()
	}
	if o.Message != nil {
		m.Message = o.Message()
	}
	if o.Type != nil {
		m.Type = o.Type()
	}
	if o.UserID != nil {
		m.UserID = o.UserID()
	}
	if o.ResolvedAt != nil {
		m.ResolvedAt = o.ResolvedAt()
	}

	o.setModelRels(m)

	return m
}

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

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

	return m
}

func ensureCreatableNotification(m *models.NotificationSetter) {
	if !(m.Created.IsValue()) {
		val := random_time_Time(nil)
		m.Created = omit.From(val)
	}
	if !(m.Link.IsValue()) {
		val := random_string(nil)
		m.Link = omit.From(val)
	}
	if !(m.Message.IsValue()) {
		val := random_string(nil)
		m.Message = omit.From(val)
	}
	if !(m.Type.IsValue()) {
		val := random_enums_Notificationtype(nil)
		m.Type = omit.From(val)
	}
	if !(m.UserID.IsValue()) {
		val := random_int32(nil)
		m.UserID = omit.From(val)
	}
}

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

	return err
}

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

	if o.r.UserUser == nil {
		NotificationMods.WithNewUserUser().Apply(ctx, o)
	}

	var rel0 *models.User

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

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

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

	m.R.UserUser = rel0

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

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

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

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

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

	return m, nil
}

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

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

// Notification has methods that act as mods for the NotificationTemplate
var NotificationMods notificationMods

type notificationMods struct{}

func (m notificationMods) RandomizeAllColumns(f *faker.Faker) NotificationMod {
	return NotificationModSlice{
		NotificationMods.RandomID(f),
		NotificationMods.RandomCreated(f),
		NotificationMods.RandomLink(f),
		NotificationMods.RandomMessage(f),
		NotificationMods.RandomType(f),
		NotificationMods.RandomUserID(f),
		NotificationMods.RandomResolvedAt(f),
	}
}

// Set the model columns to this value
func (m notificationMods) ID(val int32) NotificationMod {
	return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
		o.ID = func() int32 { return val }
	})
}

// Set the Column from the function
func (m notificationMods) IDFunc(f func() int32) NotificationMod {
	return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
		o.ID = f
	})
}

// Clear any values for the column
func (m notificationMods) UnsetID() NotificationMod {
	return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
		o.ID = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m notificationMods) RandomID(f *faker.Faker) NotificationMod {
	return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
		o.ID = func() int32 {
			return random_int32(f)
		}
	})
}

// Set the model columns to this value
func (m notificationMods) Created(val time.Time) NotificationMod {
	return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
		o.Created = func() time.Time { return val }
	})
}

// Set the Column from the function
func (m notificationMods) CreatedFunc(f func() time.Time) NotificationMod {
	return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
		o.Created = f
	})
}

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

// Set the model columns to this value
func (m notificationMods) Link(val string) NotificationMod {
	return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
		o.Link = func() string { return val }
	})
}

// Set the Column from the function
func (m notificationMods) LinkFunc(f func() string) NotificationMod {
	return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
		o.Link = f
	})
}

// Clear any values for the column
func (m notificationMods) UnsetLink() NotificationMod {
	return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
		o.Link = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m notificationMods) RandomLink(f *faker.Faker) NotificationMod {
	return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
		o.Link = func() string {
			return random_string(f)
		}
	})
}

// Set the model columns to this value
func (m notificationMods) Message(val string) NotificationMod {
	return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
		o.Message = func() string { return val }
	})
}

// Set the Column from the function
func (m notificationMods) MessageFunc(f func() string) NotificationMod {
	return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
		o.Message = f
	})
}

// Clear any values for the column
func (m notificationMods) UnsetMessage() NotificationMod {
	return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
		o.Message = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m notificationMods) RandomMessage(f *faker.Faker) NotificationMod {
	return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
		o.Message = func() string {
			return random_string(f)
		}
	})
}

// Set the model columns to this value
func (m notificationMods) Type(val enums.Notificationtype) NotificationMod {
	return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
		o.Type = func() enums.Notificationtype { return val }
	})
}

// Set the Column from the function
func (m notificationMods) TypeFunc(f func() enums.Notificationtype) NotificationMod {
	return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
		o.Type = f
	})
}

// Clear any values for the column
func (m notificationMods) UnsetType() NotificationMod {
	return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
		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 notificationMods) RandomType(f *faker.Faker) NotificationMod {
	return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
		o.Type = func() enums.Notificationtype {
			return random_enums_Notificationtype(f)
		}
	})
}

// Set the model columns to this value
func (m notificationMods) UserID(val int32) NotificationMod {
	return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
		o.UserID = func() int32 { return val }
	})
}

// Set the Column from the function
func (m notificationMods) UserIDFunc(f func() int32) NotificationMod {
	return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
		o.UserID = f
	})
}

// Clear any values for the column
func (m notificationMods) UnsetUserID() NotificationMod {
	return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
		o.UserID = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m notificationMods) RandomUserID(f *faker.Faker) NotificationMod {
	return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
		o.UserID = func() int32 {
			return random_int32(f)
		}
	})
}

// Set the model columns to this value
func (m notificationMods) ResolvedAt(val null.Val[time.Time]) NotificationMod {
	return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
		o.ResolvedAt = func() null.Val[time.Time] { return val }
	})
}

// Set the Column from the function
func (m notificationMods) ResolvedAtFunc(f func() null.Val[time.Time]) NotificationMod {
	return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
		o.ResolvedAt = f
	})
}

// Clear any values for the column
func (m notificationMods) UnsetResolvedAt() NotificationMod {
	return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
		o.ResolvedAt = 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 notificationMods) RandomResolvedAt(f *faker.Faker) NotificationMod {
	return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
		o.ResolvedAt = 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 notificationMods) RandomResolvedAtNotNull(f *faker.Faker) NotificationMod {
	return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
		o.ResolvedAt = func() null.Val[time.Time] {
			if f == nil {
				f = &defaultFaker
			}

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

func (m notificationMods) WithParentsCascading() NotificationMod {
	return NotificationModFunc(func(ctx context.Context, o *NotificationTemplate) {
		if isDone, _ := notificationWithParentsCascadingCtx.Value(ctx); isDone {
			return
		}
		ctx = notificationWithParentsCascadingCtx.WithValue(ctx, true)
		{

			related := o.f.NewUserWithContext(ctx, UserMods.WithParentsCascading())
			m.WithUserUser(related).Apply(ctx, o)
		}
	})
}

func (m notificationMods) WithUserUser(rel *UserTemplate) NotificationMod {
	return NotificationModFunc(func(ctx context.Context, o *NotificationTemplate) {
		o.r.UserUser = &notificationRUserUserR{
			o: rel,
		}
	})
}

func (m notificationMods) WithNewUserUser(mods ...UserMod) NotificationMod {
	return NotificationModFunc(func(ctx context.Context, o *NotificationTemplate) {
		related := o.f.NewUserWithContext(ctx, mods...)

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

func (m notificationMods) WithExistingUserUser(em *models.User) NotificationMod {
	return NotificationModFunc(func(ctx context.Context, o *NotificationTemplate) {
		o.r.UserUser = &notificationRUserUserR{
			o: o.f.FromExistingUser(em),
		}
	})
}

func (m notificationMods) WithoutUserUser() NotificationMod {
	return NotificationModFunc(func(ctx context.Context, o *NotificationTemplate) {
		o.r.UserUser = nil
	})
}
