// 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"
	enums "github.com/Gleipnir-Technology/nidus-sync/db/enums"
	"github.com/aarondl/opt/null"
	"github.com/aarondl/opt/omit"
	"github.com/aarondl/opt/omitnull"
	"github.com/stephenafamo/scan"
)

// PublicreportPool is an object representing the database table.
type PublicreportPool struct {
	ID                     int32                              `db:"id,pk" `
	AccessComments         string                             `db:"access_comments" `
	AccessGate             bool                               `db:"access_gate" `
	AccessFence            bool                               `db:"access_fence" `
	AccessLocked           bool                               `db:"access_locked" `
	AccessDog              bool                               `db:"access_dog" `
	AccessOther            bool                               `db:"access_other" `
	Address                string                             `db:"address" `
	AddressCountry         string                             `db:"address_country" `
	AddressPostCode        string                             `db:"address_post_code" `
	AddressPlace           string                             `db:"address_place" `
	AddressStreet          string                             `db:"address_street" `
	AddressRegion          string                             `db:"address_region" `
	Comments               string                             `db:"comments" `
	Created                time.Time                          `db:"created" `
	H3cell                 null.Val[string]                   `db:"h3cell" `
	HasAdult               bool                               `db:"has_adult" `
	HasLarvae              bool                               `db:"has_larvae" `
	HasPupae               bool                               `db:"has_pupae" `
	Location               null.Val[string]                   `db:"location" `
	MapZoom                float32                            `db:"map_zoom" `
	OwnerEmail             string                             `db:"owner_email" `
	OwnerName              string                             `db:"owner_name" `
	OwnerPhone             string                             `db:"owner_phone" `
	PublicID               string                             `db:"public_id" `
	ReporterEmail          string                             `db:"reporter_email" `
	ReporterName           string                             `db:"reporter_name" `
	ReporterPhone          string                             `db:"reporter_phone" `
	Status                 enums.PublicreportReportstatustype `db:"status" `
	OrganizationID         null.Val[int32]                    `db:"organization_id" `
	HasBackyardPermission  bool                               `db:"has_backyard_permission" `
	IsReporterConfidential bool                               `db:"is_reporter_confidential" `
	IsReporterOwner        bool                               `db:"is_reporter_owner" `
	ReporterContactConsent null.Val[bool]                     `db:"reporter_contact_consent" `

	R publicreportPoolR `db:"-" `

	C publicreportPoolC `db:"-" `
}

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

// PublicreportPools contains methods to work with the pool table
var PublicreportPools = psql.NewTablex[*PublicreportPool, PublicreportPoolSlice, *PublicreportPoolSetter]("publicreport", "pool", buildPublicreportPoolColumns("publicreport.pool"))

// PublicreportPoolsQuery is a query on the pool table
type PublicreportPoolsQuery = *psql.ViewQuery[*PublicreportPool, PublicreportPoolSlice]

// publicreportPoolR is where relationships are stored.
type publicreportPoolR struct {
	NotifyEmailPools PublicreportNotifyEmailPoolSlice // publicreport.notify_email_pool.notify_email_pool_pool_id_fkey
	NotifyPhonePools PublicreportNotifyPhonePoolSlice // publicreport.notify_phone_pool.notify_phone_pool_pool_id_fkey
	Organization     *Organization                    // publicreport.pool.pool_organization_id_fkey
	Images           PublicreportImageSlice           // publicreport.pool_image.pool_image_image_id_fkeypublicreport.pool_image.pool_image_pool_id_fkey
}

func buildPublicreportPoolColumns(alias string) publicreportPoolColumns {
	return publicreportPoolColumns{
		ColumnsExpr: expr.NewColumnsExpr(
			"id", "access_comments", "access_gate", "access_fence", "access_locked", "access_dog", "access_other", "address", "address_country", "address_post_code", "address_place", "address_street", "address_region", "comments", "created", "h3cell", "has_adult", "has_larvae", "has_pupae", "location", "map_zoom", "owner_email", "owner_name", "owner_phone", "public_id", "reporter_email", "reporter_name", "reporter_phone", "status", "organization_id", "has_backyard_permission", "is_reporter_confidential", "is_reporter_owner", "reporter_contact_consent",
		).WithParent("publicreport.pool"),
		tableAlias:             alias,
		ID:                     psql.Quote(alias, "id"),
		AccessComments:         psql.Quote(alias, "access_comments"),
		AccessGate:             psql.Quote(alias, "access_gate"),
		AccessFence:            psql.Quote(alias, "access_fence"),
		AccessLocked:           psql.Quote(alias, "access_locked"),
		AccessDog:              psql.Quote(alias, "access_dog"),
		AccessOther:            psql.Quote(alias, "access_other"),
		Address:                psql.Quote(alias, "address"),
		AddressCountry:         psql.Quote(alias, "address_country"),
		AddressPostCode:        psql.Quote(alias, "address_post_code"),
		AddressPlace:           psql.Quote(alias, "address_place"),
		AddressStreet:          psql.Quote(alias, "address_street"),
		AddressRegion:          psql.Quote(alias, "address_region"),
		Comments:               psql.Quote(alias, "comments"),
		Created:                psql.Quote(alias, "created"),
		H3cell:                 psql.Quote(alias, "h3cell"),
		HasAdult:               psql.Quote(alias, "has_adult"),
		HasLarvae:              psql.Quote(alias, "has_larvae"),
		HasPupae:               psql.Quote(alias, "has_pupae"),
		Location:               psql.Quote(alias, "location"),
		MapZoom:                psql.Quote(alias, "map_zoom"),
		OwnerEmail:             psql.Quote(alias, "owner_email"),
		OwnerName:              psql.Quote(alias, "owner_name"),
		OwnerPhone:             psql.Quote(alias, "owner_phone"),
		PublicID:               psql.Quote(alias, "public_id"),
		ReporterEmail:          psql.Quote(alias, "reporter_email"),
		ReporterName:           psql.Quote(alias, "reporter_name"),
		ReporterPhone:          psql.Quote(alias, "reporter_phone"),
		Status:                 psql.Quote(alias, "status"),
		OrganizationID:         psql.Quote(alias, "organization_id"),
		HasBackyardPermission:  psql.Quote(alias, "has_backyard_permission"),
		IsReporterConfidential: psql.Quote(alias, "is_reporter_confidential"),
		IsReporterOwner:        psql.Quote(alias, "is_reporter_owner"),
		ReporterContactConsent: psql.Quote(alias, "reporter_contact_consent"),
	}
}

