// 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 factory

import (
	"context"
	"testing"
	"time"

	"github.com/Gleipnir-Technology/bob"
	enums "github.com/Gleipnir-Technology/nidus-sync/db/enums"
	models "github.com/Gleipnir-Technology/nidus-sync/db/models"
	"github.com/aarondl/opt/omit"
	"github.com/jaswdr/faker/v2"
)

type AddressMod interface {
	Apply(context.Context, *AddressTemplate)
}

type AddressModFunc func(context.Context, *AddressTemplate)

func (f AddressModFunc) Apply(ctx context.Context, n *AddressTemplate) {
	f(ctx, n)
}

type AddressModSlice []AddressMod

func (mods AddressModSlice) Apply(ctx context.Context, n *AddressTemplate) {
	for _, f := range mods {
		f.Apply(ctx, n)
	}
}

// AddressTemplate is an object representing the database table.
// all columns are optional and should be set by mods
type AddressTemplate struct {
	Country    func() enums.Countrytype
	Created    func() time.Time
	Geom       func() string
	H3cell     func() string
	ID         func() int32
	Locality   func() string
	Number     func() int32
	PostalCode func() string
	Street     func() string
	Unit       func() string

	r addressR
	f *Factory

	alreadyPersisted bool
}

type addressR struct {
	Mailers   []*addressRMailersR
	Residents []*addressRResidentsR
	Site      *addressRSiteR
}

type addressRMailersR struct {
	number int
	o      *CommsMailerTemplate
}
type addressRResidentsR struct {
	number int
	o      *ResidentTemplate
}
type addressRSiteR struct {
	o *SiteTemplate
}

// Apply mods to the AddressTemplate
func (o *AddressTemplate) Apply(ctx context.Context, mods ...AddressMod) {
	for _, mod := range mods {
		mod.Apply(ctx, o)
	}
}

// setModelRels creates and sets the relationships on *models.Address
// according to the relationships in the template. Nothing is inserted into the db
func (t AddressTemplate) setModelRels(o *models.Address) {
	if t.r.Mailers != nil {
		rel := models.CommsMailerSlice{}
		for _, r := range t.r.Mailers {
			related := r.o.BuildMany(r.number)
			for _, rel := range related {
				rel.AddressID = o.ID // h2
				rel.R.Address = o
			}
			rel = append(rel, related...)
		}
		o.R.Mailers = rel
	}

	if t.r.Residents != nil {
		rel := models.ResidentSlice{}
		for _, r := range t.r.Residents {
			related := r.o.BuildMany(r.number)
			for _, rel := range related {
				rel.AddressID = o.ID // h2
				rel.R.Address = o
			}
			rel = append(rel, related...)
		}
		o.R.Residents = rel
	}

	if t.r.Site != nil {
		rel := t.r.Site.o.Build()
		rel.R.Address = o
		rel.AddressID = o.ID // h2
		o.R.Site = rel
	}
}

// BuildSetter returns an *models.AddressSetter
// this does nothing with the relationship templates
func (o AddressTemplate) BuildSetter() *models.AddressSetter {
	m := &models.AddressSetter{}

	if o.Country != nil {
		val := o.Country()
		m.Country = omit.From(val)
	}
	if o.Created != nil {
		val := o.Created()
		m.Created = omit.From(val)
	}
	if o.Geom != nil {
		val := o.Geom()
		m.Geom = omit.From(val)
	}
	if o.H3cell != nil {
		val := o.H3cell()
		m.H3cell = omit.From(val)
	}
	if o.ID != nil {
		val := o.ID()
		m.ID = omit.From(val)
	}
	if o.Locality != nil {
		val := o.Locality()
		m.Locality = omit.From(val)
	}
	if o.Number != nil {
		val := o.Number()
		m.Number = omit.From(val)
	}
	if o.PostalCode != nil {
		val := o.PostalCode()
		m.PostalCode = omit.From(val)
	}
	if o.Street != nil {
		val := o.Street()
		m.Street = omit.From(val)
	}
	if o.Unit != nil {
		val := o.Unit()
		m.Unit = omit.From(val)
	}

	return m
}

// BuildManySetter returns an []*models.AddressSetter
// this does nothing with the relationship templates
func (o AddressTemplate) BuildManySetter(number int) []*models.AddressSetter {
	m := make([]*models.AddressSetter, number)

	for i := range m {
		m[i] = o.BuildSetter()
	}

	return m
}

