// 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"
	"strconv"
	"time"

	"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/null"
	"github.com/aarondl/opt/omit"
	"github.com/aarondl/opt/omitnull"
	"github.com/google/uuid"
	"github.com/stephenafamo/scan"
)

// PublicreportImage is an object representing the database table.
type PublicreportImage struct {
	ID               int32            `db:"id,pk" `
	ContentType      string           `db:"content_type" `
	Created          time.Time        `db:"created" `
	Location         null.Val[string] `db:"location" `
	ResolutionX      int32            `db:"resolution_x" `
	ResolutionY      int32            `db:"resolution_y" `
	StorageUUID      uuid.UUID        `db:"storage_uuid" `
	StorageSize      int64            `db:"storage_size" `
	UploadedFilename string           `db:"uploaded_filename" `

	R publicreportImageR `db:"-" `

	C publicreportImageC `db:"-" `
}

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

// PublicreportImages contains methods to work with the image table
var PublicreportImages = psql.NewTablex[*PublicreportImage, PublicreportImageSlice, *PublicreportImageSetter]("publicreport", "image", buildPublicreportImageColumns("publicreport.image"))

// PublicreportImagesQuery is a query on the image table
type PublicreportImagesQuery = *psql.ViewQuery[*PublicreportImage, PublicreportImageSlice]

// publicreportImageR is where relationships are stored.
type publicreportImageR struct {
	ImageExifs PublicreportImageExifSlice // publicreport.image_exif.image_exif_image_id_fkey
	Nuisances  PublicreportNuisanceSlice  // publicreport.nuisance_image.nuisance_image_image_id_fkeypublicreport.nuisance_image.nuisance_image_nuisance_id_fkey
	Pools      PublicreportPoolSlice      // publicreport.pool_image.pool_image_image_id_fkeypublicreport.pool_image.pool_image_pool_id_fkey
	Quicks     PublicreportQuickSlice     // publicreport.quick_image.quick_image_image_id_fkeypublicreport.quick_image.quick_image_quick_id_fkey
}

func buildPublicreportImageColumns(alias string) publicreportImageColumns {
	return publicreportImageColumns{
		ColumnsExpr: expr.NewColumnsExpr(
			"id", "content_type", "created", "location", "resolution_x", "resolution_y", "storage_uuid", "storage_size", "uploaded_filename",
		).WithParent("publicreport.image"),
		tableAlias:       alias,
		ID:               psql.Quote(alias, "id"),
		ContentType:      psql.Quote(alias, "content_type"),
		Created:          psql.Quote(alias, "created"),
		Location:         psql.Quote(alias, "location"),
		ResolutionX:      psql.Quote(alias, "resolution_x"),
		ResolutionY:      psql.Quote(alias, "resolution_y"),
		StorageUUID:      psql.Quote(alias, "storage_uuid"),
		StorageSize:      psql.Quote(alias, "storage_size"),
		UploadedFilename: psql.Quote(alias, "uploaded_filename"),
	}
}

type publicreportImageColumns struct {
	expr.ColumnsExpr
	tableAlias       string
	ID               psql.Expression
	ContentType      psql.Expression
	Created          psql.Expression
	Location         psql.Expression
	ResolutionX      psql.Expression
	ResolutionY      psql.Expression
	StorageUUID      psql.Expression
	StorageSize      psql.Expression
	UploadedFilename psql.Expression
}

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

func (publicreportImageColumns) AliasedAs(alias string) publicreportImageColumns {
	return buildPublicreportImageColumns(alias)
}

// PublicreportImageSetter is used for insert/upsert/update operations
// All values are optional, and do not have to be set
// Generated columns are not included
type PublicreportImageSetter struct {
	ID               omit.Val[int32]      `db:"id,pk" `
	ContentType      omit.Val[string]     `db:"content_type" `
	Created          omit.Val[time.Time]  `db:"created" `
	Location         omitnull.Val[string] `db:"location" `
	ResolutionX      omit.Val[int32]      `db:"resolution_x" `
	ResolutionY      omit.Val[int32]      `db:"resolution_y" `
	StorageUUID      omit.Val[uuid.UUID]  `db:"storage_uuid" `
	StorageSize      omit.Val[int64]      `db:"storage_size" `
	UploadedFilename omit.Val[string]     `db:"uploaded_filename" `
}

func (s PublicreportImageSetter) SetColumns() []string {
	vals := make([]string, 0, 9)
	if s.ID.IsValue() {
		vals = append(vals, "id")
	}
	if s.ContentType.IsValue() {
		vals = append(vals, "content_type")
	}
	if s.Created.IsValue() {
		vals = append(vals, "created")
	}
	if !s.Location.IsUnset() {
		vals = append(vals, "location")
	}
	if s.ResolutionX.IsValue() {
		vals = append(vals, "resolution_x")
	}
	if s.ResolutionY.IsValue() {
		vals = append(vals, "resolution_y")
	}
	if s.StorageUUID.IsValue() {
		vals = append(vals, "storage_uuid")
	}
	if s.StorageSize.IsValue() {
		vals = append(vals, "storage_size")
	}
	if s.UploadedFilename.IsValue() {
		vals = append(vals, "uploaded_filename")
	}
	return vals
}

