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

// PublicreportNuisanceImageOld is an object representing the database table.
type PublicreportNuisanceImageOld struct {
	ImageID    int32 `db:"image_id,pk" `
	NuisanceID int32 `db:"nuisance_id,pk" `

	R publicreportNuisanceImageOldR `db:"-" `
}

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

// PublicreportNuisanceImageOlds contains methods to work with the nuisance_image_old table
var PublicreportNuisanceImageOlds = psql.NewTablex[*PublicreportNuisanceImageOld, PublicreportNuisanceImageOldSlice, *PublicreportNuisanceImageOldSetter]("publicreport", "nuisance_image_old", buildPublicreportNuisanceImageOldColumns("publicreport.nuisance_image_old"))

// PublicreportNuisanceImageOldsQuery is a query on the nuisance_image_old table
type PublicreportNuisanceImageOldsQuery = *psql.ViewQuery[*PublicreportNuisanceImageOld, PublicreportNuisanceImageOldSlice]

// publicreportNuisanceImageOldR is where relationships are stored.
type publicreportNuisanceImageOldR struct {
	Image               *PublicreportImage       // publicreport.nuisance_image_old.nuisance_image_image_id_fkey
	NuisanceNuisanceOld *PublicreportNuisanceOld // publicreport.nuisance_image_old.nuisance_image_nuisance_id_fkey
}

func buildPublicreportNuisanceImageOldColumns(alias string) publicreportNuisanceImageOldColumns {
	return publicreportNuisanceImageOldColumns{
		ColumnsExpr: expr.NewColumnsExpr(
			"image_id", "nuisance_id",
		).WithParent("publicreport.nuisance_image_old"),
		tableAlias: alias,
		ImageID:    psql.Quote(alias, "image_id"),
		NuisanceID: psql.Quote(alias, "nuisance_id"),
	}
}

type publicreportNuisanceImageOldColumns struct {
	expr.ColumnsExpr
	tableAlias string
	ImageID    psql.Expression
	NuisanceID psql.Expression
}

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

func (publicreportNuisanceImageOldColumns) AliasedAs(alias string) publicreportNuisanceImageOldColumns {
	return buildPublicreportNuisanceImageOldColumns(alias)
}

// PublicreportNuisanceImageOldSetter is used for insert/upsert/update operations
// All values are optional, and do not have to be set
// Generated columns are not included
type PublicreportNuisanceImageOldSetter struct {
	ImageID    omit.Val[int32] `db:"image_id,pk" `
	NuisanceID omit.Val[int32] `db:"nuisance_id,pk" `
}

func (s PublicreportNuisanceImageOldSetter) SetColumns() []string {
	vals := make([]string, 0, 2)
	if s.ImageID.IsValue() {
		vals = append(vals, "image_id")
	}
	if s.NuisanceID.IsValue() {
		vals = append(vals, "nuisance_id")
	}
	return vals
}

func (s PublicreportNuisanceImageOldSetter) Overwrite(t *PublicreportNuisanceImageOld) {
	if s.ImageID.IsValue() {
		t.ImageID = s.ImageID.MustGet()
	}
	if s.NuisanceID.IsValue() {
		t.NuisanceID = s.NuisanceID.MustGet()
	}
}

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

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

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

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

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

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

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

	return exprs
}

// FindPublicreportNuisanceImageOld retrieves a single record by primary key
// If cols is empty Find will return all columns.
func FindPublicreportNuisanceImageOld(ctx context.Context, exec bob.Executor, ImageIDPK int32, NuisanceIDPK int32, cols ...string) (*PublicreportNuisanceImageOld, error) {
	if len(cols) == 0 {
		return PublicreportNuisanceImageOlds.Query(
			sm.Where(PublicreportNuisanceImageOlds.Columns.ImageID.EQ(psql.Arg(ImageIDPK))),
			sm.Where(PublicreportNuisanceImageOlds.Columns.NuisanceID.EQ(psql.Arg(NuisanceIDPK))),
		).One(ctx, exec)
	}

	return PublicreportNuisanceImageOlds.Query(
		sm.Where(PublicreportNuisanceImageOlds.Columns.ImageID.EQ(psql.Arg(ImageIDPK))),
		sm.Where(PublicreportNuisanceImageOlds.Columns.NuisanceID.EQ(psql.Arg(NuisanceIDPK))),
		sm.Columns(PublicreportNuisanceImageOlds.Columns.Only(cols...)),
	).One(ctx, exec)
}

// PublicreportNuisanceImageOldExists checks the presence of a single record by primary key
func PublicreportNuisanceImageOldExists(ctx context.Context, exec bob.Executor, ImageIDPK int32, NuisanceIDPK int32) (bool, error) {
	return PublicreportNuisanceImageOlds.Query(
		sm.Where(PublicreportNuisanceImageOlds.Columns.ImageID.EQ(psql.Arg(ImageIDPK))),
		sm.Where(PublicreportNuisanceImageOlds.Columns.NuisanceID.EQ(psql.Arg(NuisanceIDPK))),
	).Exists(ctx, exec)
}

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

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

	return err
}

