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

// PublicreportQuickImage is an object representing the database table.
type PublicreportQuickImage struct {
	ImageID int32 `db:"image_id,pk" `
	QuickID int32 `db:"quick_id,pk" `

	R publicreportQuickImageR `db:"-" `
}

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

// PublicreportQuickImages contains methods to work with the quick_image table
var PublicreportQuickImages = psql.NewTablex[*PublicreportQuickImage, PublicreportQuickImageSlice, *PublicreportQuickImageSetter]("publicreport", "quick_image", buildPublicreportQuickImageColumns("publicreport.quick_image"))

// PublicreportQuickImagesQuery is a query on the quick_image table
type PublicreportQuickImagesQuery = *psql.ViewQuery[*PublicreportQuickImage, PublicreportQuickImageSlice]

// publicreportQuickImageR is where relationships are stored.
type publicreportQuickImageR struct {
	Image *PublicreportImage // publicreport.quick_image.quick_image_image_id_fkey
	Quick *PublicreportQuick // publicreport.quick_image.quick_image_quick_id_fkey
}

func buildPublicreportQuickImageColumns(alias string) publicreportQuickImageColumns {
	return publicreportQuickImageColumns{
		ColumnsExpr: expr.NewColumnsExpr(
			"image_id", "quick_id",
		).WithParent("publicreport.quick_image"),
		tableAlias: alias,
		ImageID:    psql.Quote(alias, "image_id"),
		QuickID:    psql.Quote(alias, "quick_id"),
	}
}

type publicreportQuickImageColumns struct {
	expr.ColumnsExpr
	tableAlias string
	ImageID    psql.Expression
	QuickID    psql.Expression
}

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

func (publicreportQuickImageColumns) AliasedAs(alias string) publicreportQuickImageColumns {
	return buildPublicreportQuickImageColumns(alias)
}

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

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

func (s PublicreportQuickImageSetter) Overwrite(t *PublicreportQuickImage) {
	if s.ImageID.IsValue() {
		t.ImageID = s.ImageID.MustGet()
	}
	if s.QuickID.IsValue() {
		t.QuickID = s.QuickID.MustGet()
	}
}

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

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

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

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

	return exprs
}

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

	return PublicreportQuickImages.Query(
		sm.Where(PublicreportQuickImages.Columns.ImageID.EQ(psql.Arg(ImageIDPK))),
		sm.Where(PublicreportQuickImages.Columns.QuickID.EQ(psql.Arg(QuickIDPK))),
		sm.Columns(PublicreportQuickImages.Columns.Only(cols...)),
	).One(ctx, exec)
}

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

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

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

	return err
}

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

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

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

	return nil
}

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

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

	return err
}

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

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

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

			return err
		}))

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

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

			return err
		}))

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

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

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

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

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

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

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

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

// Quick starts a query for related objects on publicreport.quick
func (o *PublicreportQuickImage) Quick(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportQuicksQuery {
	return PublicreportQuicks.Query(append(mods,
		sm.Where(PublicreportQuicks.Columns.ID.EQ(psql.Arg(o.QuickID))),
	)...)
}

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

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

func attachPublicreportQuickImageImage0(ctx context.Context, exec bob.Executor, count int, publicreportQuickImage0 *PublicreportQuickImage, publicreportImage1 *PublicreportImage) (*PublicreportQuickImage, error) {
	setter := &PublicreportQuickImageSetter{
		ImageID: omit.From(publicreportImage1.ID),
	}

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

	return publicreportQuickImage0, nil
}

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

	publicreportQuickImage0.R.Image = publicreportImage1

	return nil
}

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

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

	publicreportQuickImage0.R.Image = publicreportImage1

	return nil
}

func attachPublicreportQuickImageQuick0(ctx context.Context, exec bob.Executor, count int, publicreportQuickImage0 *PublicreportQuickImage, publicreportQuick1 *PublicreportQuick) (*PublicreportQuickImage, error) {
	setter := &PublicreportQuickImageSetter{
		QuickID: omit.From(publicreportQuick1.ID),
	}

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

	return publicreportQuickImage0, nil
}

