// Code generated by BobGen mysql v0.42.0. 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/aarondl/opt/omit"
	"github.com/stephenafamo/bob"
	"github.com/stephenafamo/bob/dialect/mysql"
	"github.com/stephenafamo/bob/dialect/mysql/dialect"
	"github.com/stephenafamo/bob/dialect/mysql/dm"
	"github.com/stephenafamo/bob/dialect/mysql/sm"
	"github.com/stephenafamo/bob/dialect/mysql/um"
	"github.com/stephenafamo/bob/expr"
	"github.com/stephenafamo/bob/mods"
	"github.com/stephenafamo/bob/orm"
)

// LocationsS3 is an object representing the database table.
type LocationsS3 struct {
	ID              int32  `db:"id,pk,autoincr" `
	EndpointURL     string `db:"endpoint_url" `
	AccessKeyID     string `db:"access_key_id" `
	SecretAccessKey string `db:"secret_access_key" `
	Region          string `db:"region" `
	SpaceID         string `db:"space_id" `
	Bucket          string `db:"bucket" `

	R locationsS3R `db:"-" `
}

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

// LocationsS3S contains methods to work with the locations_s3 table
var LocationsS3S = mysql.NewTablex[*LocationsS3, LocationsS3Slice, *LocationsS3Setter]("locations_s3", buildLocationsS3Columns("locations_s3"), []string{"id"}, []string{"space_id"})

// LocationsS3SQuery is a query on the locations_s3 table
type LocationsS3SQuery = *mysql.ViewQuery[*LocationsS3, LocationsS3Slice]

// locationsS3R is where relationships are stored.
type locationsS3R struct {
	SpaceLocationsSpace *LocationsSpace // locations_s3_space_id_d8fc3ba3_fk_locations_space_uuid
}

func buildLocationsS3Columns(alias string) locationsS3Columns {
	return locationsS3Columns{
		ColumnsExpr: expr.NewColumnsExpr(
			"id", "endpoint_url", "access_key_id", "secret_access_key", "region", "space_id", "bucket",
		).WithParent("locations_s3"),
		tableAlias:      alias,
		ID:              mysql.Quote(alias, "id"),
		EndpointURL:     mysql.Quote(alias, "endpoint_url"),
		AccessKeyID:     mysql.Quote(alias, "access_key_id"),
		SecretAccessKey: mysql.Quote(alias, "secret_access_key"),
		Region:          mysql.Quote(alias, "region"),
		SpaceID:         mysql.Quote(alias, "space_id"),
		Bucket:          mysql.Quote(alias, "bucket"),
	}
}

type locationsS3Columns struct {
	expr.ColumnsExpr
	tableAlias      string
	ID              mysql.Expression
	EndpointURL     mysql.Expression
	AccessKeyID     mysql.Expression
	SecretAccessKey mysql.Expression
	Region          mysql.Expression
	SpaceID         mysql.Expression
	Bucket          mysql.Expression
}

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

func (locationsS3Columns) AliasedAs(alias string) locationsS3Columns {
	return buildLocationsS3Columns(alias)
}

// LocationsS3Setter is used for insert/upsert/update operations
// All values are optional, and do not have to be set
// Generated columns are not included
type LocationsS3Setter struct {
	ID              omit.Val[int32]  `db:"id,pk,autoincr" `
	EndpointURL     omit.Val[string] `db:"endpoint_url" `
	AccessKeyID     omit.Val[string] `db:"access_key_id" `
	SecretAccessKey omit.Val[string] `db:"secret_access_key" `
	Region          omit.Val[string] `db:"region" `
	SpaceID         omit.Val[string] `db:"space_id" `
	Bucket          omit.Val[string] `db:"bucket" `
}

func (s LocationsS3Setter) SetColumns() []string {
	vals := make([]string, 0, 7)
	if s.ID.IsValue() {
		vals = append(vals, "id")
	}
	if s.EndpointURL.IsValue() {
		vals = append(vals, "endpoint_url")
	}
	if s.AccessKeyID.IsValue() {
		vals = append(vals, "access_key_id")
	}
	if s.SecretAccessKey.IsValue() {
		vals = append(vals, "secret_access_key")
	}
	if s.Region.IsValue() {
		vals = append(vals, "region")
	}
	if s.SpaceID.IsValue() {
		vals = append(vals, "space_id")
	}
	if s.Bucket.IsValue() {
		vals = append(vals, "bucket")
	}
	return vals
}

func (s LocationsS3Setter) Overwrite(t *LocationsS3) {
	if s.ID.IsValue() {
		t.ID = s.ID.MustGet()
	}
	if s.EndpointURL.IsValue() {
		t.EndpointURL = s.EndpointURL.MustGet()
	}
	if s.AccessKeyID.IsValue() {
		t.AccessKeyID = s.AccessKeyID.MustGet()
	}
	if s.SecretAccessKey.IsValue() {
		t.SecretAccessKey = s.SecretAccessKey.MustGet()
	}
	if s.Region.IsValue() {
		t.Region = s.Region.MustGet()
	}
	if s.SpaceID.IsValue() {
		t.SpaceID = s.SpaceID.MustGet()
	}
	if s.Bucket.IsValue() {
		t.Bucket = s.Bucket.MustGet()
	}
}

