// 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/aarondl/opt/omitnull"
	"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"
)

// DjangoContentType is an object representing the database table.
type DjangoContentType struct {
	ID       int32  `db:"id,pk,autoincr" `
	AppLabel string `db:"app_label" `
	Model    string `db:"model" `

	R djangoContentTypeR `db:"-" `
}

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

// DjangoContentTypes contains methods to work with the django_content_type table
var DjangoContentTypes = mysql.NewTablex[*DjangoContentType, DjangoContentTypeSlice, *DjangoContentTypeSetter]("django_content_type", buildDjangoContentTypeColumns("django_content_type"), []string{"id"}, []string{"app_label", "model"})

// DjangoContentTypesQuery is a query on the django_content_type table
type DjangoContentTypesQuery = *mysql.ViewQuery[*DjangoContentType, DjangoContentTypeSlice]

// djangoContentTypeR is where relationships are stored.
type djangoContentTypeR struct {
	ContentTypeAuthPermissions AuthPermissionSlice // auth_permission_content_type_id_2f476e4b_fk_django_co
	ContentTypeDjangoAdminLogs DjangoAdminLogSlice // django_admin_log_content_type_id_c4bce8eb_fk_django_co
}

func buildDjangoContentTypeColumns(alias string) djangoContentTypeColumns {
	return djangoContentTypeColumns{
		ColumnsExpr: expr.NewColumnsExpr(
			"id", "app_label", "model",
		).WithParent("django_content_type"),
		tableAlias: alias,
		ID:         mysql.Quote(alias, "id"),
		AppLabel:   mysql.Quote(alias, "app_label"),
		Model:      mysql.Quote(alias, "model"),
	}
}

type djangoContentTypeColumns struct {
	expr.ColumnsExpr
	tableAlias string
	ID         mysql.Expression
	AppLabel   mysql.Expression
	Model      mysql.Expression
}

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

func (djangoContentTypeColumns) AliasedAs(alias string) djangoContentTypeColumns {
	return buildDjangoContentTypeColumns(alias)
}

// DjangoContentTypeSetter is used for insert/upsert/update operations
// All values are optional, and do not have to be set
// Generated columns are not included
type DjangoContentTypeSetter struct {
	ID       omit.Val[int32]  `db:"id,pk,autoincr" `
	AppLabel omit.Val[string] `db:"app_label" `
	Model    omit.Val[string] `db:"model" `
}

func (s DjangoContentTypeSetter) SetColumns() []string {
	vals := make([]string, 0, 3)
	if s.ID.IsValue() {
		vals = append(vals, "id")
	}
	if s.AppLabel.IsValue() {
		vals = append(vals, "app_label")
	}
	if s.Model.IsValue() {
		vals = append(vals, "model")
	}
	return vals
}

func (s DjangoContentTypeSetter) Overwrite(t *DjangoContentType) {
	if s.ID.IsValue() {
		t.ID = s.ID.MustGet()
	}
	if s.AppLabel.IsValue() {
		t.AppLabel = s.AppLabel.MustGet()
	}
	if s.Model.IsValue() {
		t.Model = s.Model.MustGet()
	}
}

func (s *DjangoContentTypeSetter) Apply(q *dialect.InsertQuery) {
	q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
		return DjangoContentTypes.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.AppLabel.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.AppLabel.MustGet()).WriteSQL(ctx, w, d, start)
		}), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
			if !(s.Model.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.Model.MustGet()).WriteSQL(ctx, w, d, start)
		}))
}

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

func (s DjangoContentTypeSetter) 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.AppLabel.IsValue() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			mysql.Quote(append(prefix, "app_label")...),
			mysql.Arg(s.AppLabel),
		}})
	}

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

	return exprs
}

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

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

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

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

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

	return err
}

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

func (o *DjangoContentType) pkEQ() dialect.Expression {
	return mysql.Quote("django_content_type", "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 DjangoContentType
func (o *DjangoContentType) Update(ctx context.Context, exec bob.Executor, s *DjangoContentTypeSetter) error {
	_, err := DjangoContentTypes.Update(s.UpdateMod(), um.Where(o.pkEQ())).Exec(ctx, exec)
	if err != nil {
		return err
	}

	s.Overwrite(o)

	return nil
}

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

// Reload refreshes the DjangoContentType using the executor
func (o *DjangoContentType) Reload(ctx context.Context, exec bob.Executor) error {
	o2, err := DjangoContentTypes.Query(
		sm.Where(DjangoContentTypes.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 DjangoContentTypeSlice is retrieved from the database
func (o DjangoContentTypeSlice) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
	var err error

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

	return err
}

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

	return mysql.Quote("django_content_type", "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 DjangoContentTypeSlice) copyMatchingRows(from ...*DjangoContentType) {
	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 DjangoContentTypeSlice) 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 DjangoContentTypes.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 *DjangoContentType:
				o.copyMatchingRows(retrieved)
			case []*DjangoContentType:
				o.copyMatchingRows(retrieved...)
			case DjangoContentTypeSlice:
				o.copyMatchingRows(retrieved...)
			default:
				// If the retrieved value is not a DjangoContentType or a slice of DjangoContentType
				// then run the AfterUpdateHooks on the slice
				_, err = DjangoContentTypes.AfterUpdateHooks.RunHooks(ctx, exec, o)
			}

			return err
		}))

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

