// 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"
	"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/orm"
	"github.com/Gleipnir-Technology/bob/types/pgtypes"
	"github.com/aarondl/opt/null"
	"github.com/aarondl/opt/omit"
	"github.com/aarondl/opt/omitnull"
	enums "source.gleipnir.technology/Gleipnir/nidus-sync/db/enums"
)

// PublicreportCompliance is an object representing the database table.
type PublicreportCompliance struct {
	AccessInstructions string                             `db:"access_instructions" `
	AvailabilityNotes  string                             `db:"availability_notes" `
	Comments           string                             `db:"comments" `
	GateCode           string                             `db:"gate_code" `
	HasDog             null.Val[bool]                     `db:"has_dog" `
	PermissionType     enums.PublicreportPermissionaccess `db:"permission_type" `
	ReportID           int32                              `db:"report_id,pk" `
	ReportPhoneCanText null.Val[bool]                     `db:"report_phone_can_text" `
	WantsScheduled     null.Val[bool]                     `db:"wants_scheduled" `
	Submitted          null.Val[time.Time]                `db:"submitted" `

	R publicreportComplianceR `db:"-" `
}

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

// PublicreportCompliances contains methods to work with the compliance table
var PublicreportCompliances = psql.NewTablex[*PublicreportCompliance, PublicreportComplianceSlice, *PublicreportComplianceSetter]("publicreport", "compliance", buildPublicreportComplianceColumns("publicreport.compliance"))

// PublicreportCompliancesQuery is a query on the compliance table
type PublicreportCompliancesQuery = *psql.ViewQuery[*PublicreportCompliance, PublicreportComplianceSlice]

// publicreportComplianceR is where relationships are stored.
type publicreportComplianceR struct {
	Report *PublicreportReport // publicreport.compliance.compliance_report_id_fkey
}

func buildPublicreportComplianceColumns(alias string) publicreportComplianceColumns {
	return publicreportComplianceColumns{
		ColumnsExpr: expr.NewColumnsExpr(
			"access_instructions", "availability_notes", "comments", "gate_code", "has_dog", "permission_type", "report_id", "report_phone_can_text", "wants_scheduled", "submitted",
		).WithParent("publicreport.compliance"),
		tableAlias:         alias,
		AccessInstructions: psql.Quote(alias, "access_instructions"),
		AvailabilityNotes:  psql.Quote(alias, "availability_notes"),
		Comments:           psql.Quote(alias, "comments"),
		GateCode:           psql.Quote(alias, "gate_code"),
		HasDog:             psql.Quote(alias, "has_dog"),
		PermissionType:     psql.Quote(alias, "permission_type"),
		ReportID:           psql.Quote(alias, "report_id"),
		ReportPhoneCanText: psql.Quote(alias, "report_phone_can_text"),
		WantsScheduled:     psql.Quote(alias, "wants_scheduled"),
		Submitted:          psql.Quote(alias, "submitted"),
	}
}

type publicreportComplianceColumns struct {
	expr.ColumnsExpr
	tableAlias         string
	AccessInstructions psql.Expression
	AvailabilityNotes  psql.Expression
	Comments           psql.Expression
	GateCode           psql.Expression
	HasDog             psql.Expression
	PermissionType     psql.Expression
	ReportID           psql.Expression
	ReportPhoneCanText psql.Expression
	WantsScheduled     psql.Expression
	Submitted          psql.Expression
}

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

func (publicreportComplianceColumns) AliasedAs(alias string) publicreportComplianceColumns {
	return buildPublicreportComplianceColumns(alias)
}

// PublicreportComplianceSetter is used for insert/upsert/update operations
// All values are optional, and do not have to be set
// Generated columns are not included
type PublicreportComplianceSetter struct {
	AccessInstructions omit.Val[string]                             `db:"access_instructions" `
	AvailabilityNotes  omit.Val[string]                             `db:"availability_notes" `
	Comments           omit.Val[string]                             `db:"comments" `
	GateCode           omit.Val[string]                             `db:"gate_code" `
	HasDog             omitnull.Val[bool]                           `db:"has_dog" `
	PermissionType     omit.Val[enums.PublicreportPermissionaccess] `db:"permission_type" `
	ReportID           omit.Val[int32]                              `db:"report_id,pk" `
	ReportPhoneCanText omitnull.Val[bool]                           `db:"report_phone_can_text" `
	WantsScheduled     omitnull.Val[bool]                           `db:"wants_scheduled" `
	Submitted          omitnull.Val[time.Time]                      `db:"submitted" `
}

func (s PublicreportComplianceSetter) SetColumns() []string {
	vals := make([]string, 0, 10)
	if s.AccessInstructions.IsValue() {
		vals = append(vals, "access_instructions")
	}
	if s.AvailabilityNotes.IsValue() {
		vals = append(vals, "availability_notes")
	}
	if s.Comments.IsValue() {
		vals = append(vals, "comments")
	}
	if s.GateCode.IsValue() {
		vals = append(vals, "gate_code")
	}
	if !s.HasDog.IsUnset() {
		vals = append(vals, "has_dog")
	}
	if s.PermissionType.IsValue() {
		vals = append(vals, "permission_type")
	}
	if s.ReportID.IsValue() {
		vals = append(vals, "report_id")
	}
	if !s.ReportPhoneCanText.IsUnset() {
		vals = append(vals, "report_phone_can_text")
	}
	if !s.WantsScheduled.IsUnset() {
		vals = append(vals, "wants_scheduled")
	}
	if !s.Submitted.IsUnset() {
		vals = append(vals, "submitted")
	}
	return vals
}