type publicreportPoolColumns struct {
	expr.ColumnsExpr
	tableAlias             string
	ID                     psql.Expression
	AccessComments         psql.Expression
	AccessGate             psql.Expression
	AccessFence            psql.Expression
	AccessLocked           psql.Expression
	AccessDog              psql.Expression
	AccessOther            psql.Expression
	Address                psql.Expression
	AddressCountry         psql.Expression
	AddressPostCode        psql.Expression
	AddressPlace           psql.Expression
	AddressStreet          psql.Expression
	AddressRegion          psql.Expression
	Comments               psql.Expression
	Created                psql.Expression
	H3cell                 psql.Expression
	HasAdult               psql.Expression
	HasLarvae              psql.Expression
	HasPupae               psql.Expression
	Location               psql.Expression
	MapZoom                psql.Expression
	OwnerEmail             psql.Expression
	OwnerName              psql.Expression
	OwnerPhone             psql.Expression
	PublicID               psql.Expression
	ReporterEmail          psql.Expression
	ReporterName           psql.Expression
	ReporterPhone          psql.Expression
	Status                 psql.Expression
	OrganizationID         psql.Expression
	HasBackyardPermission  psql.Expression
	IsReporterConfidential psql.Expression
	IsReporterOwner        psql.Expression
	ReporterContactConsent psql.Expression
}

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

func (publicreportPoolColumns) AliasedAs(alias string) publicreportPoolColumns {
	return buildPublicreportPoolColumns(alias)
}

// PublicreportPoolSetter is used for insert/upsert/update operations
// All values are optional, and do not have to be set
// Generated columns are not included
type PublicreportPoolSetter struct {
	ID                     omit.Val[int32]                              `db:"id,pk" `
	AccessComments         omit.Val[string]                             `db:"access_comments" `
	AccessGate             omit.Val[bool]                               `db:"access_gate" `
	AccessFence            omit.Val[bool]                               `db:"access_fence" `
	AccessLocked           omit.Val[bool]                               `db:"access_locked" `
	AccessDog              omit.Val[bool]                               `db:"access_dog" `
	AccessOther            omit.Val[bool]                               `db:"access_other" `
	Address                omit.Val[string]                             `db:"address" `
	AddressCountry         omit.Val[string]                             `db:"address_country" `
	AddressPostCode        omit.Val[string]                             `db:"address_post_code" `
	AddressPlace           omit.Val[string]                             `db:"address_place" `
	AddressStreet          omit.Val[string]                             `db:"address_street" `
	AddressRegion          omit.Val[string]                             `db:"address_region" `
	Comments               omit.Val[string]                             `db:"comments" `
	Created                omit.Val[time.Time]                          `db:"created" `
	H3cell                 omitnull.Val[string]                         `db:"h3cell" `
	HasAdult               omit.Val[bool]                               `db:"has_adult" `
	HasLarvae              omit.Val[bool]                               `db:"has_larvae" `
	HasPupae               omit.Val[bool]                               `db:"has_pupae" `
	Location               omitnull.Val[string]                         `db:"location" `
	MapZoom                omit.Val[float32]                            `db:"map_zoom" `
	OwnerEmail             omit.Val[string]                             `db:"owner_email" `
	OwnerName              omit.Val[string]                             `db:"owner_name" `
	OwnerPhone             omit.Val[string]                             `db:"owner_phone" `
	PublicID               omit.Val[string]                             `db:"public_id" `
	ReporterEmail          omit.Val[string]                             `db:"reporter_email" `
	ReporterName           omit.Val[string]                             `db:"reporter_name" `
	ReporterPhone          omit.Val[string]                             `db:"reporter_phone" `
	Status                 omit.Val[enums.PublicreportReportstatustype] `db:"status" `
	OrganizationID         omitnull.Val[int32]                          `db:"organization_id" `
	HasBackyardPermission  omit.Val[bool]                               `db:"has_backyard_permission" `
	IsReporterConfidential omit.Val[bool]                               `db:"is_reporter_confidential" `
	IsReporterOwner        omit.Val[bool]                               `db:"is_reporter_owner" `
	ReporterContactConsent omitnull.Val[bool]                           `db:"reporter_contact_consent" `
}