// Build returns an *models.Address
// Related objects are also created and placed in the .R field
// NOTE: Objects are not inserted into the database. Use AddressTemplate.Create
func (o AddressTemplate) Build() *models.Address {
	m := &models.Address{}

	if o.Country != nil {
		m.Country = o.Country()
	}
	if o.Created != nil {
		m.Created = o.Created()
	}
	if o.Geom != nil {
		m.Geom = o.Geom()
	}
	if o.H3cell != nil {
		m.H3cell = o.H3cell()
	}
	if o.ID != nil {
		m.ID = o.ID()
	}
	if o.Locality != nil {
		m.Locality = o.Locality()
	}
	if o.Number != nil {
		m.Number = o.Number()
	}
	if o.PostalCode != nil {
		m.PostalCode = o.PostalCode()
	}
	if o.Street != nil {
		m.Street = o.Street()
	}
	if o.Unit != nil {
		m.Unit = o.Unit()
	}

	o.setModelRels(m)

	return m
}

// BuildMany returns an models.AddressSlice
// Related objects are also created and placed in the .R field
// NOTE: Objects are not inserted into the database. Use AddressTemplate.CreateMany
func (o AddressTemplate) BuildMany(number int) models.AddressSlice {
	m := make(models.AddressSlice, number)

	for i := range m {
		m[i] = o.Build()
	}

	return m
}

func ensureCreatableAddress(m *models.AddressSetter) {
	if !(m.Country.IsValue()) {
		val := random_enums_Countrytype(nil)
		m.Country = omit.From(val)
	}
	if !(m.Created.IsValue()) {
		val := random_time_Time(nil)
		m.Created = omit.From(val)
	}
	if !(m.Geom.IsValue()) {
		val := random_string(nil)
		m.Geom = omit.From(val)
	}
	if !(m.H3cell.IsValue()) {
		val := random_string(nil)
		m.H3cell = omit.From(val)
	}
	if !(m.Locality.IsValue()) {
		val := random_string(nil)
		m.Locality = omit.From(val)
	}
	if !(m.Number.IsValue()) {
		val := random_int32(nil)
		m.Number = omit.From(val)
	}
	if !(m.PostalCode.IsValue()) {
		val := random_string(nil)
		m.PostalCode = omit.From(val)
	}
	if !(m.Street.IsValue()) {
		val := random_string(nil)
		m.Street = omit.From(val)
	}
	if !(m.Unit.IsValue()) {
		val := random_string(nil)
		m.Unit = omit.From(val)
	}
}

// insertOptRels creates and inserts any optional the relationships on *models.Address
// according to the relationships in the template.
// any required relationship should have already exist on the model
func (o *AddressTemplate) insertOptRels(ctx context.Context, exec bob.Executor, m *models.Address) error {
	var err error

	isMailersDone, _ := addressRelMailersCtx.Value(ctx)
	if !isMailersDone && o.r.Mailers != nil {
		ctx = addressRelMailersCtx.WithValue(ctx, true)
		for _, r := range o.r.Mailers {
			if r.o.alreadyPersisted {
				m.R.Mailers = append(m.R.Mailers, r.o.Build())
			} else {
				rel0, err := r.o.CreateMany(ctx, exec, r.number)
				if err != nil {
					return err
				}

				err = m.AttachMailers(ctx, exec, rel0...)
				if err != nil {
					return err
				}
			}
		}
	}

	isResidentsDone, _ := addressRelResidentsCtx.Value(ctx)
	if !isResidentsDone && o.r.Residents != nil {
		ctx = addressRelResidentsCtx.WithValue(ctx, true)
		for _, r := range o.r.Residents {
			if r.o.alreadyPersisted {
				m.R.Residents = append(m.R.Residents, r.o.Build())
			} else {
				rel1, err := r.o.CreateMany(ctx, exec, r.number)
				if err != nil {
					return err
				}

				err = m.AttachResidents(ctx, exec, rel1...)
				if err != nil {
					return err
				}
			}
		}
	}

	isSiteDone, _ := addressRelSiteCtx.Value(ctx)
	if !isSiteDone && o.r.Site != nil {
		ctx = addressRelSiteCtx.WithValue(ctx, true)
		if o.r.Site.o.alreadyPersisted {
			m.R.Site = o.r.Site.o.Build()
		} else {
			var rel2 *models.Site
			rel2, err = o.r.Site.o.Create(ctx, exec)
			if err != nil {
				return err
			}
			err = m.AttachSite(ctx, exec, rel2)
			if err != nil {
				return err
			}
		}

	}

	return err
}

