// 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"
	enums "source.gleipnir.technology/Gleipnir/nidus-sync/db/enums"
)

// NoteAudioDatum is an object representing the database table.
type NoteAudioDatum struct {
	Created          time.Time           `db:"created" `
	NoteAudioVersion int32               `db:"note_audio_version,pk" `
	NoteAudioUUID    uuid.UUID           `db:"note_audio_uuid,pk" `
	Type             enums.Audiodatatype `db:"type_,pk" `

	R noteAudioDatumR `db:"-" `
}

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

// NoteAudioData contains methods to work with the note_audio_data table
var NoteAudioData = psql.NewTablex[*NoteAudioDatum, NoteAudioDatumSlice, *NoteAudioDatumSetter]("", "note_audio_data", buildNoteAudioDatumColumns("note_audio_data"))

// NoteAudioDataQuery is a query on the note_audio_data table
type NoteAudioDataQuery = *psql.ViewQuery[*NoteAudioDatum, NoteAudioDatumSlice]

// noteAudioDatumR is where relationships are stored.
type noteAudioDatumR struct {
	NoteAudio *NoteAudio // note_audio_data.note_audio_data_note_audio_version_note_audio_uuid_fkey
}

func buildNoteAudioDatumColumns(alias string) noteAudioDatumColumns {
	return noteAudioDatumColumns{
		ColumnsExpr: expr.NewColumnsExpr(
			"created", "note_audio_version", "note_audio_uuid", "type_",
		).WithParent("note_audio_data"),
		tableAlias:       alias,
		Created:          psql.Quote(alias, "created"),
		NoteAudioVersion: psql.Quote(alias, "note_audio_version"),
		NoteAudioUUID:    psql.Quote(alias, "note_audio_uuid"),
		Type:             psql.Quote(alias, "type_"),
	}
}

type noteAudioDatumColumns struct {
	expr.ColumnsExpr
	tableAlias       string
	Created          psql.Expression
	NoteAudioVersion psql.Expression
	NoteAudioUUID    psql.Expression
	Type             psql.Expression
}

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

func (noteAudioDatumColumns) AliasedAs(alias string) noteAudioDatumColumns {
	return buildNoteAudioDatumColumns(alias)
}

// NoteAudioDatumSetter is used for insert/upsert/update operations
// All values are optional, and do not have to be set
// Generated columns are not included
type NoteAudioDatumSetter struct {
	Created          omit.Val[time.Time]           `db:"created" `
	NoteAudioVersion omit.Val[int32]               `db:"note_audio_version,pk" `
	NoteAudioUUID    omit.Val[uuid.UUID]           `db:"note_audio_uuid,pk" `
	Type             omit.Val[enums.Audiodatatype] `db:"type_,pk" `
}

func (s NoteAudioDatumSetter) SetColumns() []string {
	vals := make([]string, 0, 4)
	if s.Created.IsValue() {
		vals = append(vals, "created")
	}
	if s.NoteAudioVersion.IsValue() {
		vals = append(vals, "note_audio_version")
	}
	if s.NoteAudioUUID.IsValue() {
		vals = append(vals, "note_audio_uuid")
	}
	if s.Type.IsValue() {
		vals = append(vals, "type_")
	}
	return vals
}

func (s NoteAudioDatumSetter) Overwrite(t *NoteAudioDatum) {
	if s.Created.IsValue() {
		t.Created = s.Created.MustGet()
	}
	if s.NoteAudioVersion.IsValue() {
		t.NoteAudioVersion = s.NoteAudioVersion.MustGet()
	}
	if s.NoteAudioUUID.IsValue() {
		t.NoteAudioUUID = s.NoteAudioUUID.MustGet()
	}
	if s.Type.IsValue() {
		t.Type = s.Type.MustGet()
	}
}

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

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

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

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

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

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

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

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

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

	return exprs
}

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

	return NoteAudioData.Query(
		sm.Where(NoteAudioData.Columns.NoteAudioVersion.EQ(psql.Arg(NoteAudioVersionPK))),
		sm.Where(NoteAudioData.Columns.NoteAudioUUID.EQ(psql.Arg(NoteAudioUUIDPK))),
		sm.Where(NoteAudioData.Columns.Type.EQ(psql.Arg(TypePK))),
		sm.Columns(NoteAudioData.Columns.Only(cols...)),
	).One(ctx, exec)
}

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

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

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

	return err
}

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

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

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

	return nil
}

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

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

	return err
}

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

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

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

			return err
		}))

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

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

			return err
		}))

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

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

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

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

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

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

	o2, err := NoteAudioData.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 *NoteAudioDatum) 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 NoteAudioDatumSlice) 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 attachNoteAudioDatumNoteAudio0(ctx context.Context, exec bob.Executor, count int, noteAudioDatum0 *NoteAudioDatum, noteAudio1 *NoteAudio) (*NoteAudioDatum, error) {
	setter := &NoteAudioDatumSetter{
		NoteAudioVersion: omit.From(noteAudio1.Version),
		NoteAudioUUID:    omit.From(noteAudio1.UUID),
	}

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

	return noteAudioDatum0, nil
}

func (noteAudioDatum0 *NoteAudioDatum) 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 = attachNoteAudioDatumNoteAudio0(ctx, exec, 1, noteAudioDatum0, noteAudio1)
	if err != nil {
		return err
	}

	noteAudioDatum0.R.NoteAudio = noteAudio1

	noteAudio1.R.NoteAudioData = append(noteAudio1.R.NoteAudioData, noteAudioDatum0)

	return nil
}

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

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

	noteAudioDatum0.R.NoteAudio = noteAudio1

	noteAudio1.R.NoteAudioData = append(noteAudio1.R.NoteAudioData, noteAudioDatum0)

	return nil
}

type noteAudioDatumWhere[Q psql.Filterable] struct {
	Created          psql.WhereMod[Q, time.Time]
	NoteAudioVersion psql.WhereMod[Q, int32]
	NoteAudioUUID    psql.WhereMod[Q, uuid.UUID]
	Type             psql.WhereMod[Q, enums.Audiodatatype]
}

func (noteAudioDatumWhere[Q]) AliasedAs(alias string) noteAudioDatumWhere[Q] {
	return buildNoteAudioDatumWhere[Q](buildNoteAudioDatumColumns(alias))
}

func buildNoteAudioDatumWhere[Q psql.Filterable](cols noteAudioDatumColumns) noteAudioDatumWhere[Q] {
	return noteAudioDatumWhere[Q]{
		Created:          psql.Where[Q, time.Time](cols.Created),
		NoteAudioVersion: psql.Where[Q, int32](cols.NoteAudioVersion),
		NoteAudioUUID:    psql.Where[Q, uuid.UUID](cols.NoteAudioUUID),
		Type:             psql.Where[Q, enums.Audiodatatype](cols.Type),
	}
}

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

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

		o.R.NoteAudio = rel

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

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

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

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

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

	return noteAudioDatumThenLoader[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 noteAudioDatum's NoteAudio into the .R struct
func (o *NoteAudioDatum) 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.NoteAudioData = NoteAudioDatumSlice{o}

	o.R.NoteAudio = related
	return nil
}

// LoadNoteAudio loads the noteAudioDatum's NoteAudio into the .R struct
func (os NoteAudioDatumSlice) 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.NoteAudioData = append(rel.R.NoteAudioData, o)

			o.R.NoteAudio = rel
			break
		}
	}

	return nil
}
