// Code generated by BobGen psql v0.42.5. DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.

package models

import (
	"context"
	"fmt"
	"io"

	"github.com/Gleipnir-Technology/bob"
	"github.com/Gleipnir-Technology/bob/dialect/psql"
	"github.com/Gleipnir-Technology/bob/dialect/psql/dialect"
	"github.com/Gleipnir-Technology/bob/dialect/psql/dm"
	"github.com/Gleipnir-Technology/bob/dialect/psql/sm"
	"github.com/Gleipnir-Technology/bob/dialect/psql/um"
	"github.com/Gleipnir-Technology/bob/expr"
	"github.com/Gleipnir-Technology/bob/orm"
	"github.com/Gleipnir-Technology/bob/types/pgtypes"
	"github.com/aarondl/opt/omit"
	enums "source.gleipnir.technology/Gleipnir/nidus-sync/db/enums"
)

// PublicreportNuisance is an object representing the database table.
type PublicreportNuisance struct {
	AdditionalInfo      string                                 `db:"additional_info" `
	Duration            enums.PublicreportNuisancedurationtype `db:"duration" `
	IsLocationBackyard  bool                                   `db:"is_location_backyard" `
	IsLocationFrontyard bool                                   `db:"is_location_frontyard" `
	IsLocationGarden    bool                                   `db:"is_location_garden" `
	IsLocationOther     bool                                   `db:"is_location_other" `
	IsLocationPool      bool                                   `db:"is_location_pool" `
	ReportID            int32                                  `db:"report_id,pk" `
	SourceContainer     bool                                   `db:"source_container" `
	SourceDescription   string                                 `db:"source_description" `
	SourceStagnant      bool                                   `db:"source_stagnant" `
	SourceGutter        bool                                   `db:"source_gutter" `
	TodEarly            bool                                   `db:"tod_early" `
	TodDay              bool                                   `db:"tod_day" `
	TodEvening          bool                                   `db:"tod_evening" `
	TodNight            bool                                   `db:"tod_night" `

	R publicreportNuisanceR `db:"-" `
}

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

// PublicreportNuisances contains methods to work with the nuisance table
var PublicreportNuisances = psql.NewTablex[*PublicreportNuisance, PublicreportNuisanceSlice, *PublicreportNuisanceSetter]("publicreport", "nuisance", buildPublicreportNuisanceColumns("publicreport.nuisance"))

// PublicreportNuisancesQuery is a query on the nuisance table
type PublicreportNuisancesQuery = *psql.ViewQuery[*PublicreportNuisance, PublicreportNuisanceSlice]

// publicreportNuisanceR is where relationships are stored.
type publicreportNuisanceR struct {
	Report *PublicreportReport // publicreport.nuisance.nuisance_report_id_fkey
}

func buildPublicreportNuisanceColumns(alias string) publicreportNuisanceColumns {
	return publicreportNuisanceColumns{
		ColumnsExpr: expr.NewColumnsExpr(
			"additional_info", "duration", "is_location_backyard", "is_location_frontyard", "is_location_garden", "is_location_other", "is_location_pool", "report_id", "source_container", "source_description", "source_stagnant", "source_gutter", "tod_early", "tod_day", "tod_evening", "tod_night",
		).WithParent("publicreport.nuisance"),
		tableAlias:          alias,
		AdditionalInfo:      psql.Quote(alias, "additional_info"),
		Duration:            psql.Quote(alias, "duration"),
		IsLocationBackyard:  psql.Quote(alias, "is_location_backyard"),
		IsLocationFrontyard: psql.Quote(alias, "is_location_frontyard"),
		IsLocationGarden:    psql.Quote(alias, "is_location_garden"),
		IsLocationOther:     psql.Quote(alias, "is_location_other"),
		IsLocationPool:      psql.Quote(alias, "is_location_pool"),
		ReportID:            psql.Quote(alias, "report_id"),
		SourceContainer:     psql.Quote(alias, "source_container"),
		SourceDescription:   psql.Quote(alias, "source_description"),
		SourceStagnant:      psql.Quote(alias, "source_stagnant"),
		SourceGutter:        psql.Quote(alias, "source_gutter"),
		TodEarly:            psql.Quote(alias, "tod_early"),
		TodDay:              psql.Quote(alias, "tod_day"),
		TodEvening:          psql.Quote(alias, "tod_evening"),
		TodNight:            psql.Quote(alias, "tod_night"),
	}
}

