// 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"
)

// PublicreportReportImage is an object representing the database table.
type PublicreportReportImage struct {
	ImageID  int32 `db:"image_id,pk" `
	ReportID int32 `db:"report_id,pk" `

	R publicreportReportImageR `db:"-" `
}

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

// PublicreportReportImages contains methods to work with the report_image table
var PublicreportReportImages = psql.NewTablex[*PublicreportReportImage, PublicreportReportImageSlice, *PublicreportReportImageSetter]("publicreport", "report_image", buildPublicreportReportImageColumns("publicreport.report_image"))

// PublicreportReportImagesQuery is a query on the report_image table
type PublicreportReportImagesQuery = *psql.ViewQuery[*PublicreportReportImage, PublicreportReportImageSlice]

// publicreportReportImageR is where relationships are stored.
type publicreportReportImageR struct {
	Image  *PublicreportImage  // publicreport.report_image.report_image_image_id_fkey
	Report *PublicreportReport // publicreport.report_image.report_image_report_id_fkey
}

func buildPublicreportReportImageColumns(alias string) publicreportReportImageColumns {
	return publicreportReportImageColumns{
		ColumnsExpr: expr.NewColumnsExpr(
			"image_id", "report_id",
		).WithParent("publicreport.report_image"),
		tableAlias: alias,
		ImageID:    psql.Quote(alias, "image_id"),
		ReportID:   psql.Quote(alias, "report_id"),
	}
}

type publicreportReportImageColumns struct {
	expr.ColumnsExpr
	tableAlias string
	ImageID    psql.Expression
	ReportID   psql.Expression
}

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

func (publicreportReportImageColumns) AliasedAs(alias string) publicreportReportImageColumns {
	return buildPublicreportReportImageColumns(alias)
}

// PublicreportReportImageSetter is used for insert/upsert/update operations
// All values are optional, and do not have to be set
// Generated columns are not included
type PublicreportReportImageSetter struct {
	ImageID  omit.Val[int32] `db:"image_id,pk" `
	ReportID omit.Val[int32] `db:"report_id,pk" `
}

func (s PublicreportReportImageSetter) SetColumns() []string {
	vals := make([]string, 0, 2)
	if s.ImageID.IsValue() {
		vals = append(vals, "image_id")
	}
	if s.ReportID.IsValue() {
		vals = append(vals, "report_id")
	}
	return vals
}

func (s PublicreportReportImageSetter) Overwrite(t *PublicreportReportImage) {
	if s.ImageID.IsValue() {
		t.ImageID = s.ImageID.MustGet()
	}
	if s.ReportID.IsValue() {
		t.ReportID = s.ReportID.MustGet()
	}
}

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

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

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

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

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

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

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

	return exprs
}

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

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

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

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

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

	return err
}

// primaryKeyVals returns the primary key values of the PublicreportReportImage
func (o *PublicreportReportImage) primaryKeyVals() bob.Expression {
	return psql.ArgGroup(
		o.ImageID,
		o.ReportID,
	)
}

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

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

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

	return err
}

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

	return psql.Group(psql.Quote("publicreport.report_image", "image_id"), psql.Quote("publicreport.report_image", "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 PublicreportReportImageSlice) copyMatchingRows(from ...*PublicreportReportImage) {
	for i, old := range o {
		for _, new := range from {
			if new.ImageID != old.ImageID {
				continue
			}
			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 PublicreportReportImageSlice) 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 PublicreportReportImages.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 *PublicreportReportImage:
				o.copyMatchingRows(retrieved)
			case []*PublicreportReportImage:
				o.copyMatchingRows(retrieved...)
			case PublicreportReportImageSlice:
				o.copyMatchingRows(retrieved...)
			default:
				// If the retrieved value is not a PublicreportReportImage or a slice of PublicreportReportImage
				// then run the AfterUpdateHooks on the slice
				_, err = PublicreportReportImages.AfterUpdateHooks.RunHooks(ctx, exec, o)
			}

			return err
		}))

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

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

			return err
		}))

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

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

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

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

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

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

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

	o.copyMatchingRows(o2...)

	return nil
}

