// 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/mods"
	"github.com/Gleipnir-Technology/bob/orm"
	"github.com/Gleipnir-Technology/bob/types/pgtypes"
	"github.com/aarondl/opt/omit"
)

// PublicreportPoolImage is an object representing the database table.
type PublicreportPoolImage struct {
	ImageID int32 `db:"image_id,pk" `
	PoolID  int32 `db:"pool_id,pk" `

	R publicreportPoolImageR `db:"-" `
}

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

// PublicreportPoolImages contains methods to work with the pool_image table
var PublicreportPoolImages = psql.NewTablex[*PublicreportPoolImage, PublicreportPoolImageSlice, *PublicreportPoolImageSetter]("publicreport", "pool_image", buildPublicreportPoolImageColumns("publicreport.pool_image"))

// PublicreportPoolImagesQuery is a query on the pool_image table
type PublicreportPoolImagesQuery = *psql.ViewQuery[*PublicreportPoolImage, PublicreportPoolImageSlice]

// publicreportPoolImageR is where relationships are stored.
type publicreportPoolImageR struct {
	Image *PublicreportImage // publicreport.pool_image.pool_image_image_id_fkey
	Pool  *PublicreportPool  // publicreport.pool_image.pool_image_pool_id_fkey
}

func buildPublicreportPoolImageColumns(alias string) publicreportPoolImageColumns {
	return publicreportPoolImageColumns{
		ColumnsExpr: expr.NewColumnsExpr(
			"image_id", "pool_id",
		).WithParent("publicreport.pool_image"),
		tableAlias: alias,
		ImageID:    psql.Quote(alias, "image_id"),
		PoolID:     psql.Quote(alias, "pool_id"),
	}
}

type publicreportPoolImageColumns struct {
	expr.ColumnsExpr
	tableAlias string
	ImageID    psql.Expression
	PoolID     psql.Expression
}

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

func (publicreportPoolImageColumns) AliasedAs(alias string) publicreportPoolImageColumns {
	return buildPublicreportPoolImageColumns(alias)
}

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

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

func (s PublicreportPoolImageSetter) Overwrite(t *PublicreportPoolImage) {
	if s.ImageID.IsValue() {
		t.ImageID = s.ImageID.MustGet()
	}
	if s.PoolID.IsValue() {
		t.PoolID = s.PoolID.MustGet()
	}
}

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

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

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

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

	return exprs
}

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

	return PublicreportPoolImages.Query(
		sm.Where(PublicreportPoolImages.Columns.ImageID.EQ(psql.Arg(ImageIDPK))),
		sm.Where(PublicreportPoolImages.Columns.PoolID.EQ(psql.Arg(PoolIDPK))),
		sm.Columns(PublicreportPoolImages.Columns.Only(cols...)),
	).One(ctx, exec)
}

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

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

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

	return err
}

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

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

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

	return nil
}

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

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

	return err
}

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

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

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

			return err
		}))

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

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

			return err
		}))

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

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

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

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

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

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

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

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

// Pool starts a query for related objects on publicreport.pool
func (o *PublicreportPoolImage) Pool(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportPoolsQuery {
	return PublicreportPools.Query(append(mods,
		sm.Where(PublicreportPools.Columns.ID.EQ(psql.Arg(o.PoolID))),
	)...)
}

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

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

func attachPublicreportPoolImageImage0(ctx context.Context, exec bob.Executor, count int, publicreportPoolImage0 *PublicreportPoolImage, publicreportImage1 *PublicreportImage) (*PublicreportPoolImage, error) {
	setter := &PublicreportPoolImageSetter{
		ImageID: omit.From(publicreportImage1.ID),
	}

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

	return publicreportPoolImage0, nil
}

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

	publicreportPoolImage0.R.Image = publicreportImage1

	return nil
}

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

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

	publicreportPoolImage0.R.Image = publicreportImage1

	return nil
}

func attachPublicreportPoolImagePool0(ctx context.Context, exec bob.Executor, count int, publicreportPoolImage0 *PublicreportPoolImage, publicreportPool1 *PublicreportPool) (*PublicreportPoolImage, error) {
	setter := &PublicreportPoolImageSetter{
		PoolID: omit.From(publicreportPool1.ID),
	}

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

	return publicreportPoolImage0, nil
}