type publicreportNuisanceColumns struct {
	expr.ColumnsExpr
	tableAlias          string
	AdditionalInfo      psql.Expression
	Duration            psql.Expression
	IsLocationBackyard  psql.Expression
	IsLocationFrontyard psql.Expression
	IsLocationGarden    psql.Expression
	IsLocationOther     psql.Expression
	IsLocationPool      psql.Expression
	ReportID            psql.Expression
	SourceContainer     psql.Expression
	SourceDescription   psql.Expression
	SourceStagnant      psql.Expression
	SourceGutter        psql.Expression
	TodEarly            psql.Expression
	TodDay              psql.Expression
	TodEvening          psql.Expression
	TodNight            psql.Expression
}

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

func (publicreportNuisanceColumns) AliasedAs(alias string) publicreportNuisanceColumns {
	return buildPublicreportNuisanceColumns(alias)
}

// PublicreportNuisanceSetter is used for insert/upsert/update operations
// All values are optional, and do not have to be set
// Generated columns are not included
type PublicreportNuisanceSetter struct {
	AdditionalInfo      omit.Val[string]                                 `db:"additional_info" `
	Duration            omit.Val[enums.PublicreportNuisancedurationtype] `db:"duration" `
	IsLocationBackyard  omit.Val[bool]                                   `db:"is_location_backyard" `
	IsLocationFrontyard omit.Val[bool]                                   `db:"is_location_frontyard" `
	IsLocationGarden    omit.Val[bool]                                   `db:"is_location_garden" `
	IsLocationOther     omit.Val[bool]                                   `db:"is_location_other" `
	IsLocationPool      omit.Val[bool]                                   `db:"is_location_pool" `
	ReportID            omit.Val[int32]                                  `db:"report_id,pk" `
	SourceContainer     omit.Val[bool]                                   `db:"source_container" `
	SourceDescription   omit.Val[string]                                 `db:"source_description" `
	SourceStagnant      omit.Val[bool]                                   `db:"source_stagnant" `
	SourceGutter        omit.Val[bool]                                   `db:"source_gutter" `
	TodEarly            omit.Val[bool]                                   `db:"tod_early" `
	TodDay              omit.Val[bool]                                   `db:"tod_day" `
	TodEvening          omit.Val[bool]                                   `db:"tod_evening" `
	TodNight            omit.Val[bool]                                   `db:"tod_night" `
}

func (s PublicreportNuisanceSetter) SetColumns() []string {
	vals := make([]string, 0, 16)
	if s.AdditionalInfo.IsValue() {
		vals = append(vals, "additional_info")
	}
	if s.Duration.IsValue() {
		vals = append(vals, "duration")
	}
	if s.IsLocationBackyard.IsValue() {
		vals = append(vals, "is_location_backyard")
	}
	if s.IsLocationFrontyard.IsValue() {
		vals = append(vals, "is_location_frontyard")
	}
	if s.IsLocationGarden.IsValue() {
		vals = append(vals, "is_location_garden")
	}
	if s.IsLocationOther.IsValue() {
		vals = append(vals, "is_location_other")
	}
	if s.IsLocationPool.IsValue() {
		vals = append(vals, "is_location_pool")
	}
	if s.ReportID.IsValue() {
		vals = append(vals, "report_id")
	}
	if s.SourceContainer.IsValue() {
		vals = append(vals, "source_container")
	}
	if s.SourceDescription.IsValue() {
		vals = append(vals, "source_description")
	}
	if s.SourceStagnant.IsValue() {
		vals = append(vals, "source_stagnant")
	}
	if s.SourceGutter.IsValue() {
		vals = append(vals, "source_gutter")
	}
	if s.TodEarly.IsValue() {
		vals = append(vals, "tod_early")
	}
	if s.TodDay.IsValue() {
		vals = append(vals, "tod_day")
	}
	if s.TodEvening.IsValue() {
		vals = append(vals, "tod_evening")
	}
	if s.TodNight.IsValue() {
		vals = append(vals, "tod_night")
	}
	return vals
}