func (s PublicreportPoolSetter) SetColumns() []string {
	vals := make([]string, 0, 34)
	if s.ID.IsValue() {
		vals = append(vals, "id")
	}
	if s.AccessComments.IsValue() {
		vals = append(vals, "access_comments")
	}
	if s.AccessGate.IsValue() {
		vals = append(vals, "access_gate")
	}
	if s.AccessFence.IsValue() {
		vals = append(vals, "access_fence")
	}
	if s.AccessLocked.IsValue() {
		vals = append(vals, "access_locked")
	}
	if s.AccessDog.IsValue() {
		vals = append(vals, "access_dog")
	}
	if s.AccessOther.IsValue() {
		vals = append(vals, "access_other")
	}
	if s.Address.IsValue() {
		vals = append(vals, "address")
	}
	if s.AddressCountry.IsValue() {
		vals = append(vals, "address_country")
	}
	if s.AddressPostCode.IsValue() {
		vals = append(vals, "address_post_code")
	}
	if s.AddressPlace.IsValue() {
		vals = append(vals, "address_place")
	}
	if s.AddressStreet.IsValue() {
		vals = append(vals, "address_street")
	}
	if s.AddressRegion.IsValue() {
		vals = append(vals, "address_region")
	}
	if s.Comments.IsValue() {
		vals = append(vals, "comments")
	}
	if s.Created.IsValue() {
		vals = append(vals, "created")
	}
	if !s.H3cell.IsUnset() {
		vals = append(vals, "h3cell")
	}
	if s.HasAdult.IsValue() {
		vals = append(vals, "has_adult")
	}
	if s.HasLarvae.IsValue() {
		vals = append(vals, "has_larvae")
	}
	if s.HasPupae.IsValue() {
		vals = append(vals, "has_pupae")
	}
	if !s.Location.IsUnset() {
		vals = append(vals, "location")
	}
	if s.MapZoom.IsValue() {
		vals = append(vals, "map_zoom")
	}
	if s.OwnerEmail.IsValue() {
		vals = append(vals, "owner_email")
	}
	if s.OwnerName.IsValue() {
		vals = append(vals, "owner_name")
	}
	if s.OwnerPhone.IsValue() {
		vals = append(vals, "owner_phone")
	}
	if s.PublicID.IsValue() {
		vals = append(vals, "public_id")
	}
	if s.ReporterEmail.IsValue() {
		vals = append(vals, "reporter_email")
	}
	if s.ReporterName.IsValue() {
		vals = append(vals, "reporter_name")
	}
	if s.ReporterPhone.IsValue() {
		vals = append(vals, "reporter_phone")
	}
	if s.Status.IsValue() {
		vals = append(vals, "status")
	}
	if !s.OrganizationID.IsUnset() {
		vals = append(vals, "organization_id")
	}
	if s.HasBackyardPermission.IsValue() {
		vals = append(vals, "has_backyard_permission")
	}
	if s.IsReporterConfidential.IsValue() {
		vals = append(vals, "is_reporter_confidential")
	}
	if s.IsReporterOwner.IsValue() {
		vals = append(vals, "is_reporter_owner")
	}
	if !s.ReporterContactConsent.IsUnset() {
		vals = append(vals, "reporter_contact_consent")
	}
	return vals
}

func (s PublicreportPoolSetter) Overwrite(t *PublicreportPool) {
	if s.ID.IsValue() {
		t.ID = s.ID.MustGet()
	}
	if s.AccessComments.IsValue() {
		t.AccessComments = s.AccessComments.MustGet()
	}
	if s.AccessGate.IsValue() {
		t.AccessGate = s.AccessGate.MustGet()
	}
	if s.AccessFence.IsValue() {
		t.AccessFence = s.AccessFence.MustGet()
	}
	if s.AccessLocked.IsValue() {
		t.AccessLocked = s.AccessLocked.MustGet()
	}
	if s.AccessDog.IsValue() {
		t.AccessDog = s.AccessDog.MustGet()
	}
	if s.AccessOther.IsValue() {
		t.AccessOther = s.AccessOther.MustGet()
	}
	if s.Address.IsValue() {
		t.Address = s.Address.MustGet()
	}
	if s.AddressCountry.IsValue() {
		t.AddressCountry = s.AddressCountry.MustGet()
	}
	if s.AddressPostCode.IsValue() {
		t.AddressPostCode = s.AddressPostCode.MustGet()
	}
	if s.AddressPlace.IsValue() {
		t.AddressPlace = s.AddressPlace.MustGet()
	}
	if s.AddressStreet.IsValue() {
		t.AddressStreet = s.AddressStreet.MustGet()
	}
	if s.AddressRegion.IsValue() {
		t.AddressRegion = s.AddressRegion.MustGet()
	}
	if s.Comments.IsValue() {
		t.Comments = s.Comments.MustGet()
	}
	if s.Created.IsValue() {
		t.Created = s.Created.MustGet()
	}
	if !s.H3cell.IsUnset() {
		t.H3cell = s.H3cell.MustGetNull()
	}
	if s.HasAdult.IsValue() {
		t.HasAdult = s.HasAdult.MustGet()
	}
	if s.HasLarvae.IsValue() {
		t.HasLarvae = s.HasLarvae.MustGet()
	}
	if s.HasPupae.IsValue() {
		t.HasPupae = s.HasPupae.MustGet()
	}
	if !s.Location.IsUnset() {
		t.Location = s.Location.MustGetNull()
	}
	if s.MapZoom.IsValue() {
		t.MapZoom = s.MapZoom.MustGet()
	}
	if s.OwnerEmail.IsValue() {
		t.OwnerEmail = s.OwnerEmail.MustGet()
	}
	if s.OwnerName.IsValue() {
		t.OwnerName = s.OwnerName.MustGet()
	}
	if s.OwnerPhone.IsValue() {
		t.OwnerPhone = s.OwnerPhone.MustGet()
	}
	if s.PublicID.IsValue() {
		t.PublicID = s.PublicID.MustGet()
	}
	if s.ReporterEmail.IsValue() {
		t.ReporterEmail = s.ReporterEmail.MustGet()
	}
	if s.ReporterName.IsValue() {
		t.ReporterName = s.ReporterName.MustGet()
	}
	if s.ReporterPhone.IsValue() {
		t.ReporterPhone = s.ReporterPhone.MustGet()
	}
	if s.Status.IsValue() {
		t.Status = s.Status.MustGet()
	}
	if !s.OrganizationID.IsUnset() {
		t.OrganizationID = s.OrganizationID.MustGetNull()
	}
	if s.HasBackyardPermission.IsValue() {
		t.HasBackyardPermission = s.HasBackyardPermission.MustGet()
	}
	if s.IsReporterConfidential.IsValue() {
		t.IsReporterConfidential = s.IsReporterConfidential.MustGet()
	}
	if s.IsReporterOwner.IsValue() {
		t.IsReporterOwner = s.IsReporterOwner.MustGet()
	}
	if !s.ReporterContactConsent.IsUnset() {
		t.ReporterContactConsent = s.ReporterContactConsent.MustGetNull()
	}
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return exprs
}

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

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

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

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

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

	return err
}

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