func (s PublicreportComplianceSetter) Overwrite(t *PublicreportCompliance) {
	if s.AccessInstructions.IsValue() {
		t.AccessInstructions = s.AccessInstructions.MustGet()
	}
	if s.AvailabilityNotes.IsValue() {
		t.AvailabilityNotes = s.AvailabilityNotes.MustGet()
	}
	if s.Comments.IsValue() {
		t.Comments = s.Comments.MustGet()
	}
	if s.GateCode.IsValue() {
		t.GateCode = s.GateCode.MustGet()
	}
	if !s.HasDog.IsUnset() {
		t.HasDog = s.HasDog.MustGetNull()
	}
	if s.PermissionType.IsValue() {
		t.PermissionType = s.PermissionType.MustGet()
	}
	if s.ReportID.IsValue() {
		t.ReportID = s.ReportID.MustGet()
	}
	if !s.ReportPhoneCanText.IsUnset() {
		t.ReportPhoneCanText = s.ReportPhoneCanText.MustGetNull()
	}
	if !s.WantsScheduled.IsUnset() {
		t.WantsScheduled = s.WantsScheduled.MustGetNull()
	}
	if !s.Submitted.IsUnset() {
		t.Submitted = s.Submitted.MustGetNull()
	}
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return exprs
}

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

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

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

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

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

	return err
}

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

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

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

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

	return err
}

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

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

			return err
		}))

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

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

			return err
		}))

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

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

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

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

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

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

	o2, err := PublicreportCompliances.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 *PublicreportCompliance) Report(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportReportsQuery {
	return PublicreportReports.Query(append(mods,
		sm.Where(PublicreportReports.Columns.ID.EQ(psql.Arg(o.ReportID))),
	)...)
}

func (os PublicreportComplianceSlice) 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 attachPublicreportComplianceReport0(ctx context.Context, exec bob.Executor, count int, publicreportCompliance0 *PublicreportCompliance, publicreportReport1 *PublicreportReport) (*PublicreportCompliance, error) {
	setter := &PublicreportComplianceSetter{
		ReportID: omit.From(publicreportReport1.ID),
	}

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

	return publicreportCompliance0, nil
}

func (publicreportCompliance0 *PublicreportCompliance) 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 = attachPublicreportComplianceReport0(ctx, exec, 1, publicreportCompliance0, publicreportReport1)
	if err != nil {
		return err
	}

	publicreportCompliance0.R.Report = publicreportReport1

	publicreportReport1.R.Compliance = publicreportCompliance0

	return nil
}

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

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

	publicreportCompliance0.R.Report = publicreportReport1

	publicreportReport1.R.Compliance = publicreportCompliance0

	return nil
}

type publicreportComplianceWhere[Q psql.Filterable] struct {
	AccessInstructions psql.WhereMod[Q, string]
	AvailabilityNotes  psql.WhereMod[Q, string]
	Comments           psql.WhereMod[Q, string]
	GateCode           psql.WhereMod[Q, string]
	HasDog             psql.WhereNullMod[Q, bool]
	PermissionType     psql.WhereMod[Q, enums.PublicreportPermissionaccess]
	ReportID           psql.WhereMod[Q, int32]
	ReportPhoneCanText psql.WhereNullMod[Q, bool]
	WantsScheduled     psql.WhereNullMod[Q, bool]
	Submitted          psql.WhereNullMod[Q, time.Time]
}

func (publicreportComplianceWhere[Q]) AliasedAs(alias string) publicreportComplianceWhere[Q] {
	return buildPublicreportComplianceWhere[Q](buildPublicreportComplianceColumns(alias))
}

func buildPublicreportComplianceWhere[Q psql.Filterable](cols publicreportComplianceColumns) publicreportComplianceWhere[Q] {
	return publicreportComplianceWhere[Q]{
		AccessInstructions: psql.Where[Q, string](cols.AccessInstructions),
		AvailabilityNotes:  psql.Where[Q, string](cols.AvailabilityNotes),
		Comments:           psql.Where[Q, string](cols.Comments),
		GateCode:           psql.Where[Q, string](cols.GateCode),
		HasDog:             psql.WhereNull[Q, bool](cols.HasDog),
		PermissionType:     psql.Where[Q, enums.PublicreportPermissionaccess](cols.PermissionType),
		ReportID:           psql.Where[Q, int32](cols.ReportID),
		ReportPhoneCanText: psql.WhereNull[Q, bool](cols.ReportPhoneCanText),
		WantsScheduled:     psql.WhereNull[Q, bool](cols.WantsScheduled),
		Submitted:          psql.WhereNull[Q, time.Time](cols.Submitted),
	}
}

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

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

		o.R.Report = rel

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

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

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

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

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

	return publicreportComplianceThenLoader[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 publicreportCompliance's Report into the .R struct
func (o *PublicreportCompliance) 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.Compliance = o

	o.R.Report = related
	return nil
}

// LoadReport loads the publicreportCompliance's Report into the .R struct
func (os PublicreportComplianceSlice) 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.Compliance = o

			o.R.Report = rel
			break
		}
	}

	return nil
}
