// 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"
)

// ComplianceReportRequestMailer is an object representing the database table.
type ComplianceReportRequestMailer struct {
	ComplianceReportRequestID int32 `db:"compliance_report_request_id" `
	MailerID                  int32 `db:"mailer_id" `
	ID                        int32 `db:"id,pk" `

	R complianceReportRequestMailerR `db:"-" `
}

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

// ComplianceReportRequestMailers contains methods to work with the compliance_report_request_mailer table
var ComplianceReportRequestMailers = psql.NewTablex[*ComplianceReportRequestMailer, ComplianceReportRequestMailerSlice, *ComplianceReportRequestMailerSetter]("", "compliance_report_request_mailer", buildComplianceReportRequestMailerColumns("compliance_report_request_mailer"))

// ComplianceReportRequestMailersQuery is a query on the compliance_report_request_mailer table
type ComplianceReportRequestMailersQuery = *psql.ViewQuery[*ComplianceReportRequestMailer, ComplianceReportRequestMailerSlice]

// complianceReportRequestMailerR is where relationships are stored.
type complianceReportRequestMailerR struct {
	ComplianceReportRequest *ComplianceReportRequest // compliance_report_request_mailer.compliance_report_request_mai_compliance_report_request_id_fkey
	Mailer                  *CommsMailer             // compliance_report_request_mailer.compliance_report_request_mailer_mailer_id_fkey
}

func buildComplianceReportRequestMailerColumns(alias string) complianceReportRequestMailerColumns {
	return complianceReportRequestMailerColumns{
		ColumnsExpr: expr.NewColumnsExpr(
			"compliance_report_request_id", "mailer_id", "id",
		).WithParent("compliance_report_request_mailer"),
		tableAlias:                alias,
		ComplianceReportRequestID: psql.Quote(alias, "compliance_report_request_id"),
		MailerID:                  psql.Quote(alias, "mailer_id"),
		ID:                        psql.Quote(alias, "id"),
	}
}

type complianceReportRequestMailerColumns struct {
	expr.ColumnsExpr
	tableAlias                string
	ComplianceReportRequestID psql.Expression
	MailerID                  psql.Expression
	ID                        psql.Expression
}

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

func (complianceReportRequestMailerColumns) AliasedAs(alias string) complianceReportRequestMailerColumns {
	return buildComplianceReportRequestMailerColumns(alias)
}

// ComplianceReportRequestMailerSetter is used for insert/upsert/update operations
// All values are optional, and do not have to be set
// Generated columns are not included
type ComplianceReportRequestMailerSetter struct {
	ComplianceReportRequestID omit.Val[int32] `db:"compliance_report_request_id" `
	MailerID                  omit.Val[int32] `db:"mailer_id" `
	ID                        omit.Val[int32] `db:"id,pk" `
}

func (s ComplianceReportRequestMailerSetter) SetColumns() []string {
	vals := make([]string, 0, 3)
	if s.ComplianceReportRequestID.IsValue() {
		vals = append(vals, "compliance_report_request_id")
	}
	if s.MailerID.IsValue() {
		vals = append(vals, "mailer_id")
	}
	if s.ID.IsValue() {
		vals = append(vals, "id")
	}
	return vals
}

func (s ComplianceReportRequestMailerSetter) Overwrite(t *ComplianceReportRequestMailer) {
	if s.ComplianceReportRequestID.IsValue() {
		t.ComplianceReportRequestID = s.ComplianceReportRequestID.MustGet()
	}
	if s.MailerID.IsValue() {
		t.MailerID = s.MailerID.MustGet()
	}
	if s.ID.IsValue() {
		t.ID = s.ID.MustGet()
	}
}

func (s *ComplianceReportRequestMailerSetter) Apply(q *dialect.InsertQuery) {
	q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
		return ComplianceReportRequestMailers.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.ComplianceReportRequestID.IsValue() {
			vals[0] = psql.Arg(s.ComplianceReportRequestID.MustGet())
		} else {
			vals[0] = psql.Raw("DEFAULT")
		}

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

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

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

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

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

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

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

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

	return exprs
}

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

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

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

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

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

	return err
}

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

func (o *ComplianceReportRequestMailer) pkEQ() dialect.Expression {
	return psql.Quote("compliance_report_request_mailer", "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 ComplianceReportRequestMailer
func (o *ComplianceReportRequestMailer) Update(ctx context.Context, exec bob.Executor, s *ComplianceReportRequestMailerSetter) error {
	v, err := ComplianceReportRequestMailers.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 ComplianceReportRequestMailer record with an executor
func (o *ComplianceReportRequestMailer) Delete(ctx context.Context, exec bob.Executor) error {
	_, err := ComplianceReportRequestMailers.Delete(dm.Where(o.pkEQ())).Exec(ctx, exec)
	return err
}

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

	return nil
}

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

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

	return err
}

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

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

			return err
		}))

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

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

			return err
		}))

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

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

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

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

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

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

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

	o.copyMatchingRows(o2...)

	return nil
}