func (o *PublicreportPool) pkEQ() dialect.Expression {
	return psql.Quote("publicreport.pool", "id").EQ(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
		return o.primaryKeyVals().WriteSQL(ctx, w, d, start)
	}))
}

// Update uses an executor to update the PublicreportPool
func (o *PublicreportPool) Update(ctx context.Context, exec bob.Executor, s *PublicreportPoolSetter) error {
	v, err := PublicreportPools.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 PublicreportPool record with an executor
func (o *PublicreportPool) Delete(ctx context.Context, exec bob.Executor) error {
	_, err := PublicreportPools.Delete(dm.Where(o.pkEQ())).Exec(ctx, exec)
	return err
}

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

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

	return err
}

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

	return psql.Quote("publicreport.pool", "id").In(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
		pkPairs := make([]bob.Expression, len(o))
		for i, row := range o {
			pkPairs[i] = row.primaryKeyVals()
		}
		return bob.ExpressSlice(ctx, w, d, start, pkPairs, "", ", ", "")
	}))
}

// copyMatchingRows finds models in the given slice that have the same primary key
// then it first copies the existing relationships from the old model to the new model
// and then replaces the old model in the slice with the new model
func (o PublicreportPoolSlice) copyMatchingRows(from ...*PublicreportPool) {
	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 PublicreportPoolSlice) 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 PublicreportPools.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 *PublicreportPool:
				o.copyMatchingRows(retrieved)
			case []*PublicreportPool:
				o.copyMatchingRows(retrieved...)
			case PublicreportPoolSlice:
				o.copyMatchingRows(retrieved...)
			default:
				// If the retrieved value is not a PublicreportPool or a slice of PublicreportPool
				// then run the AfterUpdateHooks on the slice
				_, err = PublicreportPools.AfterUpdateHooks.RunHooks(ctx, exec, o)
			}

			return err
		}))

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

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

			return err
		}))

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

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

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

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

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

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

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

	o.copyMatchingRows(o2...)

	return nil
}

