// 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/omit"
	"github.com/google/uuid"
)

// NoteAudioBreadcrumb is an object representing the database table.
type NoteAudioBreadcrumb struct {
	Cell             string    `db:"cell" `
	Created          time.Time `db:"created" `
	ManuallySelected bool      `db:"manually_selected" `
	NoteAudioVersion int32     `db:"note_audio_version,pk" `
	NoteAudioUUID    uuid.UUID `db:"note_audio_uuid,pk" `
	Position         int32     `db:"position,pk" `

	R noteAudioBreadcrumbR `db:"-" `
}

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

// NoteAudioBreadcrumbs contains methods to work with the note_audio_breadcrumb table
var NoteAudioBreadcrumbs = psql.NewTablex[*NoteAudioBreadcrumb, NoteAudioBreadcrumbSlice, *NoteAudioBreadcrumbSetter]("", "note_audio_breadcrumb", buildNoteAudioBreadcrumbColumns("note_audio_breadcrumb"))

// NoteAudioBreadcrumbsQuery is a query on the note_audio_breadcrumb table
type NoteAudioBreadcrumbsQuery = *psql.ViewQuery[*NoteAudioBreadcrumb, NoteAudioBreadcrumbSlice]

// noteAudioBreadcrumbR is where relationships are stored.
type noteAudioBreadcrumbR struct {
	NoteAudio *NoteAudio // note_audio_breadcrumb.note_audio_breadcrumb_note_audio_version_note_audio_uuid_fkey
}

func buildNoteAudioBreadcrumbColumns(alias string) noteAudioBreadcrumbColumns {
	return noteAudioBreadcrumbColumns{
		ColumnsExpr: expr.NewColumnsExpr(
			"cell", "created", "manually_selected", "note_audio_version", "note_audio_uuid", "position",
		).WithParent("note_audio_breadcrumb"),
		tableAlias:       alias,
		Cell:             psql.Quote(alias, "cell"),
		Created:          psql.Quote(alias, "created"),
		ManuallySelected: psql.Quote(alias, "manually_selected"),
		NoteAudioVersion: psql.Quote(alias, "note_audio_version"),
		NoteAudioUUID:    psql.Quote(alias, "note_audio_uuid"),
		Position:         psql.Quote(alias, "position"),
	}
}

type noteAudioBreadcrumbColumns struct {
	expr.ColumnsExpr
	tableAlias       string
	Cell             psql.Expression
	Created          psql.Expression
	ManuallySelected psql.Expression
	NoteAudioVersion psql.Expression
	NoteAudioUUID    psql.Expression
	Position         psql.Expression
}

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

func (noteAudioBreadcrumbColumns) AliasedAs(alias string) noteAudioBreadcrumbColumns {
	return buildNoteAudioBreadcrumbColumns(alias)
}

// NoteAudioBreadcrumbSetter is used for insert/upsert/update operations
// All values are optional, and do not have to be set
// Generated columns are not included
type NoteAudioBreadcrumbSetter struct {
	Cell             omit.Val[string]    `db:"cell" `
	Created          omit.Val[time.Time] `db:"created" `
	ManuallySelected omit.Val[bool]      `db:"manually_selected" `
	NoteAudioVersion omit.Val[int32]     `db:"note_audio_version,pk" `
	NoteAudioUUID    omit.Val[uuid.UUID] `db:"note_audio_uuid,pk" `
	Position         omit.Val[int32]     `db:"position,pk" `
}

func (s NoteAudioBreadcrumbSetter) SetColumns() []string {
	vals := make([]string, 0, 6)
	if s.Cell.IsValue() {
		vals = append(vals, "cell")
	}
	if s.Created.IsValue() {
		vals = append(vals, "created")
	}
	if s.ManuallySelected.IsValue() {
		vals = append(vals, "manually_selected")
	}
	if s.NoteAudioVersion.IsValue() {
		vals = append(vals, "note_audio_version")
	}
	if s.NoteAudioUUID.IsValue() {
		vals = append(vals, "note_audio_uuid")
	}
	if s.Position.IsValue() {
		vals = append(vals, "position")
	}
	return vals
}