func (s *LocationsS3Setter) Apply(q *dialect.InsertQuery) {
	q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
		return LocationsS3S.BeforeInsertHooks.RunHooks(ctx, exec, s)
	})

	q.AppendValues(
		bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
			if !(s.ID.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.ID.MustGet()).WriteSQL(ctx, w, d, start)
		}), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
			if !(s.EndpointURL.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.EndpointURL.MustGet()).WriteSQL(ctx, w, d, start)
		}), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
			if !(s.AccessKeyID.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.AccessKeyID.MustGet()).WriteSQL(ctx, w, d, start)
		}), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
			if !(s.SecretAccessKey.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.SecretAccessKey.MustGet()).WriteSQL(ctx, w, d, start)
		}), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
			if !(s.Region.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.Region.MustGet()).WriteSQL(ctx, w, d, start)
		}), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
			if !(s.SpaceID.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.SpaceID.MustGet()).WriteSQL(ctx, w, d, start)
		}), bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
			if !(s.Bucket.IsValue()) {
				return mysql.Raw("DEFAULT").WriteSQL(ctx, w, d, start)
			}
			return mysql.Arg(s.Bucket.MustGet()).WriteSQL(ctx, w, d, start)
		}))
}

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

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

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

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

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

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

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

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

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

	return exprs
}

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

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

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

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

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

	return err
}

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

func (o *LocationsS3) pkEQ() dialect.Expression {
	return mysql.Quote("locations_s3", "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 LocationsS3
func (o *LocationsS3) Update(ctx context.Context, exec bob.Executor, s *LocationsS3Setter) error {
	_, err := LocationsS3S.Update(s.UpdateMod(), um.Where(o.pkEQ())).Exec(ctx, exec)
	if err != nil {
		return err
	}

	s.Overwrite(o)

	return nil
}

// Delete deletes a single LocationsS3 record with an executor
func (o *LocationsS3) Delete(ctx context.Context, exec bob.Executor) error {
	_, err := LocationsS3S.Delete(dm.Where(o.pkEQ())).Exec(ctx, exec)
	return err
}

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

	return nil
}

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

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

	return err
}

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

	return mysql.Quote("locations_s3", "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 LocationsS3Slice) copyMatchingRows(from ...*LocationsS3) {
	for i, old := range o {
		for _, new := range from {
			if new.ID != old.ID {
				continue
			}
			new.R = old.R
			o[i] = new
			break
		}
	}
}

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

			return err
		}))

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

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

			return err
		}))

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

func (o LocationsS3Slice) UpdateAll(ctx context.Context, exec bob.Executor, vals LocationsS3Setter) error {
	_, err := LocationsS3S.Update(vals.UpdateMod(), o.UpdateMod()).Exec(ctx, exec)

	for i := range o {
		vals.Overwrite(o[i])
	}

	return err
}

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

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

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

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

	o.copyMatchingRows(o2...)

	return nil
}

// SpaceLocationsSpace starts a query for related objects on locations_space
func (o *LocationsS3) SpaceLocationsSpace(mods ...bob.Mod[*dialect.SelectQuery]) LocationsSpacesQuery {
	return LocationsSpaces.Query(append(mods,
		sm.Where(LocationsSpaces.Columns.UUID.EQ(mysql.Arg(o.SpaceID))),
	)...)
}

func (os LocationsS3Slice) SpaceLocationsSpace(mods ...bob.Mod[*dialect.SelectQuery]) LocationsSpacesQuery {
	PKArgSlice := make([]bob.Expression, len(os))
	for i, o := range os {
		PKArgSlice[i] = mysql.ArgGroup(o.SpaceID)
	}
	PKArgExpr := mysql.Group(PKArgSlice...)

	return LocationsSpaces.Query(append(mods,
		sm.Where(mysql.Group(LocationsSpaces.Columns.UUID).OP("IN", PKArgExpr)),
	)...)
}

func attachLocationsS3SpaceLocationsSpace0(ctx context.Context, exec bob.Executor, count int, locationsS30 *LocationsS3, locationsSpace1 *LocationsSpace) (*LocationsS3, error) {
	setter := &LocationsS3Setter{
		SpaceID: omit.From(locationsSpace1.UUID),
	}

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

	return locationsS30, nil
}

func (locationsS30 *LocationsS3) InsertSpaceLocationsSpace(ctx context.Context, exec bob.Executor, related *LocationsSpaceSetter) error {
	var err error

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

	_, err = attachLocationsS3SpaceLocationsSpace0(ctx, exec, 1, locationsS30, locationsSpace1)
	if err != nil {
		return err
	}

	locationsS30.R.SpaceLocationsSpace = locationsSpace1

	locationsSpace1.R.SpaceLocationsS3 = locationsS30

	return nil
}