// ComplianceReportRequest starts a query for related objects on compliance_report_request
func (o *ComplianceReportRequestMailer) ComplianceReportRequest(mods ...bob.Mod[*dialect.SelectQuery]) ComplianceReportRequestsQuery {
	return ComplianceReportRequests.Query(append(mods,
		sm.Where(ComplianceReportRequests.Columns.ID.EQ(psql.Arg(o.ComplianceReportRequestID))),
	)...)
}

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

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

// Mailer starts a query for related objects on comms.mailer
func (o *ComplianceReportRequestMailer) Mailer(mods ...bob.Mod[*dialect.SelectQuery]) CommsMailersQuery {
	return CommsMailers.Query(append(mods,
		sm.Where(CommsMailers.Columns.ID.EQ(psql.Arg(o.MailerID))),
	)...)
}

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

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

func attachComplianceReportRequestMailerComplianceReportRequest0(ctx context.Context, exec bob.Executor, count int, complianceReportRequestMailer0 *ComplianceReportRequestMailer, complianceReportRequest1 *ComplianceReportRequest) (*ComplianceReportRequestMailer, error) {
	setter := &ComplianceReportRequestMailerSetter{
		ComplianceReportRequestID: omit.From(complianceReportRequest1.ID),
	}

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

	return complianceReportRequestMailer0, nil
}

func (complianceReportRequestMailer0 *ComplianceReportRequestMailer) InsertComplianceReportRequest(ctx context.Context, exec bob.Executor, related *ComplianceReportRequestSetter) error {
	var err error

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

	_, err = attachComplianceReportRequestMailerComplianceReportRequest0(ctx, exec, 1, complianceReportRequestMailer0, complianceReportRequest1)
	if err != nil {
		return err
	}

	complianceReportRequestMailer0.R.ComplianceReportRequest = complianceReportRequest1

	complianceReportRequest1.R.ComplianceReportRequestMailers = append(complianceReportRequest1.R.ComplianceReportRequestMailers, complianceReportRequestMailer0)

	return nil
}

func (complianceReportRequestMailer0 *ComplianceReportRequestMailer) AttachComplianceReportRequest(ctx context.Context, exec bob.Executor, complianceReportRequest1 *ComplianceReportRequest) error {
	var err error

	_, err = attachComplianceReportRequestMailerComplianceReportRequest0(ctx, exec, 1, complianceReportRequestMailer0, complianceReportRequest1)
	if err != nil {
		return err
	}

	complianceReportRequestMailer0.R.ComplianceReportRequest = complianceReportRequest1

	complianceReportRequest1.R.ComplianceReportRequestMailers = append(complianceReportRequest1.R.ComplianceReportRequestMailers, complianceReportRequestMailer0)

	return nil
}

func attachComplianceReportRequestMailerMailer0(ctx context.Context, exec bob.Executor, count int, complianceReportRequestMailer0 *ComplianceReportRequestMailer, commsMailer1 *CommsMailer) (*ComplianceReportRequestMailer, error) {
	setter := &ComplianceReportRequestMailerSetter{
		MailerID: omit.From(commsMailer1.ID),
	}

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

	return complianceReportRequestMailer0, nil
}

func (complianceReportRequestMailer0 *ComplianceReportRequestMailer) InsertMailer(ctx context.Context, exec bob.Executor, related *CommsMailerSetter) error {
	var err error

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

	_, err = attachComplianceReportRequestMailerMailer0(ctx, exec, 1, complianceReportRequestMailer0, commsMailer1)
	if err != nil {
		return err
	}

	complianceReportRequestMailer0.R.Mailer = commsMailer1

	commsMailer1.R.ComplianceReportRequestMailers = append(commsMailer1.R.ComplianceReportRequestMailers, complianceReportRequestMailer0)

	return nil
}

func (complianceReportRequestMailer0 *ComplianceReportRequestMailer) AttachMailer(ctx context.Context, exec bob.Executor, commsMailer1 *CommsMailer) error {
	var err error

	_, err = attachComplianceReportRequestMailerMailer0(ctx, exec, 1, complianceReportRequestMailer0, commsMailer1)
	if err != nil {
		return err
	}

	complianceReportRequestMailer0.R.Mailer = commsMailer1

	commsMailer1.R.ComplianceReportRequestMailers = append(commsMailer1.R.ComplianceReportRequestMailers, complianceReportRequestMailer0)

	return nil
}