func (s NoteAudioBreadcrumbSetter) Overwrite(t *NoteAudioBreadcrumb) {
	if s.Cell.IsValue() {
		t.Cell = s.Cell.MustGet()
	}
	if s.Created.IsValue() {
		t.Created = s.Created.MustGet()
	}
	if s.ManuallySelected.IsValue() {
		t.ManuallySelected = s.ManuallySelected.MustGet()
	}
	if s.NoteAudioVersion.IsValue() {
		t.NoteAudioVersion = s.NoteAudioVersion.MustGet()
	}
	if s.NoteAudioUUID.IsValue() {
		t.NoteAudioUUID = s.NoteAudioUUID.MustGet()
	}
	if s.Position.IsValue() {
		t.Position = s.Position.MustGet()
	}
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return exprs
}

// FindNoteAudioBreadcrumb retrieves a single record by primary key
// If cols is empty Find will return all columns.
func FindNoteAudioBreadcrumb(ctx context.Context, exec bob.Executor, NoteAudioVersionPK int32, NoteAudioUUIDPK uuid.UUID, PositionPK int32, cols ...string) (*NoteAudioBreadcrumb, error) {
	if len(cols) == 0 {
		return NoteAudioBreadcrumbs.Query(
			sm.Where(NoteAudioBreadcrumbs.Columns.NoteAudioVersion.EQ(psql.Arg(NoteAudioVersionPK))),
			sm.Where(NoteAudioBreadcrumbs.Columns.NoteAudioUUID.EQ(psql.Arg(NoteAudioUUIDPK))),
			sm.Where(NoteAudioBreadcrumbs.Columns.Position.EQ(psql.Arg(PositionPK))),
		).One(ctx, exec)
	}

	return NoteAudioBreadcrumbs.Query(
		sm.Where(NoteAudioBreadcrumbs.Columns.NoteAudioVersion.EQ(psql.Arg(NoteAudioVersionPK))),
		sm.Where(NoteAudioBreadcrumbs.Columns.NoteAudioUUID.EQ(psql.Arg(NoteAudioUUIDPK))),
		sm.Where(NoteAudioBreadcrumbs.Columns.Position.EQ(psql.Arg(PositionPK))),
		sm.Columns(NoteAudioBreadcrumbs.Columns.Only(cols...)),
	).One(ctx, exec)
}

// NoteAudioBreadcrumbExists checks the presence of a single record by primary key
func NoteAudioBreadcrumbExists(ctx context.Context, exec bob.Executor, NoteAudioVersionPK int32, NoteAudioUUIDPK uuid.UUID, PositionPK int32) (bool, error) {
	return NoteAudioBreadcrumbs.Query(
		sm.Where(NoteAudioBreadcrumbs.Columns.NoteAudioVersion.EQ(psql.Arg(NoteAudioVersionPK))),
		sm.Where(NoteAudioBreadcrumbs.Columns.NoteAudioUUID.EQ(psql.Arg(NoteAudioUUIDPK))),
		sm.Where(NoteAudioBreadcrumbs.Columns.Position.EQ(psql.Arg(PositionPK))),
	).Exists(ctx, exec)
}

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

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

	return err
}

// primaryKeyVals returns the primary key values of the NoteAudioBreadcrumb
func (o *NoteAudioBreadcrumb) primaryKeyVals() bob.Expression {
	return psql.ArgGroup(
		o.NoteAudioVersion,
		o.NoteAudioUUID,
		o.Position,
	)
}