func (s PublicreportNuisanceSetter) Overwrite(t *PublicreportNuisance) {
	if s.AdditionalInfo.IsValue() {
		t.AdditionalInfo = s.AdditionalInfo.MustGet()
	}
	if s.Duration.IsValue() {
		t.Duration = s.Duration.MustGet()
	}
	if s.IsLocationBackyard.IsValue() {
		t.IsLocationBackyard = s.IsLocationBackyard.MustGet()
	}
	if s.IsLocationFrontyard.IsValue() {
		t.IsLocationFrontyard = s.IsLocationFrontyard.MustGet()
	}
	if s.IsLocationGarden.IsValue() {
		t.IsLocationGarden = s.IsLocationGarden.MustGet()
	}
	if s.IsLocationOther.IsValue() {
		t.IsLocationOther = s.IsLocationOther.MustGet()
	}
	if s.IsLocationPool.IsValue() {
		t.IsLocationPool = s.IsLocationPool.MustGet()
	}
	if s.ReportID.IsValue() {
		t.ReportID = s.ReportID.MustGet()
	}
	if s.SourceContainer.IsValue() {
		t.SourceContainer = s.SourceContainer.MustGet()
	}
	if s.SourceDescription.IsValue() {
		t.SourceDescription = s.SourceDescription.MustGet()
	}
	if s.SourceStagnant.IsValue() {
		t.SourceStagnant = s.SourceStagnant.MustGet()
	}
	if s.SourceGutter.IsValue() {
		t.SourceGutter = s.SourceGutter.MustGet()
	}
	if s.TodEarly.IsValue() {
		t.TodEarly = s.TodEarly.MustGet()
	}
	if s.TodDay.IsValue() {
		t.TodDay = s.TodDay.MustGet()
	}
	if s.TodEvening.IsValue() {
		t.TodEvening = s.TodEvening.MustGet()
	}
	if s.TodNight.IsValue() {
		t.TodNight = s.TodNight.MustGet()
	}
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return exprs
}

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

	return PublicreportNuisances.Query(
		sm.Where(PublicreportNuisances.Columns.ReportID.EQ(psql.Arg(ReportIDPK))),
		sm.Columns(PublicreportNuisances.Columns.Only(cols...)),
	).One(ctx, exec)
}

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

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

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

	return err
}

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

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

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

	return nil
}

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

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

	return err
}

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

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

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

			return err
		}))

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

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

			return err
		}))

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

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

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

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

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

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

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

	o.copyMatchingRows(o2...)

	return nil
}

