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

// PublicreportWaterImageOld is an object representing the database table.
type PublicreportWaterImageOld struct {
	ImageID int32 `db:"image_id,pk" `
	WaterID int32 `db:"water_id,pk" `

	R publicreportWaterImageOldR `db:"-" `
}

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

// PublicreportWaterImageOlds contains methods to work with the water_image_old table
var PublicreportWaterImageOlds = psql.NewTablex[*PublicreportWaterImageOld, PublicreportWaterImageOldSlice, *PublicreportWaterImageOldSetter]("publicreport", "water_image_old", buildPublicreportWaterImageOldColumns("publicreport.water_image_old"))

// PublicreportWaterImageOldsQuery is a query on the water_image_old table
type PublicreportWaterImageOldsQuery = *psql.ViewQuery[*PublicreportWaterImageOld, PublicreportWaterImageOldSlice]

// publicreportWaterImageOldR is where relationships are stored.
type publicreportWaterImageOldR struct {
	Image         *PublicreportImage    // publicreport.water_image_old.pool_image_image_id_fkey
	WaterWaterOld *PublicreportWaterOld // publicreport.water_image_old.pool_image_pool_id_fkey
}

func buildPublicreportWaterImageOldColumns(alias string) publicreportWaterImageOldColumns {
	return publicreportWaterImageOldColumns{
		ColumnsExpr: expr.NewColumnsExpr(
			"image_id", "water_id",
		).WithParent("publicreport.water_image_old"),
		tableAlias: alias,
		ImageID:    psql.Quote(alias, "image_id"),
		WaterID:    psql.Quote(alias, "water_id"),
	}
}

type publicreportWaterImageOldColumns struct {
	expr.ColumnsExpr
	tableAlias string
	ImageID    psql.Expression
	WaterID    psql.Expression
}

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

func (publicreportWaterImageOldColumns) AliasedAs(alias string) publicreportWaterImageOldColumns {
	return buildPublicreportWaterImageOldColumns(alias)
}

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

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

func (s PublicreportWaterImageOldSetter) Overwrite(t *PublicreportWaterImageOld) {
	if s.ImageID.IsValue() {
		t.ImageID = s.ImageID.MustGet()
	}
	if s.WaterID.IsValue() {
		t.WaterID = s.WaterID.MustGet()
	}
}

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

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

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

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

	return exprs
}

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

	return PublicreportWaterImageOlds.Query(
		sm.Where(PublicreportWaterImageOlds.Columns.ImageID.EQ(psql.Arg(ImageIDPK))),
		sm.Where(PublicreportWaterImageOlds.Columns.WaterID.EQ(psql.Arg(WaterIDPK))),
		sm.Columns(PublicreportWaterImageOlds.Columns.Only(cols...)),
	).One(ctx, exec)
}

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

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

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

	return err
}

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

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

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

	return nil
}

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

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

	return err
}

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

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

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

			return err
		}))

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

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

			return err
		}))

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

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

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

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

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

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

	o2, err := PublicreportWaterImageOlds.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 *PublicreportWaterImageOld) Image(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportImagesQuery {
	return PublicreportImages.Query(append(mods,
		sm.Where(PublicreportImages.Columns.ID.EQ(psql.Arg(o.ImageID))),
	)...)
}

func (os PublicreportWaterImageOldSlice) 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)),
	)...)
}

// WaterWaterOld starts a query for related objects on publicreport.water_old
func (o *PublicreportWaterImageOld) WaterWaterOld(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportWaterOldsQuery {
	return PublicreportWaterOlds.Query(append(mods,
		sm.Where(PublicreportWaterOlds.Columns.ID.EQ(psql.Arg(o.WaterID))),
	)...)
}

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

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

func attachPublicreportWaterImageOldImage0(ctx context.Context, exec bob.Executor, count int, publicreportWaterImageOld0 *PublicreportWaterImageOld, publicreportImage1 *PublicreportImage) (*PublicreportWaterImageOld, error) {
	setter := &PublicreportWaterImageOldSetter{
		ImageID: omit.From(publicreportImage1.ID),
	}

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

	return publicreportWaterImageOld0, nil
}

func (publicreportWaterImageOld0 *PublicreportWaterImageOld) 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 = attachPublicreportWaterImageOldImage0(ctx, exec, 1, publicreportWaterImageOld0, publicreportImage1)
	if err != nil {
		return err
	}

	publicreportWaterImageOld0.R.Image = publicreportImage1

	return nil
}

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

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

	publicreportWaterImageOld0.R.Image = publicreportImage1

	return nil
}

