// 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 models

import (
	"context"
	"fmt"
	"io"

	"github.com/Gleipnir-Technology/bob"
	"github.com/Gleipnir-Technology/bob/dialect/psql"
	"github.com/Gleipnir-Technology/bob/dialect/psql/dialect"
	"github.com/Gleipnir-Technology/bob/dialect/psql/dm"
	"github.com/Gleipnir-Technology/bob/dialect/psql/sm"
	"github.com/Gleipnir-Technology/bob/dialect/psql/um"
	"github.com/Gleipnir-Technology/bob/expr"
	"github.com/Gleipnir-Technology/bob/orm"
	"github.com/Gleipnir-Technology/bob/types/pgtypes"
	"github.com/aarondl/opt/omit"
)

// ReportText is an object representing the database table.
type ReportText struct {
	CreatorID int32 `db:"creator_id,pk" `
	ReportID  int32 `db:"report_id,pk" `
	TextLogID int32 `db:"text_log_id,pk" `

	R reportTextR `db:"-" `
}

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

// ReportTexts contains methods to work with the report_text table
var ReportTexts = psql.NewTablex[*ReportText, ReportTextSlice, *ReportTextSetter]("", "report_text", buildReportTextColumns("report_text"))

// ReportTextsQuery is a query on the report_text table
type ReportTextsQuery = *psql.ViewQuery[*ReportText, ReportTextSlice]

// reportTextR is where relationships are stored.
type reportTextR struct {
	CreatorUser *User               // report_text.report_text_creator_id_fkey
	Report      *PublicreportReport // report_text.report_text_report_id_fkey
	TextLog     *CommsTextLog       // report_text.report_text_text_log_id_fkey
}

func buildReportTextColumns(alias string) reportTextColumns {
	return reportTextColumns{
		ColumnsExpr: expr.NewColumnsExpr(
			"creator_id", "report_id", "text_log_id",
		).WithParent("report_text"),
		tableAlias: alias,
		CreatorID:  psql.Quote(alias, "creator_id"),
		ReportID:   psql.Quote(alias, "report_id"),
		TextLogID:  psql.Quote(alias, "text_log_id"),
	}
}

type reportTextColumns struct {
	expr.ColumnsExpr
	tableAlias string
	CreatorID  psql.Expression
	ReportID   psql.Expression
	TextLogID  psql.Expression
}

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

func (reportTextColumns) AliasedAs(alias string) reportTextColumns {
	return buildReportTextColumns(alias)
}

// ReportTextSetter is used for insert/upsert/update operations
// All values are optional, and do not have to be set
// Generated columns are not included
type ReportTextSetter struct {
	CreatorID omit.Val[int32] `db:"creator_id,pk" `
	ReportID  omit.Val[int32] `db:"report_id,pk" `
	TextLogID omit.Val[int32] `db:"text_log_id,pk" `
}

func (s ReportTextSetter) SetColumns() []string {
	vals := make([]string, 0, 3)
	if s.CreatorID.IsValue() {
		vals = append(vals, "creator_id")
	}
	if s.ReportID.IsValue() {
		vals = append(vals, "report_id")
	}
	if s.TextLogID.IsValue() {
		vals = append(vals, "text_log_id")
	}
	return vals
}

func (s ReportTextSetter) Overwrite(t *ReportText) {
	if s.CreatorID.IsValue() {
		t.CreatorID = s.CreatorID.MustGet()
	}
	if s.ReportID.IsValue() {
		t.ReportID = s.ReportID.MustGet()
	}
	if s.TextLogID.IsValue() {
		t.TextLogID = s.TextLogID.MustGet()
	}
}

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

	q.AppendValues(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
		vals := make([]bob.Expression, 3)
		if s.CreatorID.IsValue() {
			vals[0] = psql.Arg(s.CreatorID.MustGet())
		} else {
			vals[0] = psql.Raw("DEFAULT")
		}

		if s.ReportID.IsValue() {
			vals[1] = psql.Arg(s.ReportID.MustGet())
		} else {
			vals[1] = psql.Raw("DEFAULT")
		}

		if s.TextLogID.IsValue() {
			vals[2] = psql.Arg(s.TextLogID.MustGet())
		} else {
			vals[2] = psql.Raw("DEFAULT")
		}

		return bob.ExpressSlice(ctx, w, d, start, vals, "", ", ", "")
	}))
}

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

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

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

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

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

	return exprs
}

