// Code generated by BobGen mysql v0.42.0. DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.

package models

import (
	"context"
	"fmt"
	"io"

	"github.com/aarondl/opt/omit"
	"github.com/stephenafamo/bob"
	"github.com/stephenafamo/bob/dialect/mysql"
	"github.com/stephenafamo/bob/dialect/mysql/dialect"
	"github.com/stephenafamo/bob/dialect/mysql/dm"
	"github.com/stephenafamo/bob/dialect/mysql/sm"
	"github.com/stephenafamo/bob/dialect/mysql/um"
	"github.com/stephenafamo/bob/expr"
	"github.com/stephenafamo/bob/mods"
	"github.com/stephenafamo/bob/orm"
)

// AuthUserGroup is an object representing the database table.
type AuthUserGroup struct {
	ID      int32 `db:"id,pk,autoincr" `
	UserID  int32 `db:"user_id" `
	GroupID int32 `db:"group_id" `

	R authUserGroupR `db:"-" `
}

// AuthUserGroupSlice is an alias for a slice of pointers to AuthUserGroup.
// This should almost always be used instead of []*AuthUserGroup.
type AuthUserGroupSlice []*AuthUserGroup

// AuthUserGroups contains methods to work with the auth_user_groups table
var AuthUserGroups = mysql.NewTablex[*AuthUserGroup, AuthUserGroupSlice, *AuthUserGroupSetter]("auth_user_groups", buildAuthUserGroupColumns("auth_user_groups"), []string{"id"}, []string{"user_id", "group_id"})

// AuthUserGroupsQuery is a query on the auth_user_groups table
type AuthUserGroupsQuery = *mysql.ViewQuery[*AuthUserGroup, AuthUserGroupSlice]

// authUserGroupR is where relationships are stored.
type authUserGroupR struct {
	GroupAuthGroup *AuthGroup // auth_user_groups_group_id_97559544_fk_auth_group_id
	UserAuthUser   *AuthUser  // auth_user_groups_user_id_6a12ed8b_fk_auth_user_id
}

func buildAuthUserGroupColumns(alias string) authUserGroupColumns {
	return authUserGroupColumns{
		ColumnsExpr: expr.NewColumnsExpr(
			"id", "user_id", "group_id",
		).WithParent("auth_user_groups"),
		tableAlias: alias,
		ID:         mysql.Quote(alias, "id"),
		UserID:     mysql.Quote(alias, "user_id"),
		GroupID:    mysql.Quote(alias, "group_id"),
	}
}

type authUserGroupColumns struct {
	expr.ColumnsExpr
	tableAlias string
	ID         mysql.Expression
	UserID     mysql.Expression
	GroupID    mysql.Expression
}

func (c authUserGroupColumns) Alias() string {
	return c.tableAlias
}

func (authUserGroupColumns) AliasedAs(alias string) authUserGroupColumns {
	return buildAuthUserGroupColumns(alias)
}

// AuthUserGroupSetter is used for insert/upsert/update operations
// All values are optional, and do not have to be set
// Generated columns are not included
type AuthUserGroupSetter struct {
	ID      omit.Val[int32] `db:"id,pk,autoincr" `
	UserID  omit.Val[int32] `db:"user_id" `
	GroupID omit.Val[int32] `db:"group_id" `
}

func (s AuthUserGroupSetter) SetColumns() []string {
	vals := make([]string, 0, 3)
	if s.ID.IsValue() {
		vals = append(vals, "id")
	}
	if s.UserID.IsValue() {
		vals = append(vals, "user_id")
	}
	if s.GroupID.IsValue() {
		vals = append(vals, "group_id")
	}
	return vals
}

func (s AuthUserGroupSetter) Overwrite(t *AuthUserGroup) {
	if s.ID.IsValue() {
		t.ID = s.ID.MustGet()
	}
	if s.UserID.IsValue() {
		t.UserID = s.UserID.MustGet()
	}
	if s.GroupID.IsValue() {
		t.GroupID = s.GroupID.MustGet()
	}
}

func (s *AuthUserGroupSetter) Apply(q *dialect.InsertQuery) {
	q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
		return AuthUserGroups.BeforeInsertHooks.RunHooks(ctx, exec, s)
	})

	q.AppendValues(
		bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
			if !(s.ID.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.ID.MustGet()).WriteSQL(ctx, w, d, start)
		}), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
			if !(s.UserID.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.UserID.MustGet()).WriteSQL(ctx, w, d, start)
		}), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
			if !(s.GroupID.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.GroupID.MustGet()).WriteSQL(ctx, w, d, start)
		}))
}