// primaryKeyVals returns the primary key values of the PublicreportNuisanceImageOld
func (o *PublicreportNuisanceImageOld) primaryKeyVals() bob.Expression {
	return psql.ArgGroup(
		o.ImageID,
		o.NuisanceID,
	)
}

func (o *PublicreportNuisanceImageOld) pkEQ() dialect.Expression {
	return psql.Group(psql.Quote("publicreport.nuisance_image_old", "image_id"), psql.Quote("publicreport.nuisance_image_old", "nuisance_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 PublicreportNuisanceImageOld
func (o *PublicreportNuisanceImageOld) Update(ctx context.Context, exec bob.Executor, s *PublicreportNuisanceImageOldSetter) error {
	v, err := PublicreportNuisanceImageOlds.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 PublicreportNuisanceImageOld record with an executor
func (o *PublicreportNuisanceImageOld) Delete(ctx context.Context, exec bob.Executor) error {
	_, err := PublicreportNuisanceImageOlds.Delete(dm.Where(o.pkEQ())).Exec(ctx, exec)
	return err
}

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

	return nil
}

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

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

	return err
}

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

	return psql.Group(psql.Quote("publicreport.nuisance_image_old", "image_id"), psql.Quote("publicreport.nuisance_image_old", "nuisance_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 PublicreportNuisanceImageOldSlice) copyMatchingRows(from ...*PublicreportNuisanceImageOld) {
	for i, old := range o {
		for _, new := range from {
			if new.ImageID != old.ImageID {
				continue
			}
			if new.NuisanceID != old.NuisanceID {
				continue
			}
			new.R = old.R
			o[i] = new
			break
		}
	}
}

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

			return err
		}))

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

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

			return err
		}))

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

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

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

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

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

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

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

	o.copyMatchingRows(o2...)

	return nil
}

// Image starts a query for related objects on publicreport.image
func (o *PublicreportNuisanceImageOld) Image(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportImagesQuery {
	return PublicreportImages.Query(append(mods,
		sm.Where(PublicreportImages.Columns.ID.EQ(psql.Arg(o.ImageID))),
	)...)
}

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

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

// NuisanceNuisanceOld starts a query for related objects on publicreport.nuisance_old
func (o *PublicreportNuisanceImageOld) NuisanceNuisanceOld(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportNuisanceOldsQuery {
	return PublicreportNuisanceOlds.Query(append(mods,
		sm.Where(PublicreportNuisanceOlds.Columns.ID.EQ(psql.Arg(o.NuisanceID))),
	)...)
}

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

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

func attachPublicreportNuisanceImageOldImage0(ctx context.Context, exec bob.Executor, count int, publicreportNuisanceImageOld0 *PublicreportNuisanceImageOld, publicreportImage1 *PublicreportImage) (*PublicreportNuisanceImageOld, error) {
	setter := &PublicreportNuisanceImageOldSetter{
		ImageID: omit.From(publicreportImage1.ID),
	}

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

	return publicreportNuisanceImageOld0, nil
}

func (publicreportNuisanceImageOld0 *PublicreportNuisanceImageOld) InsertImage(ctx context.Context, exec bob.Executor, related *PublicreportImageSetter) error {
	var err error

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

	_, err = attachPublicreportNuisanceImageOldImage0(ctx, exec, 1, publicreportNuisanceImageOld0, publicreportImage1)
	if err != nil {
		return err
	}

	publicreportNuisanceImageOld0.R.Image = publicreportImage1

	return nil
}

func (publicreportNuisanceImageOld0 *PublicreportNuisanceImageOld) AttachImage(ctx context.Context, exec bob.Executor, publicreportImage1 *PublicreportImage) error {
	var err error

	_, err = attachPublicreportNuisanceImageOldImage0(ctx, exec, 1, publicreportNuisanceImageOld0, publicreportImage1)
	if err != nil {
		return err
	}

	publicreportNuisanceImageOld0.R.Image = publicreportImage1

	return nil
}

func attachPublicreportNuisanceImageOldNuisanceNuisanceOld0(ctx context.Context, exec bob.Executor, count int, publicreportNuisanceImageOld0 *PublicreportNuisanceImageOld, publicreportNuisanceOld1 *PublicreportNuisanceOld) (*PublicreportNuisanceImageOld, error) {
	setter := &PublicreportNuisanceImageOldSetter{
		NuisanceID: omit.From(publicreportNuisanceOld1.ID),
	}

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

	return publicreportNuisanceImageOld0, nil
}

func (publicreportNuisanceImageOld0 *PublicreportNuisanceImageOld) InsertNuisanceNuisanceOld(ctx context.Context, exec bob.Executor, related *PublicreportNuisanceOldSetter) error {
	var err error

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

	_, err = attachPublicreportNuisanceImageOldNuisanceNuisanceOld0(ctx, exec, 1, publicreportNuisanceImageOld0, publicreportNuisanceOld1)
	if err != nil {
		return err
	}

	publicreportNuisanceImageOld0.R.NuisanceNuisanceOld = publicreportNuisanceOld1

	return nil
}

func (publicreportNuisanceImageOld0 *PublicreportNuisanceImageOld) AttachNuisanceNuisanceOld(ctx context.Context, exec bob.Executor, publicreportNuisanceOld1 *PublicreportNuisanceOld) error {
	var err error

	_, err = attachPublicreportNuisanceImageOldNuisanceNuisanceOld0(ctx, exec, 1, publicreportNuisanceImageOld0, publicreportNuisanceOld1)
	if err != nil {
		return err
	}

	publicreportNuisanceImageOld0.R.NuisanceNuisanceOld = publicreportNuisanceOld1

	return nil
}

type publicreportNuisanceImageOldWhere[Q psql.Filterable] struct {
	ImageID    psql.WhereMod[Q, int32]
	NuisanceID psql.WhereMod[Q, int32]
}

func (publicreportNuisanceImageOldWhere[Q]) AliasedAs(alias string) publicreportNuisanceImageOldWhere[Q] {
	return buildPublicreportNuisanceImageOldWhere[Q](buildPublicreportNuisanceImageOldColumns(alias))
}

func buildPublicreportNuisanceImageOldWhere[Q psql.Filterable](cols publicreportNuisanceImageOldColumns) publicreportNuisanceImageOldWhere[Q] {
	return publicreportNuisanceImageOldWhere[Q]{
		ImageID:    psql.Where[Q, int32](cols.ImageID),
		NuisanceID: psql.Where[Q, int32](cols.NuisanceID),
	}
}

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

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

		o.R.Image = rel

		return nil
	case "NuisanceNuisanceOld":
		rel, ok := retrieved.(*PublicreportNuisanceOld)
		if !ok {
			return fmt.Errorf("publicreportNuisanceImageOld cannot load %T as %q", retrieved, name)
		}

		o.R.NuisanceNuisanceOld = rel

		return nil
	default:
		return fmt.Errorf("publicreportNuisanceImageOld has no relationship %q", name)
	}
}