func (publicreportQuickImage0 *PublicreportQuickImage) InsertQuick(ctx context.Context, exec bob.Executor, related *PublicreportQuickSetter) error {
	var err error

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

	_, err = attachPublicreportQuickImageQuick0(ctx, exec, 1, publicreportQuickImage0, publicreportQuick1)
	if err != nil {
		return err
	}

	publicreportQuickImage0.R.Quick = publicreportQuick1

	return nil
}

func (publicreportQuickImage0 *PublicreportQuickImage) AttachQuick(ctx context.Context, exec bob.Executor, publicreportQuick1 *PublicreportQuick) error {
	var err error

	_, err = attachPublicreportQuickImageQuick0(ctx, exec, 1, publicreportQuickImage0, publicreportQuick1)
	if err != nil {
		return err
	}

	publicreportQuickImage0.R.Quick = publicreportQuick1

	return nil
}

type publicreportQuickImageWhere[Q psql.Filterable] struct {
	ImageID psql.WhereMod[Q, int32]
	QuickID psql.WhereMod[Q, int32]
}

func (publicreportQuickImageWhere[Q]) AliasedAs(alias string) publicreportQuickImageWhere[Q] {
	return buildPublicreportQuickImageWhere[Q](buildPublicreportQuickImageColumns(alias))
}

func buildPublicreportQuickImageWhere[Q psql.Filterable](cols publicreportQuickImageColumns) publicreportQuickImageWhere[Q] {
	return publicreportQuickImageWhere[Q]{
		ImageID: psql.Where[Q, int32](cols.ImageID),
		QuickID: psql.Where[Q, int32](cols.QuickID),
	}
}

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

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

		o.R.Image = rel

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

		o.R.Quick = rel

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

type publicreportQuickImagePreloader struct {
	Image func(...psql.PreloadOption) psql.Preloader
	Quick func(...psql.PreloadOption) psql.Preloader
}

func buildPublicreportQuickImagePreloader() publicreportQuickImagePreloader {
	return publicreportQuickImagePreloader{
		Image: func(opts ...psql.PreloadOption) psql.Preloader {
			return psql.Preload[*PublicreportImage, PublicreportImageSlice](psql.PreloadRel{
				Name: "Image",
				Sides: []psql.PreloadSide{
					{
						From:        PublicreportQuickImages,
						To:          PublicreportImages,
						FromColumns: []string{"image_id"},
						ToColumns:   []string{"id"},
					},
				},
			}, PublicreportImages.Columns.Names(), opts...)
		},
		Quick: func(opts ...psql.PreloadOption) psql.Preloader {
			return psql.Preload[*PublicreportQuick, PublicreportQuickSlice](psql.PreloadRel{
				Name: "Quick",
				Sides: []psql.PreloadSide{
					{
						From:        PublicreportQuickImages,
						To:          PublicreportQuicks,
						FromColumns: []string{"quick_id"},
						ToColumns:   []string{"id"},
					},
				},
			}, PublicreportQuicks.Columns.Names(), opts...)
		},
	}
}

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

func buildPublicreportQuickImageThenLoader[Q orm.Loadable]() publicreportQuickImageThenLoader[Q] {
	type ImageLoadInterface interface {
		LoadImage(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type QuickLoadInterface interface {
		LoadQuick(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}

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

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

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

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

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

	o.R.Quick = related
	return nil
}

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

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

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

		for _, rel := range publicreportQuicks {

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

			o.R.Quick = rel
			break
		}
	}

	return nil
}

type publicreportQuickImageJoins[Q dialect.Joinable] struct {
	typ   string
	Image modAs[Q, publicreportImageColumns]
	Quick modAs[Q, publicreportQuickColumns]
}

func (j publicreportQuickImageJoins[Q]) aliasedAs(alias string) publicreportQuickImageJoins[Q] {
	return buildPublicreportQuickImageJoins[Q](buildPublicreportQuickImageColumns(alias), j.typ)
}

func buildPublicreportQuickImageJoins[Q dialect.Joinable](cols publicreportQuickImageColumns, typ string) publicreportQuickImageJoins[Q] {
	return publicreportQuickImageJoins[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
			},
		},
		Quick: modAs[Q, publicreportQuickColumns]{
			c: PublicreportQuicks.Columns,
			f: func(to publicreportQuickColumns) bob.Mod[Q] {
				mods := make(mods.QueryMods[Q], 0, 1)

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

				return mods
			},
		},
	}
}