// Create builds a address and inserts it into the database
// Relations objects are also inserted and placed in the .R field
func (o *AddressTemplate) Create(ctx context.Context, exec bob.Executor) (*models.Address, error) {
	var err error
	opt := o.BuildSetter()
	ensureCreatableAddress(opt)

	m, err := models.Addresses.Insert(opt).One(ctx, exec)
	if err != nil {
		return nil, err
	}

	if err := o.insertOptRels(ctx, exec, m); err != nil {
		return nil, err
	}
	return m, err
}

// MustCreate builds a address and inserts it into the database
// Relations objects are also inserted and placed in the .R field
// panics if an error occurs
func (o *AddressTemplate) MustCreate(ctx context.Context, exec bob.Executor) *models.Address {
	m, err := o.Create(ctx, exec)
	if err != nil {
		panic(err)
	}
	return m
}

// CreateOrFail builds a address and inserts it into the database
// Relations objects are also inserted and placed in the .R field
// It calls `tb.Fatal(err)` on the test/benchmark if an error occurs
func (o *AddressTemplate) CreateOrFail(ctx context.Context, tb testing.TB, exec bob.Executor) *models.Address {
	tb.Helper()
	m, err := o.Create(ctx, exec)
	if err != nil {
		tb.Fatal(err)
		return nil
	}
	return m
}

// CreateMany builds multiple addresses and inserts them into the database
// Relations objects are also inserted and placed in the .R field
func (o AddressTemplate) CreateMany(ctx context.Context, exec bob.Executor, number int) (models.AddressSlice, error) {
	var err error
	m := make(models.AddressSlice, number)

	for i := range m {
		m[i], err = o.Create(ctx, exec)
		if err != nil {
			return nil, err
		}
	}

	return m, nil
}

// MustCreateMany builds multiple addresses and inserts them into the database
// Relations objects are also inserted and placed in the .R field
// panics if an error occurs
func (o AddressTemplate) MustCreateMany(ctx context.Context, exec bob.Executor, number int) models.AddressSlice {
	m, err := o.CreateMany(ctx, exec, number)
	if err != nil {
		panic(err)
	}
	return m
}

// CreateManyOrFail builds multiple addresses and inserts them into the database
// Relations objects are also inserted and placed in the .R field
// It calls `tb.Fatal(err)` on the test/benchmark if an error occurs
func (o AddressTemplate) CreateManyOrFail(ctx context.Context, tb testing.TB, exec bob.Executor, number int) models.AddressSlice {
	tb.Helper()
	m, err := o.CreateMany(ctx, exec, number)
	if err != nil {
		tb.Fatal(err)
		return nil
	}
	return m
}

// Address has methods that act as mods for the AddressTemplate
var AddressMods addressMods

type addressMods struct{}

func (m addressMods) RandomizeAllColumns(f *faker.Faker) AddressMod {
	return AddressModSlice{
		AddressMods.RandomCountry(f),
		AddressMods.RandomCreated(f),
		AddressMods.RandomGeom(f),
		AddressMods.RandomH3cell(f),
		AddressMods.RandomID(f),
		AddressMods.RandomLocality(f),
		AddressMods.RandomNumber(f),
		AddressMods.RandomPostalCode(f),
		AddressMods.RandomStreet(f),
		AddressMods.RandomUnit(f),
	}
}

// Set the model columns to this value
func (m addressMods) Country(val enums.Countrytype) AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.Country = func() enums.Countrytype { return val }
	})
}

// Set the Column from the function
func (m addressMods) CountryFunc(f func() enums.Countrytype) AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.Country = f
	})
}

// Clear any values for the column
func (m addressMods) UnsetCountry() AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.Country = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m addressMods) RandomCountry(f *faker.Faker) AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.Country = func() enums.Countrytype {
			return random_enums_Countrytype(f)
		}
	})
}

// Set the model columns to this value
func (m addressMods) Created(val time.Time) AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.Created = func() time.Time { return val }
	})
}

// Set the Column from the function
func (m addressMods) CreatedFunc(f func() time.Time) AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.Created = f
	})
}