// DeleteMod modifies an delete query with "WHERE primary_key IN (o...)"
func (o DjangoContentTypeSlice) 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 DjangoContentTypes.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 *DjangoContentType:
				o.copyMatchingRows(retrieved)
			case []*DjangoContentType:
				o.copyMatchingRows(retrieved...)
			case DjangoContentTypeSlice:
				o.copyMatchingRows(retrieved...)
			default:
				// If the retrieved value is not a DjangoContentType or a slice of DjangoContentType
				// then run the AfterDeleteHooks on the slice
				_, err = DjangoContentTypes.AfterDeleteHooks.RunHooks(ctx, exec, o)
			}

			return err
		}))

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

func (o DjangoContentTypeSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals DjangoContentTypeSetter) error {
	_, err := DjangoContentTypes.Update(vals.UpdateMod(), o.UpdateMod()).Exec(ctx, exec)

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

	return err
}

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

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

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

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

	o.copyMatchingRows(o2...)

	return nil
}

// ContentTypeAuthPermissions starts a query for related objects on auth_permission
func (o *DjangoContentType) ContentTypeAuthPermissions(mods ...bob.Mod[*dialect.SelectQuery]) AuthPermissionsQuery {
	return AuthPermissions.Query(append(mods,
		sm.Where(AuthPermissions.Columns.ContentTypeID.EQ(mysql.Arg(o.ID))),
	)...)
}