func (s PublicreportImageSetter) Overwrite(t *PublicreportImage) {
	if s.ID.IsValue() {
		t.ID = s.ID.MustGet()
	}
	if s.ContentType.IsValue() {
		t.ContentType = s.ContentType.MustGet()
	}
	if s.Created.IsValue() {
		t.Created = s.Created.MustGet()
	}
	if !s.Location.IsUnset() {
		t.Location = s.Location.MustGetNull()
	}
	if s.ResolutionX.IsValue() {
		t.ResolutionX = s.ResolutionX.MustGet()
	}
	if s.ResolutionY.IsValue() {
		t.ResolutionY = s.ResolutionY.MustGet()
	}
	if s.StorageUUID.IsValue() {
		t.StorageUUID = s.StorageUUID.MustGet()
	}
	if s.StorageSize.IsValue() {
		t.StorageSize = s.StorageSize.MustGet()
	}
	if s.UploadedFilename.IsValue() {
		t.UploadedFilename = s.UploadedFilename.MustGet()
	}
}

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

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

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

		if !s.Location.IsUnset() {
			vals[3] = psql.Arg(s.Location.MustGetNull())
		} else {
			vals[3] = psql.Raw("DEFAULT")
		}

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

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

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

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

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

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

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

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

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

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

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

	if !s.Location.IsUnset() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			psql.Quote(append(prefix, "location")...),
			psql.Arg(s.Location),
		}})
	}

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

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

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

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

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

	return exprs
}

// FindPublicreportImage retrieves a single record by primary key
// If cols is empty Find will return all columns.
func FindPublicreportImage(ctx context.Context, exec bob.Executor, IDPK int32, cols ...string) (*PublicreportImage, error) {
	if len(cols) == 0 {
		return PublicreportImages.Query(
			sm.Where(PublicreportImages.Columns.ID.EQ(psql.Arg(IDPK))),
		).One(ctx, exec)
	}

	return PublicreportImages.Query(
		sm.Where(PublicreportImages.Columns.ID.EQ(psql.Arg(IDPK))),
		sm.Columns(PublicreportImages.Columns.Only(cols...)),
	).One(ctx, exec)
}

// PublicreportImageExists checks the presence of a single record by primary key
func PublicreportImageExists(ctx context.Context, exec bob.Executor, IDPK int32) (bool, error) {
	return PublicreportImages.Query(
		sm.Where(PublicreportImages.Columns.ID.EQ(psql.Arg(IDPK))),
	).Exists(ctx, exec)
}

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

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

	return err
}

// primaryKeyVals returns the primary key values of the PublicreportImage
func (o *PublicreportImage) primaryKeyVals() bob.Expression {
	return psql.Arg(o.ID)
}

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

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

	return nil
}

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

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

	return err
}

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

	return psql.Quote("publicreport.image", "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 PublicreportImageSlice) copyMatchingRows(from ...*PublicreportImage) {
	for i, old := range o {
		for _, new := range from {
			if new.ID != old.ID {
				continue
			}
			new.R = old.R
			o[i] = new
			break
		}
	}
}

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

			return err
		}))

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

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

			return err
		}))

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

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

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

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

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

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

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

	o.copyMatchingRows(o2...)

	return nil
}

// ImageExifs starts a query for related objects on publicreport.image_exif
func (o *PublicreportImage) ImageExifs(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportImageExifsQuery {
	return PublicreportImageExifs.Query(append(mods,
		sm.Where(PublicreportImageExifs.Columns.ImageID.EQ(psql.Arg(o.ID))),
	)...)
}

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

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

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

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

	return PublicreportNuisances.Query(append(mods,
		sm.InnerJoin(PublicreportNuisanceImages.NameAs()).On(
			PublicreportNuisances.Columns.ID.EQ(PublicreportNuisanceImages.Columns.NuisanceID),
		),
		sm.Where(psql.Group(PublicreportNuisanceImages.Columns.ImageID).OP("IN", PKArgExpr)),
	)...)
}

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

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

	return PublicreportPools.Query(append(mods,
		sm.InnerJoin(PublicreportPoolImages.NameAs()).On(
			PublicreportPools.Columns.ID.EQ(PublicreportPoolImages.Columns.PoolID),
		),
		sm.Where(psql.Group(PublicreportPoolImages.Columns.ImageID).OP("IN", PKArgExpr)),
	)...)
}

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

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

	return PublicreportQuicks.Query(append(mods,
		sm.InnerJoin(PublicreportQuickImages.NameAs()).On(
			PublicreportQuicks.Columns.ID.EQ(PublicreportQuickImages.Columns.QuickID),
		),
		sm.Where(psql.Group(PublicreportQuickImages.Columns.ImageID).OP("IN", PKArgExpr)),
	)...)
}

func insertPublicreportImageImageExifs0(ctx context.Context, exec bob.Executor, publicreportImageExifs1 []*PublicreportImageExifSetter, publicreportImage0 *PublicreportImage) (PublicreportImageExifSlice, error) {
	for i := range publicreportImageExifs1 {
		publicreportImageExifs1[i].ImageID = omit.From(publicreportImage0.ID)
	}

	ret, err := PublicreportImageExifs.Insert(bob.ToMods(publicreportImageExifs1...)).All(ctx, exec)
	if err != nil {
		return ret, fmt.Errorf("insertPublicreportImageImageExifs0: %w", err)
	}

	return ret, nil
}