func (o *NoteAudioBreadcrumb) pkEQ() dialect.Expression {
	return psql.Group(psql.Quote("note_audio_breadcrumb", "note_audio_version"), psql.Quote("note_audio_breadcrumb", "note_audio_uuid"), psql.Quote("note_audio_breadcrumb", "position")).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 NoteAudioBreadcrumb
func (o *NoteAudioBreadcrumb) Update(ctx context.Context, exec bob.Executor, s *NoteAudioBreadcrumbSetter) error {
	v, err := NoteAudioBreadcrumbs.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 NoteAudioBreadcrumb record with an executor
func (o *NoteAudioBreadcrumb) Delete(ctx context.Context, exec bob.Executor) error {
	_, err := NoteAudioBreadcrumbs.Delete(dm.Where(o.pkEQ())).Exec(ctx, exec)
	return err
}

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

	return nil
}

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

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

	return err
}

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

	return psql.Group(psql.Quote("note_audio_breadcrumb", "note_audio_version"), psql.Quote("note_audio_breadcrumb", "note_audio_uuid"), psql.Quote("note_audio_breadcrumb", "position")).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 NoteAudioBreadcrumbSlice) copyMatchingRows(from ...*NoteAudioBreadcrumb) {
	for i, old := range o {
		for _, new := range from {
			if new.NoteAudioVersion != old.NoteAudioVersion {
				continue
			}
			if new.NoteAudioUUID != old.NoteAudioUUID {
				continue
			}
			if new.Position != old.Position {
				continue
			}
			new.R = old.R
			o[i] = new
			break
		}
	}
}

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

			return err
		}))

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

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

			return err
		}))

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

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

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

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

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

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

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

	o.copyMatchingRows(o2...)

	return nil
}

// NoteAudio starts a query for related objects on note_audio
func (o *NoteAudioBreadcrumb) NoteAudio(mods ...bob.Mod[*dialect.SelectQuery]) NoteAudiosQuery {
	return NoteAudios.Query(append(mods,
		sm.Where(NoteAudios.Columns.Version.EQ(psql.Arg(o.NoteAudioVersion))), sm.Where(NoteAudios.Columns.UUID.EQ(psql.Arg(o.NoteAudioUUID))),
	)...)
}

func (os NoteAudioBreadcrumbSlice) NoteAudio(mods ...bob.Mod[*dialect.SelectQuery]) NoteAudiosQuery {
	pkNoteAudioVersion := make(pgtypes.Array[int32], 0, len(os))

	pkNoteAudioUUID := make(pgtypes.Array[uuid.UUID], 0, len(os))
	for _, o := range os {
		if o == nil {
			continue
		}
		pkNoteAudioVersion = append(pkNoteAudioVersion, o.NoteAudioVersion)
		pkNoteAudioUUID = append(pkNoteAudioUUID, o.NoteAudioUUID)
	}
	PKArgExpr := psql.Select(sm.Columns(
		psql.F("unnest", psql.Cast(psql.Arg(pkNoteAudioVersion), "integer[]")),
		psql.F("unnest", psql.Cast(psql.Arg(pkNoteAudioUUID), "uuid[]")),
	))

	return NoteAudios.Query(append(mods,
		sm.Where(psql.Group(NoteAudios.Columns.Version, NoteAudios.Columns.UUID).OP("IN", PKArgExpr)),
	)...)
}

func attachNoteAudioBreadcrumbNoteAudio0(ctx context.Context, exec bob.Executor, count int, noteAudioBreadcrumb0 *NoteAudioBreadcrumb, noteAudio1 *NoteAudio) (*NoteAudioBreadcrumb, error) {
	setter := &NoteAudioBreadcrumbSetter{
		NoteAudioVersion: omit.From(noteAudio1.Version),
		NoteAudioUUID:    omit.From(noteAudio1.UUID),
	}

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

	return noteAudioBreadcrumb0, nil
}

func (noteAudioBreadcrumb0 *NoteAudioBreadcrumb) InsertNoteAudio(ctx context.Context, exec bob.Executor, related *NoteAudioSetter) error {
	var err error

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

	_, err = attachNoteAudioBreadcrumbNoteAudio0(ctx, exec, 1, noteAudioBreadcrumb0, noteAudio1)
	if err != nil {
		return err
	}

	noteAudioBreadcrumb0.R.NoteAudio = noteAudio1

	noteAudio1.R.NoteAudioBreadcrumbs = append(noteAudio1.R.NoteAudioBreadcrumbs, noteAudioBreadcrumb0)

	return nil
}