// NotifyEmailPools starts a query for related objects on publicreport.notify_email_pool
func (o *PublicreportPool) NotifyEmailPools(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportNotifyEmailPoolsQuery {
	return PublicreportNotifyEmailPools.Query(append(mods,
		sm.Where(PublicreportNotifyEmailPools.Columns.PoolID.EQ(psql.Arg(o.ID))),
	)...)
}

func (os PublicreportPoolSlice) NotifyEmailPools(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportNotifyEmailPoolsQuery {
	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 PublicreportNotifyEmailPools.Query(append(mods,
		sm.Where(psql.Group(PublicreportNotifyEmailPools.Columns.PoolID).OP("IN", PKArgExpr)),
	)...)
}

// NotifyPhonePools starts a query for related objects on publicreport.notify_phone_pool
func (o *PublicreportPool) NotifyPhonePools(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportNotifyPhonePoolsQuery {
	return PublicreportNotifyPhonePools.Query(append(mods,
		sm.Where(PublicreportNotifyPhonePools.Columns.PoolID.EQ(psql.Arg(o.ID))),
	)...)
}

func (os PublicreportPoolSlice) NotifyPhonePools(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportNotifyPhonePoolsQuery {
	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 PublicreportNotifyPhonePools.Query(append(mods,
		sm.Where(psql.Group(PublicreportNotifyPhonePools.Columns.PoolID).OP("IN", PKArgExpr)),
	)...)
}

// Organization starts a query for related objects on organization
func (o *PublicreportPool) Organization(mods ...bob.Mod[*dialect.SelectQuery]) OrganizationsQuery {
	return Organizations.Query(append(mods,
		sm.Where(Organizations.Columns.ID.EQ(psql.Arg(o.OrganizationID))),
	)...)
}

func (os PublicreportPoolSlice) Organization(mods ...bob.Mod[*dialect.SelectQuery]) OrganizationsQuery {
	pkOrganizationID := make(pgtypes.Array[null.Val[int32]], 0, len(os))
	for _, o := range os {
		if o == nil {
			continue
		}
		pkOrganizationID = append(pkOrganizationID, o.OrganizationID)
	}
	PKArgExpr := psql.Select(sm.Columns(
		psql.F("unnest", psql.Cast(psql.Arg(pkOrganizationID), "integer[]")),
	))

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

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

func (os PublicreportPoolSlice) Images(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportImagesQuery {
	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 PublicreportImages.Query(append(mods,
		sm.InnerJoin(PublicreportPoolImages.NameAs()).On(
			PublicreportImages.Columns.ID.EQ(PublicreportPoolImages.Columns.ImageID),
		),
		sm.Where(psql.Group(PublicreportPoolImages.Columns.PoolID).OP("IN", PKArgExpr)),
	)...)
}

func insertPublicreportPoolNotifyEmailPools0(ctx context.Context, exec bob.Executor, publicreportNotifyEmailPools1 []*PublicreportNotifyEmailPoolSetter, publicreportPool0 *PublicreportPool) (PublicreportNotifyEmailPoolSlice, error) {
	for i := range publicreportNotifyEmailPools1 {
		publicreportNotifyEmailPools1[i].PoolID = omit.From(publicreportPool0.ID)
	}

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

	return ret, nil
}

func attachPublicreportPoolNotifyEmailPools0(ctx context.Context, exec bob.Executor, count int, publicreportNotifyEmailPools1 PublicreportNotifyEmailPoolSlice, publicreportPool0 *PublicreportPool) (PublicreportNotifyEmailPoolSlice, error) {
	setter := &PublicreportNotifyEmailPoolSetter{
		PoolID: omit.From(publicreportPool0.ID),
	}

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

	return publicreportNotifyEmailPools1, nil
}

func (publicreportPool0 *PublicreportPool) InsertNotifyEmailPools(ctx context.Context, exec bob.Executor, related ...*PublicreportNotifyEmailPoolSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	publicreportNotifyEmailPools1, err := insertPublicreportPoolNotifyEmailPools0(ctx, exec, related, publicreportPool0)
	if err != nil {
		return err
	}

	publicreportPool0.R.NotifyEmailPools = append(publicreportPool0.R.NotifyEmailPools, publicreportNotifyEmailPools1...)

	for _, rel := range publicreportNotifyEmailPools1 {
		rel.R.Pool = publicreportPool0
	}
	return nil
}

func (publicreportPool0 *PublicreportPool) AttachNotifyEmailPools(ctx context.Context, exec bob.Executor, related ...*PublicreportNotifyEmailPool) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	publicreportNotifyEmailPools1 := PublicreportNotifyEmailPoolSlice(related)

	_, err = attachPublicreportPoolNotifyEmailPools0(ctx, exec, len(related), publicreportNotifyEmailPools1, publicreportPool0)
	if err != nil {
		return err
	}

	publicreportPool0.R.NotifyEmailPools = append(publicreportPool0.R.NotifyEmailPools, publicreportNotifyEmailPools1...)

	for _, rel := range related {
		rel.R.Pool = publicreportPool0
	}

	return nil
}

func insertPublicreportPoolNotifyPhonePools0(ctx context.Context, exec bob.Executor, publicreportNotifyPhonePools1 []*PublicreportNotifyPhonePoolSetter, publicreportPool0 *PublicreportPool) (PublicreportNotifyPhonePoolSlice, error) {
	for i := range publicreportNotifyPhonePools1 {
		publicreportNotifyPhonePools1[i].PoolID = omit.From(publicreportPool0.ID)
	}

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

	return ret, nil
}

func attachPublicreportPoolNotifyPhonePools0(ctx context.Context, exec bob.Executor, count int, publicreportNotifyPhonePools1 PublicreportNotifyPhonePoolSlice, publicreportPool0 *PublicreportPool) (PublicreportNotifyPhonePoolSlice, error) {
	setter := &PublicreportNotifyPhonePoolSetter{
		PoolID: omit.From(publicreportPool0.ID),
	}

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

	return publicreportNotifyPhonePools1, nil
}

func (publicreportPool0 *PublicreportPool) InsertNotifyPhonePools(ctx context.Context, exec bob.Executor, related ...*PublicreportNotifyPhonePoolSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	publicreportNotifyPhonePools1, err := insertPublicreportPoolNotifyPhonePools0(ctx, exec, related, publicreportPool0)
	if err != nil {
		return err
	}

	publicreportPool0.R.NotifyPhonePools = append(publicreportPool0.R.NotifyPhonePools, publicreportNotifyPhonePools1...)

	for _, rel := range publicreportNotifyPhonePools1 {
		rel.R.Pool = publicreportPool0
	}
	return nil
}

func (publicreportPool0 *PublicreportPool) AttachNotifyPhonePools(ctx context.Context, exec bob.Executor, related ...*PublicreportNotifyPhonePool) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	publicreportNotifyPhonePools1 := PublicreportNotifyPhonePoolSlice(related)

	_, err = attachPublicreportPoolNotifyPhonePools0(ctx, exec, len(related), publicreportNotifyPhonePools1, publicreportPool0)
	if err != nil {
		return err
	}

	publicreportPool0.R.NotifyPhonePools = append(publicreportPool0.R.NotifyPhonePools, publicreportNotifyPhonePools1...)

	for _, rel := range related {
		rel.R.Pool = publicreportPool0
	}

	return nil
}

func attachPublicreportPoolOrganization0(ctx context.Context, exec bob.Executor, count int, publicreportPool0 *PublicreportPool, organization1 *Organization) (*PublicreportPool, error) {
	setter := &PublicreportPoolSetter{
		OrganizationID: omitnull.From(organization1.ID),
	}

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

	return publicreportPool0, nil
}

func (publicreportPool0 *PublicreportPool) InsertOrganization(ctx context.Context, exec bob.Executor, related *OrganizationSetter) error {
	var err error

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

	_, err = attachPublicreportPoolOrganization0(ctx, exec, 1, publicreportPool0, organization1)
	if err != nil {
		return err
	}

	publicreportPool0.R.Organization = organization1

	organization1.R.PublicreportPool = append(organization1.R.PublicreportPool, publicreportPool0)

	return nil
}

func (publicreportPool0 *PublicreportPool) AttachOrganization(ctx context.Context, exec bob.Executor, organization1 *Organization) error {
	var err error

	_, err = attachPublicreportPoolOrganization0(ctx, exec, 1, publicreportPool0, organization1)
	if err != nil {
		return err
	}

	publicreportPool0.R.Organization = organization1

	organization1.R.PublicreportPool = append(organization1.R.PublicreportPool, publicreportPool0)

	return nil
}

func attachPublicreportPoolImages0(ctx context.Context, exec bob.Executor, count int, publicreportPool0 *PublicreportPool, publicreportImages2 PublicreportImageSlice) (PublicreportPoolImageSlice, error) {
	setters := make([]*PublicreportPoolImageSetter, count)
	for i := range count {
		setters[i] = &PublicreportPoolImageSetter{
			PoolID:  omit.From(publicreportPool0.ID),
			ImageID: omit.From(publicreportImages2[i].ID),
		}
	}

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

	return publicreportPoolImages1, nil
}

func (publicreportPool0 *PublicreportPool) InsertImages(ctx context.Context, exec bob.Executor, related ...*PublicreportImageSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

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

	_, err = attachPublicreportPoolImages0(ctx, exec, len(related), publicreportPool0, publicreportImages2)
	if err != nil {
		return err
	}

	publicreportPool0.R.Images = append(publicreportPool0.R.Images, publicreportImages2...)

	for _, rel := range publicreportImages2 {
		rel.R.Pools = append(rel.R.Pools, publicreportPool0)
	}
	return nil
}

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

	var err error
	publicreportImages2 := PublicreportImageSlice(related)

	_, err = attachPublicreportPoolImages0(ctx, exec, len(related), publicreportPool0, publicreportImages2)
	if err != nil {
		return err
	}

	publicreportPool0.R.Images = append(publicreportPool0.R.Images, publicreportImages2...)

	for _, rel := range related {
		rel.R.Pools = append(rel.R.Pools, publicreportPool0)
	}

	return nil
}

type publicreportPoolWhere[Q psql.Filterable] struct {
	ID                     psql.WhereMod[Q, int32]
	AccessComments         psql.WhereMod[Q, string]
	AccessGate             psql.WhereMod[Q, bool]
	AccessFence            psql.WhereMod[Q, bool]
	AccessLocked           psql.WhereMod[Q, bool]
	AccessDog              psql.WhereMod[Q, bool]
	AccessOther            psql.WhereMod[Q, bool]
	Address                psql.WhereMod[Q, string]
	AddressCountry         psql.WhereMod[Q, string]
	AddressPostCode        psql.WhereMod[Q, string]
	AddressPlace           psql.WhereMod[Q, string]
	AddressStreet          psql.WhereMod[Q, string]
	AddressRegion          psql.WhereMod[Q, string]
	Comments               psql.WhereMod[Q, string]
	Created                psql.WhereMod[Q, time.Time]
	H3cell                 psql.WhereNullMod[Q, string]
	HasAdult               psql.WhereMod[Q, bool]
	HasLarvae              psql.WhereMod[Q, bool]
	HasPupae               psql.WhereMod[Q, bool]
	Location               psql.WhereNullMod[Q, string]
	MapZoom                psql.WhereMod[Q, float32]
	OwnerEmail             psql.WhereMod[Q, string]
	OwnerName              psql.WhereMod[Q, string]
	OwnerPhone             psql.WhereMod[Q, string]
	PublicID               psql.WhereMod[Q, string]
	ReporterEmail          psql.WhereMod[Q, string]
	ReporterName           psql.WhereMod[Q, string]
	ReporterPhone          psql.WhereMod[Q, string]
	Status                 psql.WhereMod[Q, enums.PublicreportReportstatustype]
	OrganizationID         psql.WhereNullMod[Q, int32]
	HasBackyardPermission  psql.WhereMod[Q, bool]
	IsReporterConfidential psql.WhereMod[Q, bool]
	IsReporterOwner        psql.WhereMod[Q, bool]
	ReporterContactConsent psql.WhereNullMod[Q, bool]
}

func (publicreportPoolWhere[Q]) AliasedAs(alias string) publicreportPoolWhere[Q] {
	return buildPublicreportPoolWhere[Q](buildPublicreportPoolColumns(alias))
}

func buildPublicreportPoolWhere[Q psql.Filterable](cols publicreportPoolColumns) publicreportPoolWhere[Q] {
	return publicreportPoolWhere[Q]{
		ID:                     psql.Where[Q, int32](cols.ID),
		AccessComments:         psql.Where[Q, string](cols.AccessComments),
		AccessGate:             psql.Where[Q, bool](cols.AccessGate),
		AccessFence:            psql.Where[Q, bool](cols.AccessFence),
		AccessLocked:           psql.Where[Q, bool](cols.AccessLocked),
		AccessDog:              psql.Where[Q, bool](cols.AccessDog),
		AccessOther:            psql.Where[Q, bool](cols.AccessOther),
		Address:                psql.Where[Q, string](cols.Address),
		AddressCountry:         psql.Where[Q, string](cols.AddressCountry),
		AddressPostCode:        psql.Where[Q, string](cols.AddressPostCode),
		AddressPlace:           psql.Where[Q, string](cols.AddressPlace),
		AddressStreet:          psql.Where[Q, string](cols.AddressStreet),
		AddressRegion:          psql.Where[Q, string](cols.AddressRegion),
		Comments:               psql.Where[Q, string](cols.Comments),
		Created:                psql.Where[Q, time.Time](cols.Created),
		H3cell:                 psql.WhereNull[Q, string](cols.H3cell),
		HasAdult:               psql.Where[Q, bool](cols.HasAdult),
		HasLarvae:              psql.Where[Q, bool](cols.HasLarvae),
		HasPupae:               psql.Where[Q, bool](cols.HasPupae),
		Location:               psql.WhereNull[Q, string](cols.Location),
		MapZoom:                psql.Where[Q, float32](cols.MapZoom),
		OwnerEmail:             psql.Where[Q, string](cols.OwnerEmail),
		OwnerName:              psql.Where[Q, string](cols.OwnerName),
		OwnerPhone:             psql.Where[Q, string](cols.OwnerPhone),
		PublicID:               psql.Where[Q, string](cols.PublicID),
		ReporterEmail:          psql.Where[Q, string](cols.ReporterEmail),
		ReporterName:           psql.Where[Q, string](cols.ReporterName),
		ReporterPhone:          psql.Where[Q, string](cols.ReporterPhone),
		Status:                 psql.Where[Q, enums.PublicreportReportstatustype](cols.Status),
		OrganizationID:         psql.WhereNull[Q, int32](cols.OrganizationID),
		HasBackyardPermission:  psql.Where[Q, bool](cols.HasBackyardPermission),
		IsReporterConfidential: psql.Where[Q, bool](cols.IsReporterConfidential),
		IsReporterOwner:        psql.Where[Q, bool](cols.IsReporterOwner),
		ReporterContactConsent: psql.WhereNull[Q, bool](cols.ReporterContactConsent),
	}
}

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

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

		o.R.NotifyEmailPools = rels

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

		o.R.NotifyPhonePools = rels

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

		o.R.Organization = rel

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

		o.R.Images = rels

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

type publicreportPoolPreloader struct {
	Organization func(...psql.PreloadOption) psql.Preloader
}

func buildPublicreportPoolPreloader() publicreportPoolPreloader {
	return publicreportPoolPreloader{
		Organization: func(opts ...psql.PreloadOption) psql.Preloader {
			return psql.Preload[*Organization, OrganizationSlice](psql.PreloadRel{
				Name: "Organization",
				Sides: []psql.PreloadSide{
					{
						From:        PublicreportPools,
						To:          Organizations,
						FromColumns: []string{"organization_id"},
						ToColumns:   []string{"id"},
					},
				},
			}, Organizations.Columns.Names(), opts...)
		},
	}
}

type publicreportPoolThenLoader[Q orm.Loadable] struct {
	NotifyEmailPools func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	NotifyPhonePools func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	Organization     func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	Images           func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
}

func buildPublicreportPoolThenLoader[Q orm.Loadable]() publicreportPoolThenLoader[Q] {
	type NotifyEmailPoolsLoadInterface interface {
		LoadNotifyEmailPools(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type NotifyPhonePoolsLoadInterface interface {
		LoadNotifyPhonePools(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type OrganizationLoadInterface interface {
		LoadOrganization(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type ImagesLoadInterface interface {
		LoadImages(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}

	return publicreportPoolThenLoader[Q]{
		NotifyEmailPools: thenLoadBuilder[Q](
			"NotifyEmailPools",
			func(ctx context.Context, exec bob.Executor, retrieved NotifyEmailPoolsLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadNotifyEmailPools(ctx, exec, mods...)
			},
		),
		NotifyPhonePools: thenLoadBuilder[Q](
			"NotifyPhonePools",
			func(ctx context.Context, exec bob.Executor, retrieved NotifyPhonePoolsLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadNotifyPhonePools(ctx, exec, mods...)
			},
		),
		Organization: thenLoadBuilder[Q](
			"Organization",
			func(ctx context.Context, exec bob.Executor, retrieved OrganizationLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadOrganization(ctx, exec, mods...)
			},
		),
		Images: thenLoadBuilder[Q](
			"Images",
			func(ctx context.Context, exec bob.Executor, retrieved ImagesLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadImages(ctx, exec, mods...)
			},
		),
	}
}

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

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

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

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

	o.R.NotifyEmailPools = related
	return nil
}

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

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

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

		o.R.NotifyEmailPools = nil
	}

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

		for _, rel := range publicreportNotifyEmailPools {

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

			rel.R.Pool = o

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

	return nil
}

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

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

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

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

	o.R.NotifyPhonePools = related
	return nil
}

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

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

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

		o.R.NotifyPhonePools = nil
	}

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

		for _, rel := range publicreportNotifyPhonePools {

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

			rel.R.Pool = o

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

	return nil
}

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

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

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

	related.R.PublicreportPool = PublicreportPoolSlice{o}

	o.R.Organization = related
	return nil
}

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

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

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

		for _, rel := range organizations {
			if !o.OrganizationID.IsValue() {
				continue
			}

			if !(o.OrganizationID.IsValue() && o.OrganizationID.MustGet() == rel.ID) {
				continue
			}

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

			o.R.Organization = rel
			break
		}
	}

	return nil
}

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

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

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

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

	o.R.Images = related
	return nil
}

// LoadImages loads the publicreportPool's Images into the .R struct
func (os PublicreportPoolSlice) LoadImages(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(PublicreportImages.Columns))
	}

	q := os.Images(append(
		mods,
		sm.Columns(PublicreportPoolImages.Columns.PoolID.As("related_publicreport.pool.ID")),
	)...)

	IDSlice := []int32{}

	mapper := scan.Mod(scan.StructMapper[*PublicreportImage](), 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.pool.ID", &IDSlice[len(IDSlice)-1])

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

	publicreportImages, err := bob.Allx[bob.SliceTransformer[*PublicreportImage, PublicreportImageSlice]](ctx, exec, q, mapper)
	if err != nil {
		return err
	}

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

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

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

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

	return nil
}

// publicreportPoolC is where relationship counts are stored.
type publicreportPoolC struct {
	NotifyEmailPools *int64
	NotifyPhonePools *int64
	Images           *int64
}

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

	switch name {
	case "NotifyEmailPools":
		o.C.NotifyEmailPools = &count
	case "NotifyPhonePools":
		o.C.NotifyPhonePools = &count
	case "Images":
		o.C.Images = &count
	}
	return nil
}