func (s AuthUserGroupSetter) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
	return um.Set(s.Expressions("auth_user_groups")...)
}

func (s AuthUserGroupSetter) Expressions(prefix ...string) []bob.Expression {
	exprs := make([]bob.Expression, 0, 3)

	if s.ID.IsValue() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			mysql.Quote(append(prefix, "id")...),
			mysql.Arg(s.ID),
		}})
	}

	if s.UserID.IsValue() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			mysql.Quote(append(prefix, "user_id")...),
			mysql.Arg(s.UserID),
		}})
	}

	if s.GroupID.IsValue() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			mysql.Quote(append(prefix, "group_id")...),
			mysql.Arg(s.GroupID),
		}})
	}

	return exprs
}

// FindAuthUserGroup retrieves a single record by primary key
// If cols is empty Find will return all columns.
func FindAuthUserGroup(ctx context.Context, exec bob.Executor, IDPK int32, cols ...string) (*AuthUserGroup, error) {
	if len(cols) == 0 {
		return AuthUserGroups.Query(
			sm.Where(AuthUserGroups.Columns.ID.EQ(mysql.Arg(IDPK))),
		).One(ctx, exec)
	}

	return AuthUserGroups.Query(
		sm.Where(AuthUserGroups.Columns.ID.EQ(mysql.Arg(IDPK))),
		sm.Columns(AuthUserGroups.Columns.Only(cols...)),
	).One(ctx, exec)
}

// AuthUserGroupExists checks the presence of a single record by primary key
func AuthUserGroupExists(ctx context.Context, exec bob.Executor, IDPK int32) (bool, error) {
	return AuthUserGroups.Query(
		sm.Where(AuthUserGroups.Columns.ID.EQ(mysql.Arg(IDPK))),
	).Exists(ctx, exec)
}

// AfterQueryHook is called after AuthUserGroup is retrieved from the database
func (o *AuthUserGroup) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
	var err error

	switch queryType {
	case bob.QueryTypeSelect:
		ctx, err = AuthUserGroups.AfterSelectHooks.RunHooks(ctx, exec, AuthUserGroupSlice{o})
	case bob.QueryTypeInsert:
		ctx, err = AuthUserGroups.AfterInsertHooks.RunHooks(ctx, exec, AuthUserGroupSlice{o})
	case bob.QueryTypeUpdate:
		ctx, err = AuthUserGroups.AfterUpdateHooks.RunHooks(ctx, exec, AuthUserGroupSlice{o})
	case bob.QueryTypeDelete:
		ctx, err = AuthUserGroups.AfterDeleteHooks.RunHooks(ctx, exec, AuthUserGroupSlice{o})
	}

	return err
}

// primaryKeyVals returns the primary key values of the AuthUserGroup
func (o *AuthUserGroup) primaryKeyVals() bob.Expression {
	return mysql.Arg(o.ID)
}

func (o *AuthUserGroup) pkEQ() dialect.Expression {
	return mysql.Quote("auth_user_groups", "id").EQ(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
		return o.primaryKeyVals().WriteSQL(ctx, w, d, start)
	}))
}

// Update uses an executor to update the AuthUserGroup
func (o *AuthUserGroup) Update(ctx context.Context, exec bob.Executor, s *AuthUserGroupSetter) error {
	_, err := AuthUserGroups.Update(s.UpdateMod(), um.Where(o.pkEQ())).Exec(ctx, exec)
	if err != nil {
		return err
	}

	s.Overwrite(o)

	return nil
}

// Delete deletes a single AuthUserGroup record with an executor
func (o *AuthUserGroup) Delete(ctx context.Context, exec bob.Executor) error {
	_, err := AuthUserGroups.Delete(dm.Where(o.pkEQ())).Exec(ctx, exec)
	return err
}

// Reload refreshes the AuthUserGroup using the executor
func (o *AuthUserGroup) Reload(ctx context.Context, exec bob.Executor) error {
	o2, err := AuthUserGroups.Query(
		sm.Where(AuthUserGroups.Columns.ID.EQ(mysql.Arg(o.ID))),
	).One(ctx, exec)
	if err != nil {
		return err
	}
	o2.R = o.R
	*o = *o2

	return nil
}