type publicreportNuisanceImageOldPreloader struct {
	Image               func(...psql.PreloadOption) psql.Preloader
	NuisanceNuisanceOld func(...psql.PreloadOption) psql.Preloader
}

func buildPublicreportNuisanceImageOldPreloader() publicreportNuisanceImageOldPreloader {
	return publicreportNuisanceImageOldPreloader{
		Image: func(opts ...psql.PreloadOption) psql.Preloader {
			return psql.Preload[*PublicreportImage, PublicreportImageSlice](psql.PreloadRel{
				Name: "Image",
				Sides: []psql.PreloadSide{
					{
						From:        PublicreportNuisanceImageOlds,
						To:          PublicreportImages,
						FromColumns: []string{"image_id"},
						ToColumns:   []string{"id"},
					},
				},
			}, PublicreportImages.Columns.Names(), opts...)
		},
		NuisanceNuisanceOld: func(opts ...psql.PreloadOption) psql.Preloader {
			return psql.Preload[*PublicreportNuisanceOld, PublicreportNuisanceOldSlice](psql.PreloadRel{
				Name: "NuisanceNuisanceOld",
				Sides: []psql.PreloadSide{
					{
						From:        PublicreportNuisanceImageOlds,
						To:          PublicreportNuisanceOlds,
						FromColumns: []string{"nuisance_id"},
						ToColumns:   []string{"id"},
					},
				},
			}, PublicreportNuisanceOlds.Columns.Names(), opts...)
		},
	}
}

type publicreportNuisanceImageOldThenLoader[Q orm.Loadable] struct {
	Image               func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	NuisanceNuisanceOld func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
}

func buildPublicreportNuisanceImageOldThenLoader[Q orm.Loadable]() publicreportNuisanceImageOldThenLoader[Q] {
	type ImageLoadInterface interface {
		LoadImage(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type NuisanceNuisanceOldLoadInterface interface {
		LoadNuisanceNuisanceOld(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}

	return publicreportNuisanceImageOldThenLoader[Q]{
		Image: thenLoadBuilder[Q](
			"Image",
			func(ctx context.Context, exec bob.Executor, retrieved ImageLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadImage(ctx, exec, mods...)
			},
		),
		NuisanceNuisanceOld: thenLoadBuilder[Q](
			"NuisanceNuisanceOld",
			func(ctx context.Context, exec bob.Executor, retrieved NuisanceNuisanceOldLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadNuisanceNuisanceOld(ctx, exec, mods...)
			},
		),
	}
}

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

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

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

	o.R.Image = related
	return nil
}

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

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

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

		for _, rel := range publicreportImages {

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

			o.R.Image = rel
			break
		}
	}

	return nil
}

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

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

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

	o.R.NuisanceNuisanceOld = related
	return nil
}

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

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

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

		for _, rel := range publicreportNuisanceOlds {

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

			o.R.NuisanceNuisanceOld = rel
			break
		}
	}

	return nil
}