// Report starts a query for related objects on publicreport.report
func (o *PublicreportNuisance) Report(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportReportsQuery {
	return PublicreportReports.Query(append(mods,
		sm.Where(PublicreportReports.Columns.ID.EQ(psql.Arg(o.ReportID))),
	)...)
}

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

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

func attachPublicreportNuisanceReport0(ctx context.Context, exec bob.Executor, count int, publicreportNuisance0 *PublicreportNuisance, publicreportReport1 *PublicreportReport) (*PublicreportNuisance, error) {
	setter := &PublicreportNuisanceSetter{
		ReportID: omit.From(publicreportReport1.ID),
	}

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

	return publicreportNuisance0, nil
}

func (publicreportNuisance0 *PublicreportNuisance) InsertReport(ctx context.Context, exec bob.Executor, related *PublicreportReportSetter) error {
	var err error

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

	_, err = attachPublicreportNuisanceReport0(ctx, exec, 1, publicreportNuisance0, publicreportReport1)
	if err != nil {
		return err
	}

	publicreportNuisance0.R.Report = publicreportReport1

	publicreportReport1.R.Nuisance = publicreportNuisance0

	return nil
}

func (publicreportNuisance0 *PublicreportNuisance) AttachReport(ctx context.Context, exec bob.Executor, publicreportReport1 *PublicreportReport) error {
	var err error

	_, err = attachPublicreportNuisanceReport0(ctx, exec, 1, publicreportNuisance0, publicreportReport1)
	if err != nil {
		return err
	}

	publicreportNuisance0.R.Report = publicreportReport1

	publicreportReport1.R.Nuisance = publicreportNuisance0

	return nil
}

type publicreportNuisanceWhere[Q psql.Filterable] struct {
	AdditionalInfo      psql.WhereMod[Q, string]
	Duration            psql.WhereMod[Q, enums.PublicreportNuisancedurationtype]
	IsLocationBackyard  psql.WhereMod[Q, bool]
	IsLocationFrontyard psql.WhereMod[Q, bool]
	IsLocationGarden    psql.WhereMod[Q, bool]
	IsLocationOther     psql.WhereMod[Q, bool]
	IsLocationPool      psql.WhereMod[Q, bool]
	ReportID            psql.WhereMod[Q, int32]
	SourceContainer     psql.WhereMod[Q, bool]
	SourceDescription   psql.WhereMod[Q, string]
	SourceStagnant      psql.WhereMod[Q, bool]
	SourceGutter        psql.WhereMod[Q, bool]
	TodEarly            psql.WhereMod[Q, bool]
	TodDay              psql.WhereMod[Q, bool]
	TodEvening          psql.WhereMod[Q, bool]
	TodNight            psql.WhereMod[Q, bool]
}

func (publicreportNuisanceWhere[Q]) AliasedAs(alias string) publicreportNuisanceWhere[Q] {
	return buildPublicreportNuisanceWhere[Q](buildPublicreportNuisanceColumns(alias))
}

func buildPublicreportNuisanceWhere[Q psql.Filterable](cols publicreportNuisanceColumns) publicreportNuisanceWhere[Q] {
	return publicreportNuisanceWhere[Q]{
		AdditionalInfo:      psql.Where[Q, string](cols.AdditionalInfo),
		Duration:            psql.Where[Q, enums.PublicreportNuisancedurationtype](cols.Duration),
		IsLocationBackyard:  psql.Where[Q, bool](cols.IsLocationBackyard),
		IsLocationFrontyard: psql.Where[Q, bool](cols.IsLocationFrontyard),
		IsLocationGarden:    psql.Where[Q, bool](cols.IsLocationGarden),
		IsLocationOther:     psql.Where[Q, bool](cols.IsLocationOther),
		IsLocationPool:      psql.Where[Q, bool](cols.IsLocationPool),
		ReportID:            psql.Where[Q, int32](cols.ReportID),
		SourceContainer:     psql.Where[Q, bool](cols.SourceContainer),
		SourceDescription:   psql.Where[Q, string](cols.SourceDescription),
		SourceStagnant:      psql.Where[Q, bool](cols.SourceStagnant),
		SourceGutter:        psql.Where[Q, bool](cols.SourceGutter),
		TodEarly:            psql.Where[Q, bool](cols.TodEarly),
		TodDay:              psql.Where[Q, bool](cols.TodDay),
		TodEvening:          psql.Where[Q, bool](cols.TodEvening),
		TodNight:            psql.Where[Q, bool](cols.TodNight),
	}
}

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

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

		o.R.Report = rel

		if rel != nil {
			rel.R.Nuisance = o
		}
		return nil
	default:
		return fmt.Errorf("publicreportNuisance has no relationship %q", name)
	}
}

type publicreportNuisancePreloader struct {
	Report func(...psql.PreloadOption) psql.Preloader
}

func buildPublicreportNuisancePreloader() publicreportNuisancePreloader {
	return publicreportNuisancePreloader{
		Report: func(opts ...psql.PreloadOption) psql.Preloader {
			return psql.Preload[*PublicreportReport, PublicreportReportSlice](psql.PreloadRel{
				Name: "Report",
				Sides: []psql.PreloadSide{
					{
						From:        PublicreportNuisances,
						To:          PublicreportReports,
						FromColumns: []string{"report_id"},
						ToColumns:   []string{"id"},
					},
				},
			}, PublicreportReports.Columns.Names(), opts...)
		},
	}
}

type publicreportNuisanceThenLoader[Q orm.Loadable] struct {
	Report func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
}

func buildPublicreportNuisanceThenLoader[Q orm.Loadable]() publicreportNuisanceThenLoader[Q] {
	type ReportLoadInterface interface {
		LoadReport(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}

	return publicreportNuisanceThenLoader[Q]{
		Report: thenLoadBuilder[Q](
			"Report",
			func(ctx context.Context, exec bob.Executor, retrieved ReportLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadReport(ctx, exec, mods...)
			},
		),
	}
}

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

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

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

	related.R.Nuisance = o

	o.R.Report = related
	return nil
}

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

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

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

		for _, rel := range publicreportReports {

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

			rel.R.Nuisance = o

			o.R.Report = rel
			break
		}
	}

	return nil
}