// FindReportText retrieves a single record by primary key
// If cols is empty Find will return all columns.
func FindReportText(ctx context.Context, exec bob.Executor, CreatorIDPK int32, ReportIDPK int32, TextLogIDPK int32, cols ...string) (*ReportText, error) {
	if len(cols) == 0 {
		return ReportTexts.Query(
			sm.Where(ReportTexts.Columns.CreatorID.EQ(psql.Arg(CreatorIDPK))),
			sm.Where(ReportTexts.Columns.ReportID.EQ(psql.Arg(ReportIDPK))),
			sm.Where(ReportTexts.Columns.TextLogID.EQ(psql.Arg(TextLogIDPK))),
		).One(ctx, exec)
	}

	return ReportTexts.Query(
		sm.Where(ReportTexts.Columns.CreatorID.EQ(psql.Arg(CreatorIDPK))),
		sm.Where(ReportTexts.Columns.ReportID.EQ(psql.Arg(ReportIDPK))),
		sm.Where(ReportTexts.Columns.TextLogID.EQ(psql.Arg(TextLogIDPK))),
		sm.Columns(ReportTexts.Columns.Only(cols...)),
	).One(ctx, exec)
}

// ReportTextExists checks the presence of a single record by primary key
func ReportTextExists(ctx context.Context, exec bob.Executor, CreatorIDPK int32, ReportIDPK int32, TextLogIDPK int32) (bool, error) {
	return ReportTexts.Query(
		sm.Where(ReportTexts.Columns.CreatorID.EQ(psql.Arg(CreatorIDPK))),
		sm.Where(ReportTexts.Columns.ReportID.EQ(psql.Arg(ReportIDPK))),
		sm.Where(ReportTexts.Columns.TextLogID.EQ(psql.Arg(TextLogIDPK))),
	).Exists(ctx, exec)
}

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

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

	return err
}

// primaryKeyVals returns the primary key values of the ReportText
func (o *ReportText) primaryKeyVals() bob.Expression {
	return psql.ArgGroup(
		o.CreatorID,
		o.ReportID,
		o.TextLogID,
	)
}

func (o *ReportText) pkEQ() dialect.Expression {
	return psql.Group(psql.Quote("report_text", "creator_id"), psql.Quote("report_text", "report_id"), psql.Quote("report_text", "text_log_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 ReportText
func (o *ReportText) Update(ctx context.Context, exec bob.Executor, s *ReportTextSetter) error {
	v, err := ReportTexts.Update(s.UpdateMod(), um.Where(o.pkEQ())).One(ctx, exec)
	if err != nil {
		return err
	}

	o.R = v.R
	*o = *v

	return nil
}

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

// Reload refreshes the ReportText using the executor
func (o *ReportText) Reload(ctx context.Context, exec bob.Executor) error {
	o2, err := ReportTexts.Query(
		sm.Where(ReportTexts.Columns.CreatorID.EQ(psql.Arg(o.CreatorID))),
		sm.Where(ReportTexts.Columns.ReportID.EQ(psql.Arg(o.ReportID))),
		sm.Where(ReportTexts.Columns.TextLogID.EQ(psql.Arg(o.TextLogID))),
	).One(ctx, exec)
	if err != nil {
		return err
	}
	o2.R = o.R
	*o = *o2

	return nil
}

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

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

	return err
}

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

	return psql.Group(psql.Quote("report_text", "creator_id"), psql.Quote("report_text", "report_id"), psql.Quote("report_text", "text_log_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 ReportTextSlice) copyMatchingRows(from ...*ReportText) {
	for i, old := range o {
		for _, new := range from {
			if new.CreatorID != old.CreatorID {
				continue
			}
			if new.ReportID != old.ReportID {
				continue
			}
			if new.TextLogID != old.TextLogID {
				continue
			}
			new.R = old.R
			o[i] = new
			break
		}
	}
}

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

			return err
		}))

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

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

			return err
		}))

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