// AfterQueryHook is called after AuthUserGroupSlice is retrieved from the database
func (o AuthUserGroupSlice) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
	var err error

	switch queryType {
	case bob.QueryTypeSelect:
		ctx, err = AuthUserGroups.AfterSelectHooks.RunHooks(ctx, exec, o)
	case bob.QueryTypeInsert:
		ctx, err = AuthUserGroups.AfterInsertHooks.RunHooks(ctx, exec, o)
	case bob.QueryTypeUpdate:
		ctx, err = AuthUserGroups.AfterUpdateHooks.RunHooks(ctx, exec, o)
	case bob.QueryTypeDelete:
		ctx, err = AuthUserGroups.AfterDeleteHooks.RunHooks(ctx, exec, o)
	}

	return err
}

func (o AuthUserGroupSlice) pkIN() dialect.Expression {
	if len(o) == 0 {
		return mysql.Raw("NULL")
	}

	return mysql.Quote("auth_user_groups", "id").In(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
		pkPairs := make([]bob.Expression, len(o))
		for i, row := range o {
			pkPairs[i] = row.primaryKeyVals()
		}
		return bob.ExpressSlice(ctx, w, d, start, pkPairs, "", ", ", "")
	}))
}

// copyMatchingRows finds models in the given slice that have the same primary key
// then it first copies the existing relationships from the old model to the new model
// and then replaces the old model in the slice with the new model
func (o AuthUserGroupSlice) copyMatchingRows(from ...*AuthUserGroup) {
	for i, old := range o {
		for _, new := range from {
			if new.ID != old.ID {
				continue
			}
			new.R = old.R
			o[i] = new
			break
		}
	}
}

// UpdateMod modifies an update query with "WHERE primary_key IN (o...)"
func (o AuthUserGroupSlice) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
	return bob.ModFunc[*dialect.UpdateQuery](func(q *dialect.UpdateQuery) {
		q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
			return AuthUserGroups.BeforeUpdateHooks.RunHooks(ctx, exec, o)
		})

		q.AppendLoader(bob.LoaderFunc(func(ctx context.Context, exec bob.Executor, retrieved any) error {
			var err error
			switch retrieved := retrieved.(type) {
			case *AuthUserGroup:
				o.copyMatchingRows(retrieved)
			case []*AuthUserGroup:
				o.copyMatchingRows(retrieved...)
			case AuthUserGroupSlice:
				o.copyMatchingRows(retrieved...)
			default:
				// If the retrieved value is not a AuthUserGroup or a slice of AuthUserGroup
				// then run the AfterUpdateHooks on the slice
				_, err = AuthUserGroups.AfterUpdateHooks.RunHooks(ctx, exec, o)
			}

			return err
		}))

		q.AppendWhere(o.pkIN())
	})
}

// DeleteMod modifies an delete query with "WHERE primary_key IN (o...)"
func (o AuthUserGroupSlice) DeleteMod() bob.Mod[*dialect.DeleteQuery] {
	return bob.ModFunc[*dialect.DeleteQuery](func(q *dialect.DeleteQuery) {
		q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
			return AuthUserGroups.BeforeDeleteHooks.RunHooks(ctx, exec, o)
		})

		q.AppendLoader(bob.LoaderFunc(func(ctx context.Context, exec bob.Executor, retrieved any) error {
			var err error
			switch retrieved := retrieved.(type) {
			case *AuthUserGroup:
				o.copyMatchingRows(retrieved)
			case []*AuthUserGroup:
				o.copyMatchingRows(retrieved...)
			case AuthUserGroupSlice:
				o.copyMatchingRows(retrieved...)
			default:
				// If the retrieved value is not a AuthUserGroup or a slice of AuthUserGroup
				// then run the AfterDeleteHooks on the slice
				_, err = AuthUserGroups.AfterDeleteHooks.RunHooks(ctx, exec, o)
			}

			return err
		}))

		q.AppendWhere(o.pkIN())
	})
}

func (o AuthUserGroupSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals AuthUserGroupSetter) error {
	_, err := AuthUserGroups.Update(vals.UpdateMod(), o.UpdateMod()).Exec(ctx, exec)

	for i := range o {
		vals.Overwrite(o[i])
	}

	return err
}

func (o AuthUserGroupSlice) DeleteAll(ctx context.Context, exec bob.Executor) error {
	if len(o) == 0 {
		return nil
	}

	_, err := AuthUserGroups.Delete(o.DeleteMod()).Exec(ctx, exec)
	return err
}