// Clear any values for the column
func (m addressMods) UnsetCreated() AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.Created = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m addressMods) RandomCreated(f *faker.Faker) AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.Created = func() time.Time {
			return random_time_Time(f)
		}
	})
}

// Set the model columns to this value
func (m addressMods) Geom(val string) AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.Geom = func() string { return val }
	})
}

// Set the Column from the function
func (m addressMods) GeomFunc(f func() string) AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.Geom = f
	})
}

// Clear any values for the column
func (m addressMods) UnsetGeom() AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.Geom = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m addressMods) RandomGeom(f *faker.Faker) AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.Geom = func() string {
			return random_string(f)
		}
	})
}

// Set the model columns to this value
func (m addressMods) H3cell(val string) AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.H3cell = func() string { return val }
	})
}

// Set the Column from the function
func (m addressMods) H3cellFunc(f func() string) AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.H3cell = f
	})
}

// Clear any values for the column
func (m addressMods) UnsetH3cell() AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.H3cell = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m addressMods) RandomH3cell(f *faker.Faker) AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.H3cell = func() string {
			return random_string(f)
		}
	})
}

// Set the model columns to this value
func (m addressMods) ID(val int32) AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.ID = func() int32 { return val }
	})
}

// Set the Column from the function
func (m addressMods) IDFunc(f func() int32) AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.ID = f
	})
}

// Clear any values for the column
func (m addressMods) UnsetID() AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.ID = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m addressMods) RandomID(f *faker.Faker) AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.ID = func() int32 {
			return random_int32(f)
		}
	})
}

// Set the model columns to this value
func (m addressMods) Locality(val string) AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.Locality = func() string { return val }
	})
}

// Set the Column from the function
func (m addressMods) LocalityFunc(f func() string) AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.Locality = f
	})
}

// Clear any values for the column
func (m addressMods) UnsetLocality() AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.Locality = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m addressMods) RandomLocality(f *faker.Faker) AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.Locality = func() string {
			return random_string(f)
		}
	})
}

// Set the model columns to this value
func (m addressMods) Number(val int32) AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.Number = func() int32 { return val }
	})
}

// Set the Column from the function
func (m addressMods) NumberFunc(f func() int32) AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.Number = f
	})
}

// Clear any values for the column
func (m addressMods) UnsetNumber() AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.Number = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m addressMods) RandomNumber(f *faker.Faker) AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.Number = func() int32 {
			return random_int32(f)
		}
	})
}

// Set the model columns to this value
func (m addressMods) PostalCode(val string) AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.PostalCode = func() string { return val }
	})
}

// Set the Column from the function
func (m addressMods) PostalCodeFunc(f func() string) AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.PostalCode = f
	})
}

// Clear any values for the column
func (m addressMods) UnsetPostalCode() AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.PostalCode = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m addressMods) RandomPostalCode(f *faker.Faker) AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.PostalCode = func() string {
			return random_string(f)
		}
	})
}

// Set the model columns to this value
func (m addressMods) Street(val string) AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.Street = func() string { return val }
	})
}

// Set the Column from the function
func (m addressMods) StreetFunc(f func() string) AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.Street = f
	})
}

// Clear any values for the column
func (m addressMods) UnsetStreet() AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.Street = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m addressMods) RandomStreet(f *faker.Faker) AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.Street = func() string {
			return random_string(f)
		}
	})
}

// Set the model columns to this value
func (m addressMods) Unit(val string) AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.Unit = func() string { return val }
	})
}

// Set the Column from the function
func (m addressMods) UnitFunc(f func() string) AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.Unit = f
	})
}

// Clear any values for the column
func (m addressMods) UnsetUnit() AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.Unit = nil
	})
}

// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m addressMods) RandomUnit(f *faker.Faker) AddressMod {
	return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
		o.Unit = func() string {
			return random_string(f)
		}
	})
}

func (m addressMods) WithParentsCascading() AddressMod {
	return AddressModFunc(func(ctx context.Context, o *AddressTemplate) {
		if isDone, _ := addressWithParentsCascadingCtx.Value(ctx); isDone {
			return
		}
		ctx = addressWithParentsCascadingCtx.WithValue(ctx, true)
		{

			related := o.f.NewSiteWithContext(ctx, SiteMods.WithParentsCascading())
			m.WithSite(related).Apply(ctx, o)
		}
	})
}

func (m addressMods) WithSite(rel *SiteTemplate) AddressMod {
	return AddressModFunc(func(ctx context.Context, o *AddressTemplate) {
		o.r.Site = &addressRSiteR{
			o: rel,
		}
	})
}