func (os DjangoContentTypeSlice) ContentTypeAuthPermissions(mods ...bob.Mod[*dialect.SelectQuery]) AuthPermissionsQuery {
	PKArgSlice := make([]bob.Expression, len(os))
	for i, o := range os {
		PKArgSlice[i] = mysql.ArgGroup(o.ID)
	}
	PKArgExpr := mysql.Group(PKArgSlice...)

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

// ContentTypeDjangoAdminLogs starts a query for related objects on django_admin_log
func (o *DjangoContentType) ContentTypeDjangoAdminLogs(mods ...bob.Mod[*dialect.SelectQuery]) DjangoAdminLogsQuery {
	return DjangoAdminLogs.Query(append(mods,
		sm.Where(DjangoAdminLogs.Columns.ContentTypeID.EQ(mysql.Arg(o.ID))),
	)...)
}

func (os DjangoContentTypeSlice) ContentTypeDjangoAdminLogs(mods ...bob.Mod[*dialect.SelectQuery]) DjangoAdminLogsQuery {
	PKArgSlice := make([]bob.Expression, len(os))
	for i, o := range os {
		PKArgSlice[i] = mysql.ArgGroup(o.ID)
	}
	PKArgExpr := mysql.Group(PKArgSlice...)

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

func insertDjangoContentTypeContentTypeAuthPermissions0(ctx context.Context, exec bob.Executor, authPermissions1 []*AuthPermissionSetter, djangoContentType0 *DjangoContentType) (AuthPermissionSlice, error) {
	for i := range authPermissions1 {
		authPermissions1[i].ContentTypeID = omit.From(djangoContentType0.ID)
	}

	ret, err := AuthPermissions.Insert(bob.ToMods(authPermissions1...)).All(ctx, exec)
	if err != nil {
		return ret, fmt.Errorf("insertDjangoContentTypeContentTypeAuthPermissions0: %w", err)
	}

	return ret, nil
}

func attachDjangoContentTypeContentTypeAuthPermissions0(ctx context.Context, exec bob.Executor, count int, authPermissions1 AuthPermissionSlice, djangoContentType0 *DjangoContentType) (AuthPermissionSlice, error) {
	setter := &AuthPermissionSetter{
		ContentTypeID: omit.From(djangoContentType0.ID),
	}

	err := authPermissions1.UpdateAll(ctx, exec, *setter)
	if err != nil {
		return nil, fmt.Errorf("attachDjangoContentTypeContentTypeAuthPermissions0: %w", err)
	}

	return authPermissions1, nil
}

func (djangoContentType0 *DjangoContentType) InsertContentTypeAuthPermissions(ctx context.Context, exec bob.Executor, related ...*AuthPermissionSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	authPermissions1, err := insertDjangoContentTypeContentTypeAuthPermissions0(ctx, exec, related, djangoContentType0)
	if err != nil {
		return err
	}

	djangoContentType0.R.ContentTypeAuthPermissions = append(djangoContentType0.R.ContentTypeAuthPermissions, authPermissions1...)

	for _, rel := range authPermissions1 {
		rel.R.ContentTypeDjangoContentType = djangoContentType0
	}
	return nil
}

func (djangoContentType0 *DjangoContentType) AttachContentTypeAuthPermissions(ctx context.Context, exec bob.Executor, related ...*AuthPermission) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	authPermissions1 := AuthPermissionSlice(related)

	_, err = attachDjangoContentTypeContentTypeAuthPermissions0(ctx, exec, len(related), authPermissions1, djangoContentType0)
	if err != nil {
		return err
	}

	djangoContentType0.R.ContentTypeAuthPermissions = append(djangoContentType0.R.ContentTypeAuthPermissions, authPermissions1...)

	for _, rel := range related {
		rel.R.ContentTypeDjangoContentType = djangoContentType0
	}

	return nil
}

func insertDjangoContentTypeContentTypeDjangoAdminLogs0(ctx context.Context, exec bob.Executor, djangoAdminLogs1 []*DjangoAdminLogSetter, djangoContentType0 *DjangoContentType) (DjangoAdminLogSlice, error) {
	for i := range djangoAdminLogs1 {
		djangoAdminLogs1[i].ContentTypeID = omitnull.From(djangoContentType0.ID)
	}

	ret, err := DjangoAdminLogs.Insert(bob.ToMods(djangoAdminLogs1...)).All(ctx, exec)
	if err != nil {
		return ret, fmt.Errorf("insertDjangoContentTypeContentTypeDjangoAdminLogs0: %w", err)
	}

	return ret, nil
}

func attachDjangoContentTypeContentTypeDjangoAdminLogs0(ctx context.Context, exec bob.Executor, count int, djangoAdminLogs1 DjangoAdminLogSlice, djangoContentType0 *DjangoContentType) (DjangoAdminLogSlice, error) {
	setter := &DjangoAdminLogSetter{
		ContentTypeID: omitnull.From(djangoContentType0.ID),
	}

	err := djangoAdminLogs1.UpdateAll(ctx, exec, *setter)
	if err != nil {
		return nil, fmt.Errorf("attachDjangoContentTypeContentTypeDjangoAdminLogs0: %w", err)
	}

	return djangoAdminLogs1, nil
}

func (djangoContentType0 *DjangoContentType) InsertContentTypeDjangoAdminLogs(ctx context.Context, exec bob.Executor, related ...*DjangoAdminLogSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	djangoAdminLogs1, err := insertDjangoContentTypeContentTypeDjangoAdminLogs0(ctx, exec, related, djangoContentType0)
	if err != nil {
		return err
	}

	djangoContentType0.R.ContentTypeDjangoAdminLogs = append(djangoContentType0.R.ContentTypeDjangoAdminLogs, djangoAdminLogs1...)

	for _, rel := range djangoAdminLogs1 {
		rel.R.ContentTypeDjangoContentType = djangoContentType0
	}
	return nil
}

func (djangoContentType0 *DjangoContentType) AttachContentTypeDjangoAdminLogs(ctx context.Context, exec bob.Executor, related ...*DjangoAdminLog) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	djangoAdminLogs1 := DjangoAdminLogSlice(related)

	_, err = attachDjangoContentTypeContentTypeDjangoAdminLogs0(ctx, exec, len(related), djangoAdminLogs1, djangoContentType0)
	if err != nil {
		return err
	}

	djangoContentType0.R.ContentTypeDjangoAdminLogs = append(djangoContentType0.R.ContentTypeDjangoAdminLogs, djangoAdminLogs1...)

	for _, rel := range related {
		rel.R.ContentTypeDjangoContentType = djangoContentType0
	}

	return nil
}

type djangoContentTypeWhere[Q mysql.Filterable] struct {
	ID       mysql.WhereMod[Q, int32]
	AppLabel mysql.WhereMod[Q, string]
	Model    mysql.WhereMod[Q, string]
}

func (djangoContentTypeWhere[Q]) AliasedAs(alias string) djangoContentTypeWhere[Q] {
	return buildDjangoContentTypeWhere[Q](buildDjangoContentTypeColumns(alias))
}