func (o ReportTextSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals ReportTextSetter) error {
	if len(o) == 0 {
		return nil
	}

	_, err := ReportTexts.Update(vals.UpdateMod(), o.UpdateMod()).All(ctx, exec)
	return err
}

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

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

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

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

	o.copyMatchingRows(o2...)

	return nil
}

// CreatorUser starts a query for related objects on user_
func (o *ReportText) CreatorUser(mods ...bob.Mod[*dialect.SelectQuery]) UsersQuery {
	return Users.Query(append(mods,
		sm.Where(Users.Columns.ID.EQ(psql.Arg(o.CreatorID))),
	)...)
}

func (os ReportTextSlice) CreatorUser(mods ...bob.Mod[*dialect.SelectQuery]) UsersQuery {
	pkCreatorID := make(pgtypes.Array[int32], 0, len(os))
	for _, o := range os {
		if o == nil {
			continue
		}
		pkCreatorID = append(pkCreatorID, o.CreatorID)
	}
	PKArgExpr := psql.Select(sm.Columns(
		psql.F("unnest", psql.Cast(psql.Arg(pkCreatorID), "integer[]")),
	))

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

// Report starts a query for related objects on publicreport.report
func (o *ReportText) Report(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportReportsQuery {
	return PublicreportReports.Query(append(mods,
		sm.Where(PublicreportReports.Columns.ID.EQ(psql.Arg(o.ReportID))),
	)...)
}

func (os ReportTextSlice) Report(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportReportsQuery {
	pkReportID := make(pgtypes.Array[int32], 0, len(os))
	for _, o := range os {
		if o == nil {
			continue
		}
		pkReportID = append(pkReportID, o.ReportID)
	}
	PKArgExpr := psql.Select(sm.Columns(
		psql.F("unnest", psql.Cast(psql.Arg(pkReportID), "integer[]")),
	))

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

// TextLog starts a query for related objects on comms.text_log
func (o *ReportText) TextLog(mods ...bob.Mod[*dialect.SelectQuery]) CommsTextLogsQuery {
	return CommsTextLogs.Query(append(mods,
		sm.Where(CommsTextLogs.Columns.ID.EQ(psql.Arg(o.TextLogID))),
	)...)
}

func (os ReportTextSlice) TextLog(mods ...bob.Mod[*dialect.SelectQuery]) CommsTextLogsQuery {
	pkTextLogID := make(pgtypes.Array[int32], 0, len(os))
	for _, o := range os {
		if o == nil {
			continue
		}
		pkTextLogID = append(pkTextLogID, o.TextLogID)
	}
	PKArgExpr := psql.Select(sm.Columns(
		psql.F("unnest", psql.Cast(psql.Arg(pkTextLogID), "integer[]")),
	))

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

func attachReportTextCreatorUser0(ctx context.Context, exec bob.Executor, count int, reportText0 *ReportText, user1 *User) (*ReportText, error) {
	setter := &ReportTextSetter{
		CreatorID: omit.From(user1.ID),
	}

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

	return reportText0, nil
}

func (reportText0 *ReportText) InsertCreatorUser(ctx context.Context, exec bob.Executor, related *UserSetter) error {
	var err error

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

	_, err = attachReportTextCreatorUser0(ctx, exec, 1, reportText0, user1)
	if err != nil {
		return err
	}

	reportText0.R.CreatorUser = user1

	user1.R.CreatorReportTexts = append(user1.R.CreatorReportTexts, reportText0)

	return nil
}

func (reportText0 *ReportText) AttachCreatorUser(ctx context.Context, exec bob.Executor, user1 *User) error {
	var err error

	_, err = attachReportTextCreatorUser0(ctx, exec, 1, reportText0, user1)
	if err != nil {
		return err
	}

	reportText0.R.CreatorUser = user1

	user1.R.CreatorReportTexts = append(user1.R.CreatorReportTexts, reportText0)

	return nil
}

func attachReportTextReport0(ctx context.Context, exec bob.Executor, count int, reportText0 *ReportText, publicreportReport1 *PublicreportReport) (*ReportText, error) {
	setter := &ReportTextSetter{
		ReportID: omit.From(publicreportReport1.ID),
	}

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

	return reportText0, nil
}

func (reportText0 *ReportText) InsertReport(ctx context.Context, exec bob.Executor, related *PublicreportReportSetter) error {
	var err error

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

	_, err = attachReportTextReport0(ctx, exec, 1, reportText0, publicreportReport1)
	if err != nil {
		return err
	}

	reportText0.R.Report = publicreportReport1

	publicreportReport1.R.ReportTexts = append(publicreportReport1.R.ReportTexts, reportText0)

	return nil
}

func (reportText0 *ReportText) AttachReport(ctx context.Context, exec bob.Executor, publicreportReport1 *PublicreportReport) error {
	var err error

	_, err = attachReportTextReport0(ctx, exec, 1, reportText0, publicreportReport1)
	if err != nil {
		return err
	}

	reportText0.R.Report = publicreportReport1

	publicreportReport1.R.ReportTexts = append(publicreportReport1.R.ReportTexts, reportText0)

	return nil
}

func attachReportTextTextLog0(ctx context.Context, exec bob.Executor, count int, reportText0 *ReportText, commsTextLog1 *CommsTextLog) (*ReportText, error) {
	setter := &ReportTextSetter{
		TextLogID: omit.From(commsTextLog1.ID),
	}

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

	return reportText0, nil
}

func (reportText0 *ReportText) InsertTextLog(ctx context.Context, exec bob.Executor, related *CommsTextLogSetter) error {
	var err error

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

	_, err = attachReportTextTextLog0(ctx, exec, 1, reportText0, commsTextLog1)
	if err != nil {
		return err
	}

	reportText0.R.TextLog = commsTextLog1

	commsTextLog1.R.ReportTexts = append(commsTextLog1.R.ReportTexts, reportText0)

	return nil
}

func (reportText0 *ReportText) AttachTextLog(ctx context.Context, exec bob.Executor, commsTextLog1 *CommsTextLog) error {
	var err error

	_, err = attachReportTextTextLog0(ctx, exec, 1, reportText0, commsTextLog1)
	if err != nil {
		return err
	}

	reportText0.R.TextLog = commsTextLog1

	commsTextLog1.R.ReportTexts = append(commsTextLog1.R.ReportTexts, reportText0)

	return nil
}

type reportTextWhere[Q psql.Filterable] struct {
	CreatorID psql.WhereMod[Q, int32]
	ReportID  psql.WhereMod[Q, int32]
	TextLogID psql.WhereMod[Q, int32]
}

func (reportTextWhere[Q]) AliasedAs(alias string) reportTextWhere[Q] {
	return buildReportTextWhere[Q](buildReportTextColumns(alias))
}

func buildReportTextWhere[Q psql.Filterable](cols reportTextColumns) reportTextWhere[Q] {
	return reportTextWhere[Q]{
		CreatorID: psql.Where[Q, int32](cols.CreatorID),
		ReportID:  psql.Where[Q, int32](cols.ReportID),
		TextLogID: psql.Where[Q, int32](cols.TextLogID),
	}
}

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

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

		o.R.CreatorUser = rel

		if rel != nil {
			rel.R.CreatorReportTexts = ReportTextSlice{o}
		}
		return nil
	case "Report":
		rel, ok := retrieved.(*PublicreportReport)
		if !ok {
			return fmt.Errorf("reportText cannot load %T as %q", retrieved, name)
		}

		o.R.Report = rel

		if rel != nil {
			rel.R.ReportTexts = ReportTextSlice{o}
		}
		return nil
	case "TextLog":
		rel, ok := retrieved.(*CommsTextLog)
		if !ok {
			return fmt.Errorf("reportText cannot load %T as %q", retrieved, name)
		}

		o.R.TextLog = rel

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

type reportTextPreloader struct {
	CreatorUser func(...psql.PreloadOption) psql.Preloader
	Report      func(...psql.PreloadOption) psql.Preloader
	TextLog     func(...psql.PreloadOption) psql.Preloader
}

func buildReportTextPreloader() reportTextPreloader {
	return reportTextPreloader{
		CreatorUser: func(opts ...psql.PreloadOption) psql.Preloader {
			return psql.Preload[*User, UserSlice](psql.PreloadRel{
				Name: "CreatorUser",
				Sides: []psql.PreloadSide{
					{
						From:        ReportTexts,
						To:          Users,
						FromColumns: []string{"creator_id"},
						ToColumns:   []string{"id"},
					},
				},
			}, Users.Columns.Names(), opts...)
		},
		Report: func(opts ...psql.PreloadOption) psql.Preloader {
			return psql.Preload[*PublicreportReport, PublicreportReportSlice](psql.PreloadRel{
				Name: "Report",
				Sides: []psql.PreloadSide{
					{
						From:        ReportTexts,
						To:          PublicreportReports,
						FromColumns: []string{"report_id"},
						ToColumns:   []string{"id"},
					},
				},
			}, PublicreportReports.Columns.Names(), opts...)
		},
		TextLog: func(opts ...psql.PreloadOption) psql.Preloader {
			return psql.Preload[*CommsTextLog, CommsTextLogSlice](psql.PreloadRel{
				Name: "TextLog",
				Sides: []psql.PreloadSide{
					{
						From:        ReportTexts,
						To:          CommsTextLogs,
						FromColumns: []string{"text_log_id"},
						ToColumns:   []string{"id"},
					},
				},
			}, CommsTextLogs.Columns.Names(), opts...)
		},
	}
}

type reportTextThenLoader[Q orm.Loadable] struct {
	CreatorUser func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	Report      func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	TextLog     func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
}

func buildReportTextThenLoader[Q orm.Loadable]() reportTextThenLoader[Q] {
	type CreatorUserLoadInterface interface {
		LoadCreatorUser(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type ReportLoadInterface interface {
		LoadReport(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type TextLogLoadInterface interface {
		LoadTextLog(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}

	return reportTextThenLoader[Q]{
		CreatorUser: thenLoadBuilder[Q](
			"CreatorUser",
			func(ctx context.Context, exec bob.Executor, retrieved CreatorUserLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadCreatorUser(ctx, exec, mods...)
			},
		),
		Report: thenLoadBuilder[Q](
			"Report",
			func(ctx context.Context, exec bob.Executor, retrieved ReportLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadReport(ctx, exec, mods...)
			},
		),
		TextLog: thenLoadBuilder[Q](
			"TextLog",
			func(ctx context.Context, exec bob.Executor, retrieved TextLogLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadTextLog(ctx, exec, mods...)
			},
		),
	}
}

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

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

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

	related.R.CreatorReportTexts = ReportTextSlice{o}

	o.R.CreatorUser = related
	return nil
}

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

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

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

		for _, rel := range users {

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

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

			o.R.CreatorUser = rel
			break
		}
	}

	return nil
}

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

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

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

	related.R.ReportTexts = ReportTextSlice{o}

	o.R.Report = related
	return nil
}

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

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

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

		for _, rel := range publicreportReports {

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

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

			o.R.Report = rel
			break
		}
	}

	return nil
}

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

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

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

	related.R.ReportTexts = ReportTextSlice{o}

	o.R.TextLog = related
	return nil
}

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

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

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

		for _, rel := range commsTextLogs {

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

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

			o.R.TextLog = rel
			break
		}
	}

	return nil
}