func attachPublicreportImageImageExifs0(ctx context.Context, exec bob.Executor, count int, publicreportImageExifs1 PublicreportImageExifSlice, publicreportImage0 *PublicreportImage) (PublicreportImageExifSlice, error) {
	setter := &PublicreportImageExifSetter{
		ImageID: omit.From(publicreportImage0.ID),
	}

	err := publicreportImageExifs1.UpdateAll(ctx, exec, *setter)
	if err != nil {
		return nil, fmt.Errorf("attachPublicreportImageImageExifs0: %w", err)
	}

	return publicreportImageExifs1, nil
}

func (publicreportImage0 *PublicreportImage) InsertImageExifs(ctx context.Context, exec bob.Executor, related ...*PublicreportImageExifSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	publicreportImageExifs1, err := insertPublicreportImageImageExifs0(ctx, exec, related, publicreportImage0)
	if err != nil {
		return err
	}

	publicreportImage0.R.ImageExifs = append(publicreportImage0.R.ImageExifs, publicreportImageExifs1...)

	for _, rel := range publicreportImageExifs1 {
		rel.R.Image = publicreportImage0
	}
	return nil
}

func (publicreportImage0 *PublicreportImage) AttachImageExifs(ctx context.Context, exec bob.Executor, related ...*PublicreportImageExif) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	publicreportImageExifs1 := PublicreportImageExifSlice(related)

	_, err = attachPublicreportImageImageExifs0(ctx, exec, len(related), publicreportImageExifs1, publicreportImage0)
	if err != nil {
		return err
	}

	publicreportImage0.R.ImageExifs = append(publicreportImage0.R.ImageExifs, publicreportImageExifs1...)

	for _, rel := range related {
		rel.R.Image = publicreportImage0
	}

	return nil
}

func attachPublicreportImageNuisances0(ctx context.Context, exec bob.Executor, count int, publicreportImage0 *PublicreportImage, publicreportNuisances2 PublicreportNuisanceSlice) (PublicreportNuisanceImageSlice, error) {
	setters := make([]*PublicreportNuisanceImageSetter, count)
	for i := range count {
		setters[i] = &PublicreportNuisanceImageSetter{
			ImageID:    omit.From(publicreportImage0.ID),
			NuisanceID: omit.From(publicreportNuisances2[i].ID),
		}
	}

	publicreportNuisanceImages1, err := PublicreportNuisanceImages.Insert(bob.ToMods(setters...)).All(ctx, exec)
	if err != nil {
		return nil, fmt.Errorf("attachPublicreportImageNuisances0: %w", err)
	}

	return publicreportNuisanceImages1, nil
}

func (publicreportImage0 *PublicreportImage) InsertNuisances(ctx context.Context, exec bob.Executor, related ...*PublicreportNuisanceSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	inserted, err := PublicreportNuisances.Insert(bob.ToMods(related...)).All(ctx, exec)
	if err != nil {
		return fmt.Errorf("inserting related objects: %w", err)
	}
	publicreportNuisances2 := PublicreportNuisanceSlice(inserted)

	_, err = attachPublicreportImageNuisances0(ctx, exec, len(related), publicreportImage0, publicreportNuisances2)
	if err != nil {
		return err
	}

	publicreportImage0.R.Nuisances = append(publicreportImage0.R.Nuisances, publicreportNuisances2...)

	for _, rel := range publicreportNuisances2 {
		rel.R.Images = append(rel.R.Images, publicreportImage0)
	}
	return nil
}

func (publicreportImage0 *PublicreportImage) AttachNuisances(ctx context.Context, exec bob.Executor, related ...*PublicreportNuisance) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	publicreportNuisances2 := PublicreportNuisanceSlice(related)

	_, err = attachPublicreportImageNuisances0(ctx, exec, len(related), publicreportImage0, publicreportNuisances2)
	if err != nil {
		return err
	}

	publicreportImage0.R.Nuisances = append(publicreportImage0.R.Nuisances, publicreportNuisances2...)

	for _, rel := range related {
		rel.R.Images = append(rel.R.Images, publicreportImage0)
	}

	return nil
}

func attachPublicreportImagePools0(ctx context.Context, exec bob.Executor, count int, publicreportImage0 *PublicreportImage, publicreportPools2 PublicreportPoolSlice) (PublicreportPoolImageSlice, error) {
	setters := make([]*PublicreportPoolImageSetter, count)
	for i := range count {
		setters[i] = &PublicreportPoolImageSetter{
			ImageID: omit.From(publicreportImage0.ID),
			PoolID:  omit.From(publicreportPools2[i].ID),
		}
	}

	publicreportPoolImages1, err := PublicreportPoolImages.Insert(bob.ToMods(setters...)).All(ctx, exec)
	if err != nil {
		return nil, fmt.Errorf("attachPublicreportImagePools0: %w", err)
	}

	return publicreportPoolImages1, nil
}