func (publicreportPoolImage0 *PublicreportPoolImage) InsertPool(ctx context.Context, exec bob.Executor, related *PublicreportPoolSetter) error {
	var err error

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

	_, err = attachPublicreportPoolImagePool0(ctx, exec, 1, publicreportPoolImage0, publicreportPool1)
	if err != nil {
		return err
	}

	publicreportPoolImage0.R.Pool = publicreportPool1

	return nil
}

func (publicreportPoolImage0 *PublicreportPoolImage) AttachPool(ctx context.Context, exec bob.Executor, publicreportPool1 *PublicreportPool) error {
	var err error

	_, err = attachPublicreportPoolImagePool0(ctx, exec, 1, publicreportPoolImage0, publicreportPool1)
	if err != nil {
		return err
	}

	publicreportPoolImage0.R.Pool = publicreportPool1

	return nil
}

type publicreportPoolImageWhere[Q psql.Filterable] struct {
	ImageID psql.WhereMod[Q, int32]
	PoolID  psql.WhereMod[Q, int32]
}

func (publicreportPoolImageWhere[Q]) AliasedAs(alias string) publicreportPoolImageWhere[Q] {
	return buildPublicreportPoolImageWhere[Q](buildPublicreportPoolImageColumns(alias))
}

func buildPublicreportPoolImageWhere[Q psql.Filterable](cols publicreportPoolImageColumns) publicreportPoolImageWhere[Q] {
	return publicreportPoolImageWhere[Q]{
		ImageID: psql.Where[Q, int32](cols.ImageID),
		PoolID:  psql.Where[Q, int32](cols.PoolID),
	}
}

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

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

		o.R.Image = rel

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

		o.R.Pool = rel

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

type publicreportPoolImagePreloader struct {
	Image func(...psql.PreloadOption) psql.Preloader
	Pool  func(...psql.PreloadOption) psql.Preloader
}

func buildPublicreportPoolImagePreloader() publicreportPoolImagePreloader {
	return publicreportPoolImagePreloader{
		Image: func(opts ...psql.PreloadOption) psql.Preloader {
			return psql.Preload[*PublicreportImage, PublicreportImageSlice](psql.PreloadRel{
				Name: "Image",
				Sides: []psql.PreloadSide{
					{
						From:        PublicreportPoolImages,
						To:          PublicreportImages,
						FromColumns: []string{"image_id"},
						ToColumns:   []string{"id"},
					},
				},
			}, PublicreportImages.Columns.Names(), opts...)
		},
		Pool: func(opts ...psql.PreloadOption) psql.Preloader {
			return psql.Preload[*PublicreportPool, PublicreportPoolSlice](psql.PreloadRel{
				Name: "Pool",
				Sides: []psql.PreloadSide{
					{
						From:        PublicreportPoolImages,
						To:          PublicreportPools,
						FromColumns: []string{"pool_id"},
						ToColumns:   []string{"id"},
					},
				},
			}, PublicreportPools.Columns.Names(), opts...)
		},
	}
}

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

func buildPublicreportPoolImageThenLoader[Q orm.Loadable]() publicreportPoolImageThenLoader[Q] {
	type ImageLoadInterface interface {
		LoadImage(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type PoolLoadInterface interface {
		LoadPool(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}

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

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

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

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

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

	o.R.Pool = related
	return nil
}

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

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

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

		for _, rel := range publicreportPools {

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

			o.R.Pool = rel
			break
		}
	}

	return nil
}

type publicreportPoolImageJoins[Q dialect.Joinable] struct {
	typ   string
	Image modAs[Q, publicreportImageColumns]
	Pool  modAs[Q, publicreportPoolColumns]
}

func (j publicreportPoolImageJoins[Q]) aliasedAs(alias string) publicreportPoolImageJoins[Q] {
	return buildPublicreportPoolImageJoins[Q](buildPublicreportPoolImageColumns(alias), j.typ)
}

func buildPublicreportPoolImageJoins[Q dialect.Joinable](cols publicreportPoolImageColumns, typ string) publicreportPoolImageJoins[Q] {
	return publicreportPoolImageJoins[Q]{
		typ: typ,
		Image: modAs[Q, publicreportImageColumns]{
			c: PublicreportImages.Columns,
			f: func(to publicreportImageColumns) bob.Mod[Q] {
				mods := make(mods.QueryMods[Q], 0, 1)

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

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

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

				return mods
			},
		},
	}
}