type publicreportPoolCountPreloader struct {
	NotifyEmailPools func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader
	NotifyPhonePools func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader
	Images           func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader
}

func buildPublicreportPoolCountPreloader() publicreportPoolCountPreloader {
	return publicreportPoolCountPreloader{
		NotifyEmailPools: func(mods ...bob.Mod[*dialect.SelectQuery]) psql.Preloader {
			return countPreloader[*PublicreportPool]("NotifyEmailPools", func(parent string) bob.Expression {
				// Build a correlated subquery: (SELECT COUNT(*) FROM related WHERE fk = parent.pk)
				if parent == "" {
					parent = PublicreportPools.Alias()
				}

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

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

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

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

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

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

type publicreportPoolCountThenLoader[Q orm.Loadable] struct {
	NotifyEmailPools func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	NotifyPhonePools func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	Images           func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
}

func buildPublicreportPoolCountThenLoader[Q orm.Loadable]() publicreportPoolCountThenLoader[Q] {
	type NotifyEmailPoolsCountInterface interface {
		LoadCountNotifyEmailPools(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type NotifyPhonePoolsCountInterface interface {
		LoadCountNotifyPhonePools(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type ImagesCountInterface interface {
		LoadCountImages(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}

	return publicreportPoolCountThenLoader[Q]{
		NotifyEmailPools: countThenLoadBuilder[Q](
			"NotifyEmailPools",
			func(ctx context.Context, exec bob.Executor, retrieved NotifyEmailPoolsCountInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadCountNotifyEmailPools(ctx, exec, mods...)
			},
		),
		NotifyPhonePools: countThenLoadBuilder[Q](
			"NotifyPhonePools",
			func(ctx context.Context, exec bob.Executor, retrieved NotifyPhonePoolsCountInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadCountNotifyPhonePools(ctx, exec, mods...)
			},
		),
		Images: countThenLoadBuilder[Q](
			"Images",
			func(ctx context.Context, exec bob.Executor, retrieved ImagesCountInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadCountImages(ctx, exec, mods...)
			},
		),
	}
}

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

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

	o.C.NotifyEmailPools = &count
	return nil
}

// LoadCountNotifyEmailPools loads the count of NotifyEmailPools for a slice
func (os PublicreportPoolSlice) LoadCountNotifyEmailPools(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.LoadCountNotifyEmailPools(ctx, exec, mods...); err != nil {
			return err
		}
	}

	return nil
}

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

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

	o.C.NotifyPhonePools = &count
	return nil
}

// LoadCountNotifyPhonePools loads the count of NotifyPhonePools for a slice
func (os PublicreportPoolSlice) LoadCountNotifyPhonePools(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.LoadCountNotifyPhonePools(ctx, exec, mods...); err != nil {
			return err
		}
	}

	return nil
}

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

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

	o.C.Images = &count
	return nil
}

// LoadCountImages loads the count of Images for a slice
func (os PublicreportPoolSlice) LoadCountImages(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.LoadCountImages(ctx, exec, mods...); err != nil {
			return err
		}
	}

	return nil
}

type publicreportPoolJoins[Q dialect.Joinable] struct {
	typ              string
	NotifyEmailPools modAs[Q, publicreportNotifyEmailPoolColumns]
	NotifyPhonePools modAs[Q, publicreportNotifyPhonePoolColumns]
	Organization     modAs[Q, organizationColumns]
	Images           modAs[Q, publicreportImageColumns]
}

func (j publicreportPoolJoins[Q]) aliasedAs(alias string) publicreportPoolJoins[Q] {
	return buildPublicreportPoolJoins[Q](buildPublicreportPoolColumns(alias), j.typ)
}

func buildPublicreportPoolJoins[Q dialect.Joinable](cols publicreportPoolColumns, typ string) publicreportPoolJoins[Q] {
	return publicreportPoolJoins[Q]{
		typ: typ,
		NotifyEmailPools: modAs[Q, publicreportNotifyEmailPoolColumns]{
			c: PublicreportNotifyEmailPools.Columns,
			f: func(to publicreportNotifyEmailPoolColumns) bob.Mod[Q] {
				mods := make(mods.QueryMods[Q], 0, 1)

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

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

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

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

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

				return mods
			},
		},
		Images: modAs[Q, publicreportImageColumns]{
			c: PublicreportImages.Columns,
			f: func(to publicreportImageColumns) 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.PoolID.EQ(cols.ID),
					))
				}
				{
					cols := PublicreportPoolImages.Columns.AliasedAs(PublicreportPoolImages.Columns.Alias() + random)
					mods = append(mods, dialect.Join[Q](typ, PublicreportImages.Name().As(to.Alias())).On(
						to.ID.EQ(cols.ImageID),
					))
				}

				return mods
			},
		},
	}
}