func (publicreportImage0 *PublicreportImage) InsertPools(ctx context.Context, exec bob.Executor, related ...*PublicreportPoolSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	inserted, err := PublicreportPools.Insert(bob.ToMods(related...)).All(ctx, exec)
	if err != nil {
		return fmt.Errorf("inserting related objects: %w", err)
	}
	publicreportPools2 := PublicreportPoolSlice(inserted)

	_, err = attachPublicreportImagePools0(ctx, exec, len(related), publicreportImage0, publicreportPools2)
	if err != nil {
		return err
	}

	publicreportImage0.R.Pools = append(publicreportImage0.R.Pools, publicreportPools2...)

	for _, rel := range publicreportPools2 {
		rel.R.Images = append(rel.R.Images, publicreportImage0)
	}
	return nil
}

func (publicreportImage0 *PublicreportImage) AttachPools(ctx context.Context, exec bob.Executor, related ...*PublicreportPool) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	publicreportPools2 := PublicreportPoolSlice(related)

	_, err = attachPublicreportImagePools0(ctx, exec, len(related), publicreportImage0, publicreportPools2)
	if err != nil {
		return err
	}

	publicreportImage0.R.Pools = append(publicreportImage0.R.Pools, publicreportPools2...)

	for _, rel := range related {
		rel.R.Images = append(rel.R.Images, publicreportImage0)
	}

	return nil
}

func attachPublicreportImageQuicks0(ctx context.Context, exec bob.Executor, count int, publicreportImage0 *PublicreportImage, publicreportQuicks2 PublicreportQuickSlice) (PublicreportQuickImageSlice, error) {
	setters := make([]*PublicreportQuickImageSetter, count)
	for i := range count {
		setters[i] = &PublicreportQuickImageSetter{
			ImageID: omit.From(publicreportImage0.ID),
			QuickID: omit.From(publicreportQuicks2[i].ID),
		}
	}

	publicreportQuickImages1, err := PublicreportQuickImages.Insert(bob.ToMods(setters...)).All(ctx, exec)
	if err != nil {
		return nil, fmt.Errorf("attachPublicreportImageQuicks0: %w", err)
	}

	return publicreportQuickImages1, nil
}

func (publicreportImage0 *PublicreportImage) InsertQuicks(ctx context.Context, exec bob.Executor, related ...*PublicreportQuickSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	inserted, err := PublicreportQuicks.Insert(bob.ToMods(related...)).All(ctx, exec)
	if err != nil {
		return fmt.Errorf("inserting related objects: %w", err)
	}
	publicreportQuicks2 := PublicreportQuickSlice(inserted)

	_, err = attachPublicreportImageQuicks0(ctx, exec, len(related), publicreportImage0, publicreportQuicks2)
	if err != nil {
		return err
	}

	publicreportImage0.R.Quicks = append(publicreportImage0.R.Quicks, publicreportQuicks2...)

	for _, rel := range publicreportQuicks2 {
		rel.R.Images = append(rel.R.Images, publicreportImage0)
	}
	return nil
}

func (publicreportImage0 *PublicreportImage) AttachQuicks(ctx context.Context, exec bob.Executor, related ...*PublicreportQuick) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	publicreportQuicks2 := PublicreportQuickSlice(related)

	_, err = attachPublicreportImageQuicks0(ctx, exec, len(related), publicreportImage0, publicreportQuicks2)
	if err != nil {
		return err
	}

	publicreportImage0.R.Quicks = append(publicreportImage0.R.Quicks, publicreportQuicks2...)

	for _, rel := range related {
		rel.R.Images = append(rel.R.Images, publicreportImage0)
	}

	return nil
}

type publicreportImageWhere[Q psql.Filterable] struct {
	ID               psql.WhereMod[Q, int32]
	ContentType      psql.WhereMod[Q, string]
	Created          psql.WhereMod[Q, time.Time]
	Location         psql.WhereNullMod[Q, string]
	ResolutionX      psql.WhereMod[Q, int32]
	ResolutionY      psql.WhereMod[Q, int32]
	StorageUUID      psql.WhereMod[Q, uuid.UUID]
	StorageSize      psql.WhereMod[Q, int64]
	UploadedFilename psql.WhereMod[Q, string]
}

func (publicreportImageWhere[Q]) AliasedAs(alias string) publicreportImageWhere[Q] {
	return buildPublicreportImageWhere[Q](buildPublicreportImageColumns(alias))
}

