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

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

	R publicreportNuisanceImageR `db:"-" `
}

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

// PublicreportNuisanceImages contains methods to work with the nuisance_image table
var PublicreportNuisanceImages = psql.NewTablex[*PublicreportNuisanceImage, PublicreportNuisanceImageSlice, *PublicreportNuisanceImageSetter]("publicreport", "nuisance_image", buildPublicreportNuisanceImageColumns("publicreport.nuisance_image"))

// PublicreportNuisanceImagesQuery is a query on the nuisance_image table
type PublicreportNuisanceImagesQuery = *psql.ViewQuery[*PublicreportNuisanceImage, PublicreportNuisanceImageSlice]

// publicreportNuisanceImageR is where relationships are stored.
type publicreportNuisanceImageR struct {
	Image    *PublicreportImage    // publicreport.nuisance_image.nuisance_image_image_id_fkey
	Nuisance *PublicreportNuisance // publicreport.nuisance_image.nuisance_image_nuisance_id_fkey
}

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

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

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

func (publicreportNuisanceImageColumns) AliasedAs(alias string) publicreportNuisanceImageColumns {
	return buildPublicreportNuisanceImageColumns(alias)
}

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

func (s PublicreportNuisanceImageSetter) 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 PublicreportNuisanceImageSetter) Overwrite(t *PublicreportNuisanceImage) {
	if s.ImageID.IsValue() {
		t.ImageID = s.ImageID.MustGet()
	}
	if s.NuisanceID.IsValue() {
		t.NuisanceID = s.NuisanceID.MustGet()
	}
}

func (s *PublicreportNuisanceImageSetter) Apply(q *dialect.InsertQuery) {
	q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
		return PublicreportNuisanceImages.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 PublicreportNuisanceImageSetter) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
	return um.Set(s.Expressions()...)
}

func (s PublicreportNuisanceImageSetter) 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
}

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

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

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

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

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

	return err
}

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

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

// Reload refreshes the PublicreportNuisanceImage using the executor
func (o *PublicreportNuisanceImage) Reload(ctx context.Context, exec bob.Executor) error {
	o2, err := PublicreportNuisanceImages.Query(
		sm.Where(PublicreportNuisanceImages.Columns.ImageID.EQ(psql.Arg(o.ImageID))),
		sm.Where(PublicreportNuisanceImages.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 PublicreportNuisanceImageSlice is retrieved from the database
func (o PublicreportNuisanceImageSlice) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
	var err error

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

	return err
}

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

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

			return err
		}))

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

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

			return err
		}))

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

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

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

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

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

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

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

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

// Nuisance starts a query for related objects on publicreport.nuisance
func (o *PublicreportNuisanceImage) Nuisance(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportNuisancesQuery {
	return PublicreportNuisances.Query(append(mods,
		sm.Where(PublicreportNuisances.Columns.ID.EQ(psql.Arg(o.NuisanceID))),
	)...)
}

func (os PublicreportNuisanceImageSlice) Nuisance(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportNuisancesQuery {
	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 PublicreportNuisances.Query(append(mods,
		sm.Where(psql.Group(PublicreportNuisances.Columns.ID).OP("IN", PKArgExpr)),
	)...)
}

func attachPublicreportNuisanceImageImage0(ctx context.Context, exec bob.Executor, count int, publicreportNuisanceImage0 *PublicreportNuisanceImage, publicreportImage1 *PublicreportImage) (*PublicreportNuisanceImage, error) {
	setter := &PublicreportNuisanceImageSetter{
		ImageID: omit.From(publicreportImage1.ID),
	}

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

	return publicreportNuisanceImage0, nil
}

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

	publicreportNuisanceImage0.R.Image = publicreportImage1

	return nil
}

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

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

	publicreportNuisanceImage0.R.Image = publicreportImage1

	return nil
}

func attachPublicreportNuisanceImageNuisance0(ctx context.Context, exec bob.Executor, count int, publicreportNuisanceImage0 *PublicreportNuisanceImage, publicreportNuisance1 *PublicreportNuisance) (*PublicreportNuisanceImage, error) {
	setter := &PublicreportNuisanceImageSetter{
		NuisanceID: omit.From(publicreportNuisance1.ID),
	}

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

	return publicreportNuisanceImage0, nil
}