func (m addressMods) WithNewSite(mods ...SiteMod) AddressMod {
	return AddressModFunc(func(ctx context.Context, o *AddressTemplate) {
		related := o.f.NewSiteWithContext(ctx, mods...)

		m.WithSite(related).Apply(ctx, o)
	})
}

func (m addressMods) WithExistingSite(em *models.Site) AddressMod {
	return AddressModFunc(func(ctx context.Context, o *AddressTemplate) {
		o.r.Site = &addressRSiteR{
			o: o.f.FromExistingSite(em),
		}
	})
}

func (m addressMods) WithoutSite() AddressMod {
	return AddressModFunc(func(ctx context.Context, o *AddressTemplate) {
		o.r.Site = nil
	})
}

func (m addressMods) WithMailers(number int, related *CommsMailerTemplate) AddressMod {
	return AddressModFunc(func(ctx context.Context, o *AddressTemplate) {
		o.r.Mailers = []*addressRMailersR{{
			number: number,
			o:      related,
		}}
	})
}

func (m addressMods) WithNewMailers(number int, mods ...CommsMailerMod) AddressMod {
	return AddressModFunc(func(ctx context.Context, o *AddressTemplate) {
		related := o.f.NewCommsMailerWithContext(ctx, mods...)
		m.WithMailers(number, related).Apply(ctx, o)
	})
}

func (m addressMods) AddMailers(number int, related *CommsMailerTemplate) AddressMod {
	return AddressModFunc(func(ctx context.Context, o *AddressTemplate) {
		o.r.Mailers = append(o.r.Mailers, &addressRMailersR{
			number: number,
			o:      related,
		})
	})
}

func (m addressMods) AddNewMailers(number int, mods ...CommsMailerMod) AddressMod {
	return AddressModFunc(func(ctx context.Context, o *AddressTemplate) {
		related := o.f.NewCommsMailerWithContext(ctx, mods...)
		m.AddMailers(number, related).Apply(ctx, o)
	})
}

func (m addressMods) AddExistingMailers(existingModels ...*models.CommsMailer) AddressMod {
	return AddressModFunc(func(ctx context.Context, o *AddressTemplate) {
		for _, em := range existingModels {
			o.r.Mailers = append(o.r.Mailers, &addressRMailersR{
				o: o.f.FromExistingCommsMailer(em),
			})
		}
	})
}

func (m addressMods) WithoutMailers() AddressMod {
	return AddressModFunc(func(ctx context.Context, o *AddressTemplate) {
		o.r.Mailers = nil
	})
}

func (m addressMods) WithResidents(number int, related *ResidentTemplate) AddressMod {
	return AddressModFunc(func(ctx context.Context, o *AddressTemplate) {
		o.r.Residents = []*addressRResidentsR{{
			number: number,
			o:      related,
		}}
	})
}

func (m addressMods) WithNewResidents(number int, mods ...ResidentMod) AddressMod {
	return AddressModFunc(func(ctx context.Context, o *AddressTemplate) {
		related := o.f.NewResidentWithContext(ctx, mods...)
		m.WithResidents(number, related).Apply(ctx, o)
	})
}

func (m addressMods) AddResidents(number int, related *ResidentTemplate) AddressMod {
	return AddressModFunc(func(ctx context.Context, o *AddressTemplate) {
		o.r.Residents = append(o.r.Residents, &addressRResidentsR{
			number: number,
			o:      related,
		})
	})
}

func (m addressMods) AddNewResidents(number int, mods ...ResidentMod) AddressMod {
	return AddressModFunc(func(ctx context.Context, o *AddressTemplate) {
		related := o.f.NewResidentWithContext(ctx, mods...)
		m.AddResidents(number, related).Apply(ctx, o)
	})
}

func (m addressMods) AddExistingResidents(existingModels ...*models.Resident) AddressMod {
	return AddressModFunc(func(ctx context.Context, o *AddressTemplate) {
		for _, em := range existingModels {
			o.r.Residents = append(o.r.Residents, &addressRResidentsR{
				o: o.f.FromExistingResident(em),
			})
		}
	})
}

func (m addressMods) WithoutResidents() AddressMod {
	return AddressModFunc(func(ctx context.Context, o *AddressTemplate) {
		o.r.Residents = nil
	})
}