func buildDjangoContentTypeWhere[Q mysql.Filterable](cols djangoContentTypeColumns) djangoContentTypeWhere[Q] {
	return djangoContentTypeWhere[Q]{
		ID:       mysql.Where[Q, int32](cols.ID),
		AppLabel: mysql.Where[Q, string](cols.AppLabel),
		Model:    mysql.Where[Q, string](cols.Model),
	}
}

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

	switch name {
	case "ContentTypeAuthPermissions":
		rels, ok := retrieved.(AuthPermissionSlice)
		if !ok {
			return fmt.Errorf("djangoContentType cannot load %T as %q", retrieved, name)
		}

		o.R.ContentTypeAuthPermissions = rels

		for _, rel := range rels {
			if rel != nil {
				rel.R.ContentTypeDjangoContentType = o
			}
		}
		return nil
	case "ContentTypeDjangoAdminLogs":
		rels, ok := retrieved.(DjangoAdminLogSlice)
		if !ok {
			return fmt.Errorf("djangoContentType cannot load %T as %q", retrieved, name)
		}

		o.R.ContentTypeDjangoAdminLogs = rels

		for _, rel := range rels {
			if rel != nil {
				rel.R.ContentTypeDjangoContentType = o
			}
		}
		return nil
	default:
		return fmt.Errorf("djangoContentType has no relationship %q", name)
	}
}

type djangoContentTypePreloader struct{}

func buildDjangoContentTypePreloader() djangoContentTypePreloader {
	return djangoContentTypePreloader{}
}

type djangoContentTypeThenLoader[Q orm.Loadable] struct {
	ContentTypeAuthPermissions func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	ContentTypeDjangoAdminLogs func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
}

func buildDjangoContentTypeThenLoader[Q orm.Loadable]() djangoContentTypeThenLoader[Q] {
	type ContentTypeAuthPermissionsLoadInterface interface {
		LoadContentTypeAuthPermissions(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type ContentTypeDjangoAdminLogsLoadInterface interface {
		LoadContentTypeDjangoAdminLogs(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}

	return djangoContentTypeThenLoader[Q]{
		ContentTypeAuthPermissions: thenLoadBuilder[Q](
			"ContentTypeAuthPermissions",
			func(ctx context.Context, exec bob.Executor, retrieved ContentTypeAuthPermissionsLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadContentTypeAuthPermissions(ctx, exec, mods...)
			},
		),
		ContentTypeDjangoAdminLogs: thenLoadBuilder[Q](
			"ContentTypeDjangoAdminLogs",
			func(ctx context.Context, exec bob.Executor, retrieved ContentTypeDjangoAdminLogsLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadContentTypeDjangoAdminLogs(ctx, exec, mods...)
			},
		),
	}
}

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

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

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

	for _, rel := range related {
		rel.R.ContentTypeDjangoContentType = o
	}

	o.R.ContentTypeAuthPermissions = related
	return nil
}

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

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

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

		o.R.ContentTypeAuthPermissions = nil
	}

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

		for _, rel := range authPermissions {

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

			rel.R.ContentTypeDjangoContentType = o

			o.R.ContentTypeAuthPermissions = append(o.R.ContentTypeAuthPermissions, rel)
		}
	}

	return nil
}

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

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

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

	for _, rel := range related {
		rel.R.ContentTypeDjangoContentType = o
	}

	o.R.ContentTypeDjangoAdminLogs = related
	return nil
}

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

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

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

		o.R.ContentTypeDjangoAdminLogs = nil
	}

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

		for _, rel := range djangoAdminLogs {

			if !rel.ContentTypeID.IsValue() {
				continue
			}
			if !(rel.ContentTypeID.IsValue() && o.ID == rel.ContentTypeID.MustGet()) {
				continue
			}

			rel.R.ContentTypeDjangoContentType = o

			o.R.ContentTypeDjangoAdminLogs = append(o.R.ContentTypeDjangoAdminLogs, rel)
		}
	}

	return nil
}

type djangoContentTypeJoins[Q dialect.Joinable] struct {
	typ                        string
	ContentTypeAuthPermissions modAs[Q, authPermissionColumns]
	ContentTypeDjangoAdminLogs modAs[Q, djangoAdminLogColumns]
}

func (j djangoContentTypeJoins[Q]) aliasedAs(alias string) djangoContentTypeJoins[Q] {
	return buildDjangoContentTypeJoins[Q](buildDjangoContentTypeColumns(alias), j.typ)
}

func buildDjangoContentTypeJoins[Q dialect.Joinable](cols djangoContentTypeColumns, typ string) djangoContentTypeJoins[Q] {
	return djangoContentTypeJoins[Q]{
		typ: typ,
		ContentTypeAuthPermissions: modAs[Q, authPermissionColumns]{
			c: AuthPermissions.Columns,
			f: func(to authPermissionColumns) bob.Mod[Q] {
				mods := make(mods.QueryMods[Q], 0, 1)

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

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

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

				return mods
			},
		},
	}
}