func attachPublicreportWaterImageOldWaterWaterOld0(ctx context.Context, exec bob.Executor, count int, publicreportWaterImageOld0 *PublicreportWaterImageOld, publicreportWaterOld1 *PublicreportWaterOld) (*PublicreportWaterImageOld, error) {
	setter := &PublicreportWaterImageOldSetter{
		WaterID: omit.From(publicreportWaterOld1.ID),
	}

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

	return publicreportWaterImageOld0, nil
}

func (publicreportWaterImageOld0 *PublicreportWaterImageOld) InsertWaterWaterOld(ctx context.Context, exec bob.Executor, related *PublicreportWaterOldSetter) error {
	var err error

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

	_, err = attachPublicreportWaterImageOldWaterWaterOld0(ctx, exec, 1, publicreportWaterImageOld0, publicreportWaterOld1)
	if err != nil {
		return err
	}

	publicreportWaterImageOld0.R.WaterWaterOld = publicreportWaterOld1

	return nil
}

func (publicreportWaterImageOld0 *PublicreportWaterImageOld) AttachWaterWaterOld(ctx context.Context, exec bob.Executor, publicreportWaterOld1 *PublicreportWaterOld) error {
	var err error

	_, err = attachPublicreportWaterImageOldWaterWaterOld0(ctx, exec, 1, publicreportWaterImageOld0, publicreportWaterOld1)
	if err != nil {
		return err
	}

	publicreportWaterImageOld0.R.WaterWaterOld = publicreportWaterOld1

	return nil
}

type publicreportWaterImageOldWhere[Q psql.Filterable] struct {
	ImageID psql.WhereMod[Q, int32]
	WaterID psql.WhereMod[Q, int32]
}

func (publicreportWaterImageOldWhere[Q]) AliasedAs(alias string) publicreportWaterImageOldWhere[Q] {
	return buildPublicreportWaterImageOldWhere[Q](buildPublicreportWaterImageOldColumns(alias))
}

func buildPublicreportWaterImageOldWhere[Q psql.Filterable](cols publicreportWaterImageOldColumns) publicreportWaterImageOldWhere[Q] {
	return publicreportWaterImageOldWhere[Q]{
		ImageID: psql.Where[Q, int32](cols.ImageID),
		WaterID: psql.Where[Q, int32](cols.WaterID),
	}
}

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

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

		o.R.Image = rel

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

		o.R.WaterWaterOld = rel

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

type publicreportWaterImageOldPreloader struct {
	Image         func(...psql.PreloadOption) psql.Preloader
	WaterWaterOld func(...psql.PreloadOption) psql.Preloader
}

func buildPublicreportWaterImageOldPreloader() publicreportWaterImageOldPreloader {
	return publicreportWaterImageOldPreloader{
		Image: func(opts ...psql.PreloadOption) psql.Preloader {
			return psql.Preload[*PublicreportImage, PublicreportImageSlice](psql.PreloadRel{
				Name: "Image",
				Sides: []psql.PreloadSide{
					{
						From:        PublicreportWaterImageOlds,
						To:          PublicreportImages,
						FromColumns: []string{"image_id"},
						ToColumns:   []string{"id"},
					},
				},
			}, PublicreportImages.Columns.Names(), opts...)
		},
		WaterWaterOld: func(opts ...psql.PreloadOption) psql.Preloader {
			return psql.Preload[*PublicreportWaterOld, PublicreportWaterOldSlice](psql.PreloadRel{
				Name: "WaterWaterOld",
				Sides: []psql.PreloadSide{
					{
						From:        PublicreportWaterImageOlds,
						To:          PublicreportWaterOlds,
						FromColumns: []string{"water_id"},
						ToColumns:   []string{"id"},
					},
				},
			}, PublicreportWaterOlds.Columns.Names(), opts...)
		},
	}
}

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

func buildPublicreportWaterImageOldThenLoader[Q orm.Loadable]() publicreportWaterImageOldThenLoader[Q] {
	type ImageLoadInterface interface {
		LoadImage(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type WaterWaterOldLoadInterface interface {
		LoadWaterWaterOld(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}

	return publicreportWaterImageOldThenLoader[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...)
			},
		),
		WaterWaterOld: thenLoadBuilder[Q](
			"WaterWaterOld",
			func(ctx context.Context, exec bob.Executor, retrieved WaterWaterOldLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadWaterWaterOld(ctx, exec, mods...)
			},
		),
	}
}

// LoadImage loads the publicreportWaterImageOld's Image into the .R struct
func (o *PublicreportWaterImageOld) 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 publicreportWaterImageOld's Image into the .R struct
func (os PublicreportWaterImageOldSlice) 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
}

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

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

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

	o.R.WaterWaterOld = related
	return nil
}

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

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

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

		for _, rel := range publicreportWaterOlds {

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

			o.R.WaterWaterOld = rel
			break
		}
	}

	return nil
}