// Image starts a query for related objects on publicreport.image
func (o *PublicreportReportImage) Image(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportImagesQuery {
	return PublicreportImages.Query(append(mods,
		sm.Where(PublicreportImages.Columns.ID.EQ(psql.Arg(o.ImageID))),
	)...)
}

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

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

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

func (os PublicreportReportImageSlice) 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 attachPublicreportReportImageImage0(ctx context.Context, exec bob.Executor, count int, publicreportReportImage0 *PublicreportReportImage, publicreportImage1 *PublicreportImage) (*PublicreportReportImage, error) {
	setter := &PublicreportReportImageSetter{
		ImageID: omit.From(publicreportImage1.ID),
	}

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

	return publicreportReportImage0, nil
}

func (publicreportReportImage0 *PublicreportReportImage) InsertImage(ctx context.Context, exec bob.Executor, related *PublicreportImageSetter) error {
	var err error

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

	_, err = attachPublicreportReportImageImage0(ctx, exec, 1, publicreportReportImage0, publicreportImage1)
	if err != nil {
		return err
	}

	publicreportReportImage0.R.Image = publicreportImage1

	return nil
}

func (publicreportReportImage0 *PublicreportReportImage) AttachImage(ctx context.Context, exec bob.Executor, publicreportImage1 *PublicreportImage) error {
	var err error

	_, err = attachPublicreportReportImageImage0(ctx, exec, 1, publicreportReportImage0, publicreportImage1)
	if err != nil {
		return err
	}

	publicreportReportImage0.R.Image = publicreportImage1

	return nil
}

func attachPublicreportReportImageReport0(ctx context.Context, exec bob.Executor, count int, publicreportReportImage0 *PublicreportReportImage, publicreportReport1 *PublicreportReport) (*PublicreportReportImage, error) {
	setter := &PublicreportReportImageSetter{
		ReportID: omit.From(publicreportReport1.ID),
	}

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

	return publicreportReportImage0, nil
}

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

	publicreportReportImage0.R.Report = publicreportReport1

	return nil
}

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

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

	publicreportReportImage0.R.Report = publicreportReport1

	return nil
}

type publicreportReportImageWhere[Q psql.Filterable] struct {
	ImageID  psql.WhereMod[Q, int32]
	ReportID psql.WhereMod[Q, int32]
}

func (publicreportReportImageWhere[Q]) AliasedAs(alias string) publicreportReportImageWhere[Q] {
	return buildPublicreportReportImageWhere[Q](buildPublicreportReportImageColumns(alias))
}

func buildPublicreportReportImageWhere[Q psql.Filterable](cols publicreportReportImageColumns) publicreportReportImageWhere[Q] {
	return publicreportReportImageWhere[Q]{
		ImageID:  psql.Where[Q, int32](cols.ImageID),
		ReportID: psql.Where[Q, int32](cols.ReportID),
	}
}

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

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

		o.R.Image = rel

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

		o.R.Report = rel

		return nil
	default:
		return fmt.Errorf("publicreportReportImage has no relationship %q", name)
	}
}

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

func buildPublicreportReportImagePreloader() publicreportReportImagePreloader {
	return publicreportReportImagePreloader{
		Image: func(opts ...psql.PreloadOption) psql.Preloader {
			return psql.Preload[*PublicreportImage, PublicreportImageSlice](psql.PreloadRel{
				Name: "Image",
				Sides: []psql.PreloadSide{
					{
						From:        PublicreportReportImages,
						To:          PublicreportImages,
						FromColumns: []string{"image_id"},
						ToColumns:   []string{"id"},
					},
				},
			}, PublicreportImages.Columns.Names(), opts...)
		},
		Report: func(opts ...psql.PreloadOption) psql.Preloader {
			return psql.Preload[*PublicreportReport, PublicreportReportSlice](psql.PreloadRel{
				Name: "Report",
				Sides: []psql.PreloadSide{
					{
						From:        PublicreportReportImages,
						To:          PublicreportReports,
						FromColumns: []string{"report_id"},
						ToColumns:   []string{"id"},
					},
				},
			}, PublicreportReports.Columns.Names(), opts...)
		},
	}
}

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

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

	return publicreportReportImageThenLoader[Q]{
		Image: thenLoadBuilder[Q](
			"Image",
			func(ctx context.Context, exec bob.Executor, retrieved ImageLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadImage(ctx, exec, mods...)
			},
		),
		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...)
			},
		),
	}
}

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

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

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

	o.R.Image = related
	return nil
}

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

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

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

		for _, rel := range publicreportImages {

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

			o.R.Image = rel
			break
		}
	}

	return nil
}

// LoadReport loads the publicreportReportImage's Report into the .R struct
func (o *PublicreportReportImage) 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
	}

	o.R.Report = related
	return nil
}

// LoadReport loads the publicreportReportImage's Report into the .R struct
func (os PublicreportReportImageSlice) 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
			}

			o.R.Report = rel
			break
		}
	}

	return nil
}