func buildPublicreportImageWhere[Q psql.Filterable](cols publicreportImageColumns) publicreportImageWhere[Q] {
	return publicreportImageWhere[Q]{
		ID:               psql.Where[Q, int32](cols.ID),
		ContentType:      psql.Where[Q, string](cols.ContentType),
		Created:          psql.Where[Q, time.Time](cols.Created),
		Location:         psql.WhereNull[Q, string](cols.Location),
		ResolutionX:      psql.Where[Q, int32](cols.ResolutionX),
		ResolutionY:      psql.Where[Q, int32](cols.ResolutionY),
		StorageUUID:      psql.Where[Q, uuid.UUID](cols.StorageUUID),
		StorageSize:      psql.Where[Q, int64](cols.StorageSize),
		UploadedFilename: psql.Where[Q, string](cols.UploadedFilename),
	}
}

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

	switch name {
	case "ImageExifs":
		rels, ok := retrieved.(PublicreportImageExifSlice)
		if !ok {
			return fmt.Errorf("publicreportImage cannot load %T as %q", retrieved, name)
		}

		o.R.ImageExifs = rels

		for _, rel := range rels {
			if rel != nil {
				rel.R.Image = o
			}
		}
		return nil
	case "Nuisances":
		rels, ok := retrieved.(PublicreportNuisanceSlice)
		if !ok {
			return fmt.Errorf("publicreportImage cannot load %T as %q", retrieved, name)
		}

		o.R.Nuisances = rels

		for _, rel := range rels {
			if rel != nil {
				rel.R.Images = PublicreportImageSlice{o}
			}
		}
		return nil
	case "Pools":
		rels, ok := retrieved.(PublicreportPoolSlice)
		if !ok {
			return fmt.Errorf("publicreportImage cannot load %T as %q", retrieved, name)
		}

		o.R.Pools = rels

		for _, rel := range rels {
			if rel != nil {
				rel.R.Images = PublicreportImageSlice{o}
			}
		}
		return nil
	case "Quicks":
		rels, ok := retrieved.(PublicreportQuickSlice)
		if !ok {
			return fmt.Errorf("publicreportImage cannot load %T as %q", retrieved, name)
		}

		o.R.Quicks = rels

		for _, rel := range rels {
			if rel != nil {
				rel.R.Images = PublicreportImageSlice{o}
			}
		}
		return nil
	default:
		return fmt.Errorf("publicreportImage has no relationship %q", name)
	}
}

type publicreportImagePreloader struct{}

func buildPublicreportImagePreloader() publicreportImagePreloader {
	return publicreportImagePreloader{}
}

type publicreportImageThenLoader[Q orm.Loadable] struct {
	ImageExifs func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	Nuisances  func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	Pools      func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	Quicks     func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
}

func buildPublicreportImageThenLoader[Q orm.Loadable]() publicreportImageThenLoader[Q] {
	type ImageExifsLoadInterface interface {
		LoadImageExifs(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type NuisancesLoadInterface interface {
		LoadNuisances(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type PoolsLoadInterface interface {
		LoadPools(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type QuicksLoadInterface interface {
		LoadQuicks(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}

	return publicreportImageThenLoader[Q]{
		ImageExifs: thenLoadBuilder[Q](
			"ImageExifs",
			func(ctx context.Context, exec bob.Executor, retrieved ImageExifsLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadImageExifs(ctx, exec, mods...)
			},
		),
		Nuisances: thenLoadBuilder[Q](
			"Nuisances",
			func(ctx context.Context, exec bob.Executor, retrieved NuisancesLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadNuisances(ctx, exec, mods...)
			},
		),
		Pools: thenLoadBuilder[Q](
			"Pools",
			func(ctx context.Context, exec bob.Executor, retrieved PoolsLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadPools(ctx, exec, mods...)
			},
		),
		Quicks: thenLoadBuilder[Q](
			"Quicks",
			func(ctx context.Context, exec bob.Executor, retrieved QuicksLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadQuicks(ctx, exec, mods...)
			},
		),
	}
}

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

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

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

	for _, rel := range related {
		rel.R.Image = o
	}

	o.R.ImageExifs = related
	return nil
}

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

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

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

		o.R.ImageExifs = nil
	}

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

		for _, rel := range publicreportImageExifs {

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

			rel.R.Image = o

			o.R.ImageExifs = append(o.R.ImageExifs, rel)
		}
	}

	return nil
}

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

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

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

	for _, rel := range related {
		rel.R.Images = PublicreportImageSlice{o}
	}

	o.R.Nuisances = related
	return nil
}

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

	// since we are changing the columns, we need to check if the original columns were set or add the defaults
	sq := dialect.SelectQuery{}
	for _, mod := range mods {
		mod.Apply(&sq)
	}

	if len(sq.SelectList.Columns) == 0 {
		mods = append(mods, sm.Columns(PublicreportNuisances.Columns))
	}

	q := os.Nuisances(append(
		mods,
		sm.Columns(PublicreportNuisanceImages.Columns.ImageID.As("related_publicreport.image.ID")),
	)...)

	IDSlice := []int32{}

	mapper := scan.Mod(scan.StructMapper[*PublicreportNuisance](), func(ctx context.Context, cols []string) (scan.BeforeFunc, func(any, any) error) {
		return func(row *scan.Row) (any, error) {
				IDSlice = append(IDSlice, *new(int32))
				row.ScheduleScanByName("related_publicreport.image.ID", &IDSlice[len(IDSlice)-1])

				return nil, nil
			},
			func(any, any) error {
				return nil
			}
	})

	publicreportNuisances, err := bob.Allx[bob.SliceTransformer[*PublicreportNuisance, PublicreportNuisanceSlice]](ctx, exec, q, mapper)
	if err != nil {
		return err
	}

	for _, o := range os {
		o.R.Nuisances = nil
	}

	for _, o := range os {
		for i, rel := range publicreportNuisances {
			if !(o.ID == IDSlice[i]) {
				continue
			}

			rel.R.Images = append(rel.R.Images, o)

			o.R.Nuisances = append(o.R.Nuisances, rel)
		}
	}

	return nil
}

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

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

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

	for _, rel := range related {
		rel.R.Images = PublicreportImageSlice{o}
	}

	o.R.Pools = related
	return nil
}

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

	// since we are changing the columns, we need to check if the original columns were set or add the defaults
	sq := dialect.SelectQuery{}
	for _, mod := range mods {
		mod.Apply(&sq)
	}

	if len(sq.SelectList.Columns) == 0 {
		mods = append(mods, sm.Columns(PublicreportPools.Columns))
	}

	q := os.Pools(append(
		mods,
		sm.Columns(PublicreportPoolImages.Columns.ImageID.As("related_publicreport.image.ID")),
	)...)

	IDSlice := []int32{}

	mapper := scan.Mod(scan.StructMapper[*PublicreportPool](), func(ctx context.Context, cols []string) (scan.BeforeFunc, func(any, any) error) {
		return func(row *scan.Row) (any, error) {
				IDSlice = append(IDSlice, *new(int32))
				row.ScheduleScanByName("related_publicreport.image.ID", &IDSlice[len(IDSlice)-1])

				return nil, nil
			},
			func(any, any) error {
				return nil
			}
	})

	publicreportPools, err := bob.Allx[bob.SliceTransformer[*PublicreportPool, PublicreportPoolSlice]](ctx, exec, q, mapper)
	if err != nil {
		return err
	}

	for _, o := range os {
		o.R.Pools = nil
	}

	for _, o := range os {
		for i, rel := range publicreportPools {
			if !(o.ID == IDSlice[i]) {
				continue
			}

			rel.R.Images = append(rel.R.Images, o)

			o.R.Pools = append(o.R.Pools, rel)
		}
	}

	return nil
}

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

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

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

	for _, rel := range related {
		rel.R.Images = PublicreportImageSlice{o}
	}

	o.R.Quicks = related
	return nil
}

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

	// since we are changing the columns, we need to check if the original columns were set or add the defaults
	sq := dialect.SelectQuery{}
	for _, mod := range mods {
		mod.Apply(&sq)
	}

	if len(sq.SelectList.Columns) == 0 {
		mods = append(mods, sm.Columns(PublicreportQuicks.Columns))
	}

	q := os.Quicks(append(
		mods,
		sm.Columns(PublicreportQuickImages.Columns.ImageID.As("related_publicreport.image.ID")),
	)...)

	IDSlice := []int32{}

	mapper := scan.Mod(scan.StructMapper[*PublicreportQuick](), func(ctx context.Context, cols []string) (scan.BeforeFunc, func(any, any) error) {
		return func(row *scan.Row) (any, error) {
				IDSlice = append(IDSlice, *new(int32))
				row.ScheduleScanByName("related_publicreport.image.ID", &IDSlice[len(IDSlice)-1])

				return nil, nil
			},
			func(any, any) error {
				return nil
			}
	})

	publicreportQuicks, err := bob.Allx[bob.SliceTransformer[*PublicreportQuick, PublicreportQuickSlice]](ctx, exec, q, mapper)
	if err != nil {
		return err
	}

	for _, o := range os {
		o.R.Quicks = nil
	}

	for _, o := range os {
		for i, rel := range publicreportQuicks {
			if !(o.ID == IDSlice[i]) {
				continue
			}

			rel.R.Images = append(rel.R.Images, o)

			o.R.Quicks = append(o.R.Quicks, rel)
		}
	}

	return nil
}