func (o AuthUserGroupSlice) ReloadAll(ctx context.Context, exec bob.Executor) error {
	if len(o) == 0 {
		return nil
	}

	o2, err := AuthUserGroups.Query(sm.Where(o.pkIN())).All(ctx, exec)
	if err != nil {
		return err
	}

	o.copyMatchingRows(o2...)

	return nil
}

// GroupAuthGroup starts a query for related objects on auth_group
func (o *AuthUserGroup) GroupAuthGroup(mods ...bob.Mod[*dialect.SelectQuery]) AuthGroupsQuery {
	return AuthGroups.Query(append(mods,
		sm.Where(AuthGroups.Columns.ID.EQ(mysql.Arg(o.GroupID))),
	)...)
}

func (os AuthUserGroupSlice) GroupAuthGroup(mods ...bob.Mod[*dialect.SelectQuery]) AuthGroupsQuery {
	PKArgSlice := make([]bob.Expression, len(os))
	for i, o := range os {
		PKArgSlice[i] = mysql.ArgGroup(o.GroupID)
	}
	PKArgExpr := mysql.Group(PKArgSlice...)

	return AuthGroups.Query(append(mods,
		sm.Where(mysql.Group(AuthGroups.Columns.ID).OP("IN", PKArgExpr)),
	)...)
}

// UserAuthUser starts a query for related objects on auth_user
func (o *AuthUserGroup) UserAuthUser(mods ...bob.Mod[*dialect.SelectQuery]) AuthUsersQuery {
	return AuthUsers.Query(append(mods,
		sm.Where(AuthUsers.Columns.ID.EQ(mysql.Arg(o.UserID))),
	)...)
}

func (os AuthUserGroupSlice) UserAuthUser(mods ...bob.Mod[*dialect.SelectQuery]) AuthUsersQuery {
	PKArgSlice := make([]bob.Expression, len(os))
	for i, o := range os {
		PKArgSlice[i] = mysql.ArgGroup(o.UserID)
	}
	PKArgExpr := mysql.Group(PKArgSlice...)

	return AuthUsers.Query(append(mods,
		sm.Where(mysql.Group(AuthUsers.Columns.ID).OP("IN", PKArgExpr)),
	)...)
}

func attachAuthUserGroupGroupAuthGroup0(ctx context.Context, exec bob.Executor, count int, authUserGroup0 *AuthUserGroup, authGroup1 *AuthGroup) (*AuthUserGroup, error) {
	setter := &AuthUserGroupSetter{
		GroupID: omit.From(authGroup1.ID),
	}

	err := authUserGroup0.Update(ctx, exec, setter)
	if err != nil {
		return nil, fmt.Errorf("attachAuthUserGroupGroupAuthGroup0: %w", err)
	}

	return authUserGroup0, nil
}

func (authUserGroup0 *AuthUserGroup) InsertGroupAuthGroup(ctx context.Context, exec bob.Executor, related *AuthGroupSetter) error {
	var err error

	authGroup1, err := AuthGroups.Insert(related).One(ctx, exec)
	if err != nil {
		return fmt.Errorf("inserting related objects: %w", err)
	}

	_, err = attachAuthUserGroupGroupAuthGroup0(ctx, exec, 1, authUserGroup0, authGroup1)
	if err != nil {
		return err
	}

	authUserGroup0.R.GroupAuthGroup = authGroup1

	authGroup1.R.GroupAuthUserGroups = append(authGroup1.R.GroupAuthUserGroups, authUserGroup0)

	return nil
}

func (authUserGroup0 *AuthUserGroup) AttachGroupAuthGroup(ctx context.Context, exec bob.Executor, authGroup1 *AuthGroup) error {
	var err error

	_, err = attachAuthUserGroupGroupAuthGroup0(ctx, exec, 1, authUserGroup0, authGroup1)
	if err != nil {
		return err
	}

	authUserGroup0.R.GroupAuthGroup = authGroup1

	authGroup1.R.GroupAuthUserGroups = append(authGroup1.R.GroupAuthUserGroups, authUserGroup0)

	return nil
}

func attachAuthUserGroupUserAuthUser0(ctx context.Context, exec bob.Executor, count int, authUserGroup0 *AuthUserGroup, authUser1 *AuthUser) (*AuthUserGroup, error) {
	setter := &AuthUserGroupSetter{
		UserID: omit.From(authUser1.ID),
	}

	err := authUserGroup0.Update(ctx, exec, setter)
	if err != nil {
		return nil, fmt.Errorf("attachAuthUserGroupUserAuthUser0: %w", err)
	}

	return authUserGroup0, nil
}