func (publicreportNuisanceImage0 *PublicreportNuisanceImage) InsertNuisance(ctx context.Context, exec bob.Executor, related *PublicreportNuisanceSetter) error {
	var err error

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

	_, err = attachPublicreportNuisanceImageNuisance0(ctx, exec, 1, publicreportNuisanceImage0, publicreportNuisance1)
	if err != nil {
		return err
	}

	publicreportNuisanceImage0.R.Nuisance = publicreportNuisance1

	return nil
}

func (publicreportNuisanceImage0 *PublicreportNuisanceImage) AttachNuisance(ctx context.Context, exec bob.Executor, publicreportNuisance1 *PublicreportNuisance) error {
	var err error

	_, err = attachPublicreportNuisanceImageNuisance0(ctx, exec, 1, publicreportNuisanceImage0, publicreportNuisance1)
	if err != nil {
		return err
	}

	publicreportNuisanceImage0.R.Nuisance = publicreportNuisance1

	return nil
}

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

func (publicreportNuisanceImageWhere[Q]) AliasedAs(alias string) publicreportNuisanceImageWhere[Q] {
	return buildPublicreportNuisanceImageWhere[Q](buildPublicreportNuisanceImageColumns(alias))
}

func buildPublicreportNuisanceImageWhere[Q psql.Filterable](cols publicreportNuisanceImageColumns) publicreportNuisanceImageWhere[Q] {
	return publicreportNuisanceImageWhere[Q]{
		ImageID:    psql.Where[Q, int32](cols.ImageID),
		NuisanceID: psql.Where[Q, int32](cols.NuisanceID),
	}
}

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

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

		o.R.Image = rel

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

		o.R.Nuisance = rel

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

type publicreportNuisanceImagePreloader struct {
	Image    func(...psql.PreloadOption) psql.Preloader
	Nuisance func(...psql.PreloadOption) psql.Preloader
}

func buildPublicreportNuisanceImagePreloader() publicreportNuisanceImagePreloader {
	return publicreportNuisanceImagePreloader{
		Image: func(opts ...psql.PreloadOption) psql.Preloader {
			return psql.Preload[*PublicreportImage, PublicreportImageSlice](psql.PreloadRel{
				Name: "Image",
				Sides: []psql.PreloadSide{
					{
						From:        PublicreportNuisanceImages,
						To:          PublicreportImages,
						FromColumns: []string{"image_id"},
						ToColumns:   []string{"id"},
					},
				},
			}, PublicreportImages.Columns.Names(), opts...)
		},
		Nuisance: func(opts ...psql.PreloadOption) psql.Preloader {
			return psql.Preload[*PublicreportNuisance, PublicreportNuisanceSlice](psql.PreloadRel{
				Name: "Nuisance",
				Sides: []psql.PreloadSide{
					{
						From:        PublicreportNuisanceImages,
						To:          PublicreportNuisances,
						FromColumns: []string{"nuisance_id"},
						ToColumns:   []string{"id"},
					},
				},
			}, PublicreportNuisances.Columns.Names(), opts...)
		},
	}
}

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

func buildPublicreportNuisanceImageThenLoader[Q orm.Loadable]() publicreportNuisanceImageThenLoader[Q] {
	type ImageLoadInterface interface {
		LoadImage(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type NuisanceLoadInterface interface {
		LoadNuisance(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}

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

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

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

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

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

	o.R.Nuisance = related
	return nil
}

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

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

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

		for _, rel := range publicreportNuisances {

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

			o.R.Nuisance = rel
			break
		}
	}

	return nil
}

type publicreportNuisanceImageJoins[Q dialect.Joinable] struct {
	typ      string
	Image    modAs[Q, publicreportImageColumns]
	Nuisance modAs[Q, publicreportNuisanceColumns]
}

func (j publicreportNuisanceImageJoins[Q]) aliasedAs(alias string) publicreportNuisanceImageJoins[Q] {
	return buildPublicreportNuisanceImageJoins[Q](buildPublicreportNuisanceImageColumns(alias), j.typ)
}

func buildPublicreportNuisanceImageJoins[Q dialect.Joinable](cols publicreportNuisanceImageColumns, typ string) publicreportNuisanceImageJoins[Q] {
	return publicreportNuisanceImageJoins[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
			},
		},
		Nuisance: modAs[Q, publicreportNuisanceColumns]{
			c: PublicreportNuisances.Columns,
			f: func(to publicreportNuisanceColumns) bob.Mod[Q] {
				mods := make(mods.QueryMods[Q], 0, 1)

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

				return mods
			},
		},
	}
}