// publicreportImageC is where relationship counts are stored.
type publicreportImageC struct {
	ImageExifs *int64
	Nuisances  *int64
	Pools      *int64
	Quicks     *int64
}

// PreloadCount sets a count in the C struct by name
func (o *PublicreportImage) PreloadCount(name string, count int64) error {
	if o == nil {
		return nil
	}

	switch name {
	case "ImageExifs":
		o.C.ImageExifs = &count
	case "Nuisances":
		o.C.Nuisances = &count
	case "Pools":
		o.C.Pools = &count
	case "Quicks":
		o.C.Quicks = &count
	}
	return nil
}

type publicreportImageCountPreloader struct {
	ImageExifs func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader
	Nuisances  func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader
	Pools      func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader
	Quicks     func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader
}

func buildPublicreportImageCountPreloader() publicreportImageCountPreloader {
	return publicreportImageCountPreloader{
		ImageExifs: func(mods ...bob.Mod[*dialect.SelectQuery]) psql.Preloader {
			return countPreloader[*PublicreportImage]("ImageExifs", func(parent string) bob.Expression {
				// Build a correlated subquery: (SELECT COUNT(*) FROM related WHERE fk = parent.pk)
				if parent == "" {
					parent = PublicreportImages.Alias()
				}

				subqueryMods := []bob.Mod[*dialect.SelectQuery]{
					sm.Columns(psql.Raw("count(*)")),

					sm.From(PublicreportImageExifs.Name()),
					sm.Where(psql.Quote(PublicreportImageExifs.Alias(), "image_id").EQ(psql.Quote(parent, "id"))),
				}
				subqueryMods = append(subqueryMods, mods...)
				return psql.Group(psql.Select(subqueryMods...).Expression)
			})
		},
		Nuisances: func(mods ...bob.Mod[*dialect.SelectQuery]) psql.Preloader {
			return countPreloader[*PublicreportImage]("Nuisances", func(parent string) bob.Expression {
				// Build a correlated subquery: (SELECT COUNT(*) FROM related WHERE fk = parent.pk)
				if parent == "" {
					parent = PublicreportImages.Alias()
				}

				subqueryMods := []bob.Mod[*dialect.SelectQuery]{
					sm.Columns(psql.Raw("count(*)")),

					sm.From(PublicreportNuisanceImages.Name()),
					sm.Where(psql.Quote(PublicreportNuisanceImages.Alias(), "image_id").EQ(psql.Quote(parent, "id"))),
					sm.InnerJoin(PublicreportNuisances.Name()).On(
						psql.Quote(PublicreportNuisances.Alias(), "id").EQ(psql.Quote(PublicreportNuisanceImages.Alias(), "nuisance_id")),
					),
				}
				subqueryMods = append(subqueryMods, mods...)
				return psql.Group(psql.Select(subqueryMods...).Expression)
			})
		},
		Pools: func(mods ...bob.Mod[*dialect.SelectQuery]) psql.Preloader {
			return countPreloader[*PublicreportImage]("Pools", func(parent string) bob.Expression {
				// Build a correlated subquery: (SELECT COUNT(*) FROM related WHERE fk = parent.pk)
				if parent == "" {
					parent = PublicreportImages.Alias()
				}

				subqueryMods := []bob.Mod[*dialect.SelectQuery]{
					sm.Columns(psql.Raw("count(*)")),

					sm.From(PublicreportPoolImages.Name()),
					sm.Where(psql.Quote(PublicreportPoolImages.Alias(), "image_id").EQ(psql.Quote(parent, "id"))),
					sm.InnerJoin(PublicreportPools.Name()).On(
						psql.Quote(PublicreportPools.Alias(), "id").EQ(psql.Quote(PublicreportPoolImages.Alias(), "pool_id")),
					),
				}
				subqueryMods = append(subqueryMods, mods...)
				return psql.Group(psql.Select(subqueryMods...).Expression)
			})
		},
		Quicks: func(mods ...bob.Mod[*dialect.SelectQuery]) psql.Preloader {
			return countPreloader[*PublicreportImage]("Quicks", func(parent string) bob.Expression {
				// Build a correlated subquery: (SELECT COUNT(*) FROM related WHERE fk = parent.pk)
				if parent == "" {
					parent = PublicreportImages.Alias()
				}

				subqueryMods := []bob.Mod[*dialect.SelectQuery]{
					sm.Columns(psql.Raw("count(*)")),

					sm.From(PublicreportQuickImages.Name()),
					sm.Where(psql.Quote(PublicreportQuickImages.Alias(), "image_id").EQ(psql.Quote(parent, "id"))),
					sm.InnerJoin(PublicreportQuicks.Name()).On(
						psql.Quote(PublicreportQuicks.Alias(), "id").EQ(psql.Quote(PublicreportQuickImages.Alias(), "quick_id")),
					),
				}
				subqueryMods = append(subqueryMods, mods...)
				return psql.Group(psql.Select(subqueryMods...).Expression)
			})
		},
	}
}