type complianceReportRequestMailerWhere[Q psql.Filterable] struct {
	ComplianceReportRequestID psql.WhereMod[Q, int32]
	MailerID                  psql.WhereMod[Q, int32]
	ID                        psql.WhereMod[Q, int32]
}

func (complianceReportRequestMailerWhere[Q]) AliasedAs(alias string) complianceReportRequestMailerWhere[Q] {
	return buildComplianceReportRequestMailerWhere[Q](buildComplianceReportRequestMailerColumns(alias))
}

func buildComplianceReportRequestMailerWhere[Q psql.Filterable](cols complianceReportRequestMailerColumns) complianceReportRequestMailerWhere[Q] {
	return complianceReportRequestMailerWhere[Q]{
		ComplianceReportRequestID: psql.Where[Q, int32](cols.ComplianceReportRequestID),
		MailerID:                  psql.Where[Q, int32](cols.MailerID),
		ID:                        psql.Where[Q, int32](cols.ID),
	}
}

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

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

		o.R.ComplianceReportRequest = rel

		if rel != nil {
			rel.R.ComplianceReportRequestMailers = ComplianceReportRequestMailerSlice{o}
		}
		return nil
	case "Mailer":
		rel, ok := retrieved.(*CommsMailer)
		if !ok {
			return fmt.Errorf("complianceReportRequestMailer cannot load %T as %q", retrieved, name)
		}

		o.R.Mailer = rel

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

type complianceReportRequestMailerPreloader struct {
	ComplianceReportRequest func(...psql.PreloadOption) psql.Preloader
	Mailer                  func(...psql.PreloadOption) psql.Preloader
}

func buildComplianceReportRequestMailerPreloader() complianceReportRequestMailerPreloader {
	return complianceReportRequestMailerPreloader{
		ComplianceReportRequest: func(opts ...psql.PreloadOption) psql.Preloader {
			return psql.Preload[*ComplianceReportRequest, ComplianceReportRequestSlice](psql.PreloadRel{
				Name: "ComplianceReportRequest",
				Sides: []psql.PreloadSide{
					{
						From:        ComplianceReportRequestMailers,
						To:          ComplianceReportRequests,
						FromColumns: []string{"compliance_report_request_id"},
						ToColumns:   []string{"id"},
					},
				},
			}, ComplianceReportRequests.Columns.Names(), opts...)
		},
		Mailer: func(opts ...psql.PreloadOption) psql.Preloader {
			return psql.Preload[*CommsMailer, CommsMailerSlice](psql.PreloadRel{
				Name: "Mailer",
				Sides: []psql.PreloadSide{
					{
						From:        ComplianceReportRequestMailers,
						To:          CommsMailers,
						FromColumns: []string{"mailer_id"},
						ToColumns:   []string{"id"},
					},
				},
			}, CommsMailers.Columns.Names(), opts...)
		},
	}
}

type complianceReportRequestMailerThenLoader[Q orm.Loadable] struct {
	ComplianceReportRequest func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	Mailer                  func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
}

func buildComplianceReportRequestMailerThenLoader[Q orm.Loadable]() complianceReportRequestMailerThenLoader[Q] {
	type ComplianceReportRequestLoadInterface interface {
		LoadComplianceReportRequest(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type MailerLoadInterface interface {
		LoadMailer(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}

	return complianceReportRequestMailerThenLoader[Q]{
		ComplianceReportRequest: thenLoadBuilder[Q](
			"ComplianceReportRequest",
			func(ctx context.Context, exec bob.Executor, retrieved ComplianceReportRequestLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadComplianceReportRequest(ctx, exec, mods...)
			},
		),
		Mailer: thenLoadBuilder[Q](
			"Mailer",
			func(ctx context.Context, exec bob.Executor, retrieved MailerLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadMailer(ctx, exec, mods...)
			},
		),
	}
}

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

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

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

	related.R.ComplianceReportRequestMailers = ComplianceReportRequestMailerSlice{o}

	o.R.ComplianceReportRequest = related
	return nil
}

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

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

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

		for _, rel := range complianceReportRequests {

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

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

			o.R.ComplianceReportRequest = rel
			break
		}
	}

	return nil
}

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

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

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

	related.R.ComplianceReportRequestMailers = ComplianceReportRequestMailerSlice{o}

	o.R.Mailer = related
	return nil
}

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

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

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

		for _, rel := range commsMailers {

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

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

			o.R.Mailer = rel
			break
		}
	}

	return nil
}