func (authUserGroup0 *AuthUserGroup) InsertUserAuthUser(ctx context.Context, exec bob.Executor, related *AuthUserSetter) error {
	var err error

	authUser1, err := AuthUsers.Insert(related).One(ctx, exec)
	if err != nil {
		return fmt.Errorf("inserting related objects: %w", err)
	}

	_, err = attachAuthUserGroupUserAuthUser0(ctx, exec, 1, authUserGroup0, authUser1)
	if err != nil {
		return err
	}

	authUserGroup0.R.UserAuthUser = authUser1

	authUser1.R.UserAuthUserGroups = append(authUser1.R.UserAuthUserGroups, authUserGroup0)

	return nil
}

func (authUserGroup0 *AuthUserGroup) AttachUserAuthUser(ctx context.Context, exec bob.Executor, authUser1 *AuthUser) error {
	var err error

	_, err = attachAuthUserGroupUserAuthUser0(ctx, exec, 1, authUserGroup0, authUser1)
	if err != nil {
		return err
	}

	authUserGroup0.R.UserAuthUser = authUser1

	authUser1.R.UserAuthUserGroups = append(authUser1.R.UserAuthUserGroups, authUserGroup0)

	return nil
}

type authUserGroupWhere[Q mysql.Filterable] struct {
	ID      mysql.WhereMod[Q, int32]
	UserID  mysql.WhereMod[Q, int32]
	GroupID mysql.WhereMod[Q, int32]
}

func (authUserGroupWhere[Q]) AliasedAs(alias string) authUserGroupWhere[Q] {
	return buildAuthUserGroupWhere[Q](buildAuthUserGroupColumns(alias))
}

func buildAuthUserGroupWhere[Q mysql.Filterable](cols authUserGroupColumns) authUserGroupWhere[Q] {
	return authUserGroupWhere[Q]{
		ID:      mysql.Where[Q, int32](cols.ID),
		UserID:  mysql.Where[Q, int32](cols.UserID),
		GroupID: mysql.Where[Q, int32](cols.GroupID),
	}
}

func (o *AuthUserGroup) Preload(name string, retrieved any) error {
	if o == nil {
		return nil
	}

	switch name {
	case "GroupAuthGroup":
		rel, ok := retrieved.(*AuthGroup)
		if !ok {
			return fmt.Errorf("authUserGroup cannot load %T as %q", retrieved, name)
		}

		o.R.GroupAuthGroup = rel

		if rel != nil {
			rel.R.GroupAuthUserGroups = AuthUserGroupSlice{o}
		}
		return nil
	case "UserAuthUser":
		rel, ok := retrieved.(*AuthUser)
		if !ok {
			return fmt.Errorf("authUserGroup cannot load %T as %q", retrieved, name)
		}

		o.R.UserAuthUser = rel

		if rel != nil {
			rel.R.UserAuthUserGroups = AuthUserGroupSlice{o}
		}
		return nil
	default:
		return fmt.Errorf("authUserGroup has no relationship %q", name)
	}
}

type authUserGroupPreloader struct {
	GroupAuthGroup func(...mysql.PreloadOption) mysql.Preloader
	UserAuthUser   func(...mysql.PreloadOption) mysql.Preloader
}

func buildAuthUserGroupPreloader() authUserGroupPreloader {
	return authUserGroupPreloader{
		GroupAuthGroup: func(opts ...mysql.PreloadOption) mysql.Preloader {
			return mysql.Preload[*AuthGroup, AuthGroupSlice](mysql.PreloadRel{
				Name: "GroupAuthGroup",
				Sides: []mysql.PreloadSide{
					{
						From:        AuthUserGroups,
						To:          AuthGroups,
						FromColumns: []string{"group_id"},
						ToColumns:   []string{"id"},
					},
				},
			}, AuthGroups.Columns.Names(), opts...)
		},
		UserAuthUser: func(opts ...mysql.PreloadOption) mysql.Preloader {
			return mysql.Preload[*AuthUser, AuthUserSlice](mysql.PreloadRel{
				Name: "UserAuthUser",
				Sides: []mysql.PreloadSide{
					{
						From:        AuthUserGroups,
						To:          AuthUsers,
						FromColumns: []string{"user_id"},
						ToColumns:   []string{"id"},
					},
				},
			}, AuthUsers.Columns.Names(), opts...)
		},
	}
}