type publicreportImageCountThenLoader[Q orm.Loadable] struct {
	ImageExifs func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	Nuisances  func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	Pools      func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	Quicks     func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
}

func buildPublicreportImageCountThenLoader[Q orm.Loadable]() publicreportImageCountThenLoader[Q] {
	type ImageExifsCountInterface interface {
		LoadCountImageExifs(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type NuisancesCountInterface interface {
		LoadCountNuisances(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type PoolsCountInterface interface {
		LoadCountPools(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type QuicksCountInterface interface {
		LoadCountQuicks(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}

	return publicreportImageCountThenLoader[Q]{
		ImageExifs: countThenLoadBuilder[Q](
			"ImageExifs",
			func(ctx context.Context, exec bob.Executor, retrieved ImageExifsCountInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadCountImageExifs(ctx, exec, mods...)
			},
		),
		Nuisances: countThenLoadBuilder[Q](
			"Nuisances",
			func(ctx context.Context, exec bob.Executor, retrieved NuisancesCountInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadCountNuisances(ctx, exec, mods...)
			},
		),
		Pools: countThenLoadBuilder[Q](
			"Pools",
			func(ctx context.Context, exec bob.Executor, retrieved PoolsCountInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadCountPools(ctx, exec, mods...)
			},
		),
		Quicks: countThenLoadBuilder[Q](
			"Quicks",
			func(ctx context.Context, exec bob.Executor, retrieved QuicksCountInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadCountQuicks(ctx, exec, mods...)
			},
		),
	}
}

// LoadCountImageExifs loads the count of ImageExifs into the C struct
func (o *PublicreportImage) LoadCountImageExifs(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	count, err := o.ImageExifs(mods...).Count(ctx, exec)
	if err != nil {
		return err
	}

	o.C.ImageExifs = &count
	return nil
}

// LoadCountImageExifs loads the count of ImageExifs for a slice
func (os PublicreportImageSlice) LoadCountImageExifs(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	for _, o := range os {
		if err := o.LoadCountImageExifs(ctx, exec, mods...); err != nil {
			return err
		}
	}

	return nil
}

// LoadCountNuisances loads the count of Nuisances into the C struct
func (o *PublicreportImage) LoadCountNuisances(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	count, err := o.Nuisances(mods...).Count(ctx, exec)
	if err != nil {
		return err
	}

	o.C.Nuisances = &count
	return nil
}

// LoadCountNuisances loads the count of Nuisances for a slice
func (os PublicreportImageSlice) LoadCountNuisances(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	for _, o := range os {
		if err := o.LoadCountNuisances(ctx, exec, mods...); err != nil {
			return err
		}
	}

	return nil
}

// LoadCountPools loads the count of Pools into the C struct
func (o *PublicreportImage) LoadCountPools(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	count, err := o.Pools(mods...).Count(ctx, exec)
	if err != nil {
		return err
	}

	o.C.Pools = &count
	return nil
}

// LoadCountPools loads the count of Pools for a slice
func (os PublicreportImageSlice) LoadCountPools(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	for _, o := range os {
		if err := o.LoadCountPools(ctx, exec, mods...); err != nil {
			return err
		}
	}

	return nil
}

// LoadCountQuicks loads the count of Quicks into the C struct
func (o *PublicreportImage) LoadCountQuicks(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	count, err := o.Quicks(mods...).Count(ctx, exec)
	if err != nil {
		return err
	}

	o.C.Quicks = &count
	return nil
}

// LoadCountQuicks loads the count of Quicks for a slice
func (os PublicreportImageSlice) LoadCountQuicks(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	for _, o := range os {
		if err := o.LoadCountQuicks(ctx, exec, mods...); err != nil {
			return err
		}
	}

	return nil
}

type publicreportImageJoins[Q dialect.Joinable] struct {
	typ        string
	ImageExifs modAs[Q, publicreportImageExifColumns]
	Nuisances  modAs[Q, publicreportNuisanceColumns]
	Pools      modAs[Q, publicreportPoolColumns]
	Quicks     modAs[Q, publicreportQuickColumns]
}

func (j publicreportImageJoins[Q]) aliasedAs(alias string) publicreportImageJoins[Q] {
	return buildPublicreportImageJoins[Q](buildPublicreportImageColumns(alias), j.typ)
}

func buildPublicreportImageJoins[Q dialect.Joinable](cols publicreportImageColumns, typ string) publicreportImageJoins[Q] {
	return publicreportImageJoins[Q]{
		typ: typ,
		ImageExifs: modAs[Q, publicreportImageExifColumns]{
			c: PublicreportImageExifs.Columns,
			f: func(to publicreportImageExifColumns) bob.Mod[Q] {
				mods := make(mods.QueryMods[Q], 0, 1)

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

				return mods
			},
		},
		Nuisances: modAs[Q, publicreportNuisanceColumns]{
			c: PublicreportNuisances.Columns,
			f: func(to publicreportNuisanceColumns) bob.Mod[Q] {
				random := strconv.FormatInt(randInt(), 10)
				mods := make(mods.QueryMods[Q], 0, 2)

				{
					to := PublicreportNuisanceImages.Columns.AliasedAs(PublicreportNuisanceImages.Columns.Alias() + random)
					mods = append(mods, dialect.Join[Q](typ, PublicreportNuisanceImages.Name().As(to.Alias())).On(
						to.ImageID.EQ(cols.ID),
					))
				}
				{
					cols := PublicreportNuisanceImages.Columns.AliasedAs(PublicreportNuisanceImages.Columns.Alias() + random)
					mods = append(mods, dialect.Join[Q](typ, PublicreportNuisances.Name().As(to.Alias())).On(
						to.ID.EQ(cols.NuisanceID),
					))
				}

				return mods
			},
		},
		Pools: modAs[Q, publicreportPoolColumns]{
			c: PublicreportPools.Columns,
			f: func(to publicreportPoolColumns) bob.Mod[Q] {
				random := strconv.FormatInt(randInt(), 10)
				mods := make(mods.QueryMods[Q], 0, 2)

				{
					to := PublicreportPoolImages.Columns.AliasedAs(PublicreportPoolImages.Columns.Alias() + random)
					mods = append(mods, dialect.Join[Q](typ, PublicreportPoolImages.Name().As(to.Alias())).On(
						to.ImageID.EQ(cols.ID),
					))
				}
				{
					cols := PublicreportPoolImages.Columns.AliasedAs(PublicreportPoolImages.Columns.Alias() + random)
					mods = append(mods, dialect.Join[Q](typ, PublicreportPools.Name().As(to.Alias())).On(
						to.ID.EQ(cols.PoolID),
					))
				}

				return mods
			},
		},
		Quicks: modAs[Q, publicreportQuickColumns]{
			c: PublicreportQuicks.Columns,
			f: func(to publicreportQuickColumns) bob.Mod[Q] {
				random := strconv.FormatInt(randInt(), 10)
				mods := make(mods.QueryMods[Q], 0, 2)

				{
					to := PublicreportQuickImages.Columns.AliasedAs(PublicreportQuickImages.Columns.Alias() + random)
					mods = append(mods, dialect.Join[Q](typ, PublicreportQuickImages.Name().As(to.Alias())).On(
						to.ImageID.EQ(cols.ID),
					))
				}
				{
					cols := PublicreportQuickImages.Columns.AliasedAs(PublicreportQuickImages.Columns.Alias() + random)
					mods = append(mods, dialect.Join[Q](typ, PublicreportQuicks.Name().As(to.Alias())).On(
						to.ID.EQ(cols.QuickID),
					))
				}

				return mods
			},
		},
	}
}