func (locationsS30 *LocationsS3) AttachSpaceLocationsSpace(ctx context.Context, exec bob.Executor, locationsSpace1 *LocationsSpace) error {
	var err error

	_, err = attachLocationsS3SpaceLocationsSpace0(ctx, exec, 1, locationsS30, locationsSpace1)
	if err != nil {
		return err
	}

	locationsS30.R.SpaceLocationsSpace = locationsSpace1

	locationsSpace1.R.SpaceLocationsS3 = locationsS30

	return nil
}

type locationsS3Where[Q mysql.Filterable] struct {
	ID              mysql.WhereMod[Q, int32]
	EndpointURL     mysql.WhereMod[Q, string]
	AccessKeyID     mysql.WhereMod[Q, string]
	SecretAccessKey mysql.WhereMod[Q, string]
	Region          mysql.WhereMod[Q, string]
	SpaceID         mysql.WhereMod[Q, string]
	Bucket          mysql.WhereMod[Q, string]
}

func (locationsS3Where[Q]) AliasedAs(alias string) locationsS3Where[Q] {
	return buildLocationsS3Where[Q](buildLocationsS3Columns(alias))
}

func buildLocationsS3Where[Q mysql.Filterable](cols locationsS3Columns) locationsS3Where[Q] {
	return locationsS3Where[Q]{
		ID:              mysql.Where[Q, int32](cols.ID),
		EndpointURL:     mysql.Where[Q, string](cols.EndpointURL),
		AccessKeyID:     mysql.Where[Q, string](cols.AccessKeyID),
		SecretAccessKey: mysql.Where[Q, string](cols.SecretAccessKey),
		Region:          mysql.Where[Q, string](cols.Region),
		SpaceID:         mysql.Where[Q, string](cols.SpaceID),
		Bucket:          mysql.Where[Q, string](cols.Bucket),
	}
}

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

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

		o.R.SpaceLocationsSpace = rel

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

type locationsS3Preloader struct {
	SpaceLocationsSpace func(...mysql.PreloadOption) mysql.Preloader
}

func buildLocationsS3Preloader() locationsS3Preloader {
	return locationsS3Preloader{
		SpaceLocationsSpace: func(opts ...mysql.PreloadOption) mysql.Preloader {
			return mysql.Preload[*LocationsSpace, LocationsSpaceSlice](mysql.PreloadRel{
				Name: "SpaceLocationsSpace",
				Sides: []mysql.PreloadSide{
					{
						From:        LocationsS3S,
						To:          LocationsSpaces,
						FromColumns: []string{"space_id"},
						ToColumns:   []string{"uuid"},
					},
				},
			}, LocationsSpaces.Columns.Names(), opts...)
		},
	}
}

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

func buildLocationsS3ThenLoader[Q orm.Loadable]() locationsS3ThenLoader[Q] {
	type SpaceLocationsSpaceLoadInterface interface {
		LoadSpaceLocationsSpace(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}

	return locationsS3ThenLoader[Q]{
		SpaceLocationsSpace: thenLoadBuilder[Q](
			"SpaceLocationsSpace",
			func(ctx context.Context, exec bob.Executor, retrieved SpaceLocationsSpaceLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadSpaceLocationsSpace(ctx, exec, mods...)
			},
		),
	}
}

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

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

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

	related.R.SpaceLocationsS3 = o

	o.R.SpaceLocationsSpace = related
	return nil
}

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

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

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

		for _, rel := range locationsSpaces {

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

			rel.R.SpaceLocationsS3 = o

			o.R.SpaceLocationsSpace = rel
			break
		}
	}

	return nil
}

type locationsS3Joins[Q dialect.Joinable] struct {
	typ                 string
	SpaceLocationsSpace modAs[Q, locationsSpaceColumns]
}

func (j locationsS3Joins[Q]) aliasedAs(alias string) locationsS3Joins[Q] {
	return buildLocationsS3Joins[Q](buildLocationsS3Columns(alias), j.typ)
}

func buildLocationsS3Joins[Q dialect.Joinable](cols locationsS3Columns, typ string) locationsS3Joins[Q] {
	return locationsS3Joins[Q]{
		typ: typ,
		SpaceLocationsSpace: modAs[Q, locationsSpaceColumns]{
			c: LocationsSpaces.Columns,
			f: func(to locationsSpaceColumns) bob.Mod[Q] {
				mods := make(mods.QueryMods[Q], 0, 1)

				{
					mods = append(mods, dialect.Join[Q](typ, LocationsSpaces.Name().As(to.Alias())).On(
						to.UUID.EQ(cols.SpaceID),
					))
				}

				return mods
			},
		},
	}
}