type authUserGroupThenLoader[Q orm.Loadable] struct {
	GroupAuthGroup func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	UserAuthUser   func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
}

func buildAuthUserGroupThenLoader[Q orm.Loadable]() authUserGroupThenLoader[Q] {
	type GroupAuthGroupLoadInterface interface {
		LoadGroupAuthGroup(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type UserAuthUserLoadInterface interface {
		LoadUserAuthUser(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}

	return authUserGroupThenLoader[Q]{
		GroupAuthGroup: thenLoadBuilder[Q](
			"GroupAuthGroup",
			func(ctx context.Context, exec bob.Executor, retrieved GroupAuthGroupLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadGroupAuthGroup(ctx, exec, mods...)
			},
		),
		UserAuthUser: thenLoadBuilder[Q](
			"UserAuthUser",
			func(ctx context.Context, exec bob.Executor, retrieved UserAuthUserLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadUserAuthUser(ctx, exec, mods...)
			},
		),
	}
}

// LoadGroupAuthGroup loads the authUserGroup's GroupAuthGroup into the .R struct
func (o *AuthUserGroup) LoadGroupAuthGroup(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	// Reset the relationship
	o.R.GroupAuthGroup = nil

	related, err := o.GroupAuthGroup(mods...).One(ctx, exec)
	if err != nil {
		return err
	}

	related.R.GroupAuthUserGroups = AuthUserGroupSlice{o}

	o.R.GroupAuthGroup = related
	return nil
}

// LoadGroupAuthGroup loads the authUserGroup's GroupAuthGroup into the .R struct
func (os AuthUserGroupSlice) LoadGroupAuthGroup(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	authGroups, err := os.GroupAuthGroup(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		for _, rel := range authGroups {

			if !(o.GroupID == rel.ID) {
				continue
			}

			rel.R.GroupAuthUserGroups = append(rel.R.GroupAuthUserGroups, o)

			o.R.GroupAuthGroup = rel
			break
		}
	}

	return nil
}

// LoadUserAuthUser loads the authUserGroup's UserAuthUser into the .R struct
func (o *AuthUserGroup) LoadUserAuthUser(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	// Reset the relationship
	o.R.UserAuthUser = nil

	related, err := o.UserAuthUser(mods...).One(ctx, exec)
	if err != nil {
		return err
	}

	related.R.UserAuthUserGroups = AuthUserGroupSlice{o}

	o.R.UserAuthUser = related
	return nil
}

// LoadUserAuthUser loads the authUserGroup's UserAuthUser into the .R struct
func (os AuthUserGroupSlice) LoadUserAuthUser(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	authUsers, err := os.UserAuthUser(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		for _, rel := range authUsers {

			if !(o.UserID == rel.ID) {
				continue
			}

			rel.R.UserAuthUserGroups = append(rel.R.UserAuthUserGroups, o)

			o.R.UserAuthUser = rel
			break
		}
	}

	return nil
}

type authUserGroupJoins[Q dialect.Joinable] struct {
	typ            string
	GroupAuthGroup modAs[Q, authGroupColumns]
	UserAuthUser   modAs[Q, authUserColumns]
}

func (j authUserGroupJoins[Q]) aliasedAs(alias string) authUserGroupJoins[Q] {
	return buildAuthUserGroupJoins[Q](buildAuthUserGroupColumns(alias), j.typ)
}

func buildAuthUserGroupJoins[Q dialect.Joinable](cols authUserGroupColumns, typ string) authUserGroupJoins[Q] {
	return authUserGroupJoins[Q]{
		typ: typ,
		GroupAuthGroup: modAs[Q, authGroupColumns]{
			c: AuthGroups.Columns,
			f: func(to authGroupColumns) bob.Mod[Q] {
				mods := make(mods.QueryMods[Q], 0, 1)

				{
					mods = append(mods, dialect.Join[Q](typ, AuthGroups.Name().As(to.Alias())).On(
						to.ID.EQ(cols.GroupID),
					))
				}

				return mods
			},
		},
		UserAuthUser: modAs[Q, authUserColumns]{
			c: AuthUsers.Columns,
			f: func(to authUserColumns) bob.Mod[Q] {
				mods := make(mods.QueryMods[Q], 0, 1)

				{
					mods = append(mods, dialect.Join[Q](typ, AuthUsers.Name().As(to.Alias())).On(
						to.ID.EQ(cols.UserID),
					))
				}

				return mods
			},
		},
	}
}