func (noteAudioBreadcrumb0 *NoteAudioBreadcrumb) AttachNoteAudio(ctx context.Context, exec bob.Executor, noteAudio1 *NoteAudio) error {
	var err error

	_, err = attachNoteAudioBreadcrumbNoteAudio0(ctx, exec, 1, noteAudioBreadcrumb0, noteAudio1)
	if err != nil {
		return err
	}

	noteAudioBreadcrumb0.R.NoteAudio = noteAudio1

	noteAudio1.R.NoteAudioBreadcrumbs = append(noteAudio1.R.NoteAudioBreadcrumbs, noteAudioBreadcrumb0)

	return nil
}

type noteAudioBreadcrumbWhere[Q psql.Filterable] struct {
	Cell             psql.WhereMod[Q, string]
	Created          psql.WhereMod[Q, time.Time]
	ManuallySelected psql.WhereMod[Q, bool]
	NoteAudioVersion psql.WhereMod[Q, int32]
	NoteAudioUUID    psql.WhereMod[Q, uuid.UUID]
	Position         psql.WhereMod[Q, int32]
}

func (noteAudioBreadcrumbWhere[Q]) AliasedAs(alias string) noteAudioBreadcrumbWhere[Q] {
	return buildNoteAudioBreadcrumbWhere[Q](buildNoteAudioBreadcrumbColumns(alias))
}

func buildNoteAudioBreadcrumbWhere[Q psql.Filterable](cols noteAudioBreadcrumbColumns) noteAudioBreadcrumbWhere[Q] {
	return noteAudioBreadcrumbWhere[Q]{
		Cell:             psql.Where[Q, string](cols.Cell),
		Created:          psql.Where[Q, time.Time](cols.Created),
		ManuallySelected: psql.Where[Q, bool](cols.ManuallySelected),
		NoteAudioVersion: psql.Where[Q, int32](cols.NoteAudioVersion),
		NoteAudioUUID:    psql.Where[Q, uuid.UUID](cols.NoteAudioUUID),
		Position:         psql.Where[Q, int32](cols.Position),
	}
}

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

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

		o.R.NoteAudio = rel

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

type noteAudioBreadcrumbPreloader struct {
	NoteAudio func(...psql.PreloadOption) psql.Preloader
}

func buildNoteAudioBreadcrumbPreloader() noteAudioBreadcrumbPreloader {
	return noteAudioBreadcrumbPreloader{
		NoteAudio: func(opts ...psql.PreloadOption) psql.Preloader {
			return psql.Preload[*NoteAudio, NoteAudioSlice](psql.PreloadRel{
				Name: "NoteAudio",
				Sides: []psql.PreloadSide{
					{
						From:        NoteAudioBreadcrumbs,
						To:          NoteAudios,
						FromColumns: []string{"note_audio_version", "note_audio_uuid"},
						ToColumns:   []string{"version", "uuid"},
					},
				},
			}, NoteAudios.Columns.Names(), opts...)
		},
	}
}

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

func buildNoteAudioBreadcrumbThenLoader[Q orm.Loadable]() noteAudioBreadcrumbThenLoader[Q] {
	type NoteAudioLoadInterface interface {
		LoadNoteAudio(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}

	return noteAudioBreadcrumbThenLoader[Q]{
		NoteAudio: thenLoadBuilder[Q](
			"NoteAudio",
			func(ctx context.Context, exec bob.Executor, retrieved NoteAudioLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadNoteAudio(ctx, exec, mods...)
			},
		),
	}
}

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

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

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

	related.R.NoteAudioBreadcrumbs = NoteAudioBreadcrumbSlice{o}

	o.R.NoteAudio = related
	return nil
}

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

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

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

		for _, rel := range noteAudios {

			if !(o.NoteAudioVersion == rel.Version) {
				continue
			}

			if !(o.NoteAudioUUID == rel.UUID) {
				continue
			}

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

			o.R.NoteAudio = rel
			break
		}
	}

	return nil
}
