// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"database/sql/driver"
	"fmt"
	"math"

	"entgo.io/ent"
	"entgo.io/ent/dialect"
	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/google/uuid"
	"github.com/lightsparkdev/spark/so/ent/paymentintent"
	"github.com/lightsparkdev/spark/so/ent/predicate"
	"github.com/lightsparkdev/spark/so/ent/tokentransaction"
	"github.com/lightsparkdev/spark/so/ent/transfer"
)

// PaymentIntentQuery is the builder for querying PaymentIntent entities.
type PaymentIntentQuery struct {
	config
	ctx                  *QueryContext
	order                []paymentintent.OrderOption
	inters               []Interceptor
	predicates           []predicate.PaymentIntent
	withTransfer         *TransferQuery
	withTokenTransaction *TokenTransactionQuery
	modifiers            []func(*sql.Selector)
	// intermediate query (i.e. traversal path).
	sql  *sql.Selector
	path func(context.Context) (*sql.Selector, error)
}

// Where adds a new predicate for the PaymentIntentQuery builder.
func (piq *PaymentIntentQuery) Where(ps ...predicate.PaymentIntent) *PaymentIntentQuery {
	piq.predicates = append(piq.predicates, ps...)
	return piq
}

// Limit the number of records to be returned by this query.
func (piq *PaymentIntentQuery) Limit(limit int) *PaymentIntentQuery {
	piq.ctx.Limit = &limit
	return piq
}

// Offset to start from.
func (piq *PaymentIntentQuery) Offset(offset int) *PaymentIntentQuery {
	piq.ctx.Offset = &offset
	return piq
}

// Unique configures the query builder to filter duplicate records on query.
// By default, unique is set to true, and can be disabled using this method.
func (piq *PaymentIntentQuery) Unique(unique bool) *PaymentIntentQuery {
	piq.ctx.Unique = &unique
	return piq
}

// Order specifies how the records should be ordered.
func (piq *PaymentIntentQuery) Order(o ...paymentintent.OrderOption) *PaymentIntentQuery {
	piq.order = append(piq.order, o...)
	return piq
}

// QueryTransfer chains the current query on the "transfer" edge.
func (piq *PaymentIntentQuery) QueryTransfer() *TransferQuery {
	query := (&TransferClient{config: piq.config}).Query()
	query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
		if err := piq.prepareQuery(ctx); err != nil {
			return nil, err
		}
		selector := piq.sqlQuery(ctx)
		if err := selector.Err(); err != nil {
			return nil, err
		}
		step := sqlgraph.NewStep(
			sqlgraph.From(paymentintent.Table, paymentintent.FieldID, selector),
			sqlgraph.To(transfer.Table, transfer.FieldID),
			sqlgraph.Edge(sqlgraph.O2M, true, paymentintent.TransferTable, paymentintent.TransferColumn),
		)
		fromU = sqlgraph.SetNeighbors(piq.driver.Dialect(), step)
		return fromU, nil
	}
	return query
}

// QueryTokenTransaction chains the current query on the "token_transaction" edge.
func (piq *PaymentIntentQuery) QueryTokenTransaction() *TokenTransactionQuery {
	query := (&TokenTransactionClient{config: piq.config}).Query()
	query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
		if err := piq.prepareQuery(ctx); err != nil {
			return nil, err
		}
		selector := piq.sqlQuery(ctx)
		if err := selector.Err(); err != nil {
			return nil, err
		}
		step := sqlgraph.NewStep(
			sqlgraph.From(paymentintent.Table, paymentintent.FieldID, selector),
			sqlgraph.To(tokentransaction.Table, tokentransaction.FieldID),
			sqlgraph.Edge(sqlgraph.O2M, true, paymentintent.TokenTransactionTable, paymentintent.TokenTransactionColumn),
		)
		fromU = sqlgraph.SetNeighbors(piq.driver.Dialect(), step)
		return fromU, nil
	}
	return query
}

// First returns the first PaymentIntent entity from the query.
// Returns a *NotFoundError when no PaymentIntent was found.
func (piq *PaymentIntentQuery) First(ctx context.Context) (*PaymentIntent, error) {
	nodes, err := piq.Limit(1).All(setContextOp(ctx, piq.ctx, ent.OpQueryFirst))
	if err != nil {
		return nil, err
	}
	if len(nodes) == 0 {
		return nil, &NotFoundError{paymentintent.Label}
	}
	return nodes[0], nil
}

// FirstX is like First, but panics if an error occurs.
func (piq *PaymentIntentQuery) FirstX(ctx context.Context) *PaymentIntent {
	node, err := piq.First(ctx)
	if err != nil && !IsNotFound(err) {
		panic(err)
	}
	return node
}

// FirstID returns the first PaymentIntent ID from the query.
// Returns a *NotFoundError when no PaymentIntent ID was found.
func (piq *PaymentIntentQuery) FirstID(ctx context.Context) (id uuid.UUID, err error) {
	var ids []uuid.UUID
	if ids, err = piq.Limit(1).IDs(setContextOp(ctx, piq.ctx, ent.OpQueryFirstID)); err != nil {
		return
	}
	if len(ids) == 0 {
		err = &NotFoundError{paymentintent.Label}
		return
	}
	return ids[0], nil
}

// FirstIDX is like FirstID, but panics if an error occurs.
func (piq *PaymentIntentQuery) FirstIDX(ctx context.Context) uuid.UUID {
	id, err := piq.FirstID(ctx)
	if err != nil && !IsNotFound(err) {
		panic(err)
	}
	return id
}

// Only returns a single PaymentIntent entity found by the query, ensuring it only returns one.
// Returns a *NotSingularError when more than one PaymentIntent entity is found.
// Returns a *NotFoundError when no PaymentIntent entities are found.
func (piq *PaymentIntentQuery) Only(ctx context.Context) (*PaymentIntent, error) {
	nodes, err := piq.Limit(2).All(setContextOp(ctx, piq.ctx, ent.OpQueryOnly))
	if err != nil {
		return nil, err
	}
	switch len(nodes) {
	case 1:
		return nodes[0], nil
	case 0:
		return nil, &NotFoundError{paymentintent.Label}
	default:
		return nil, &NotSingularError{paymentintent.Label}
	}
}

// OnlyX is like Only, but panics if an error occurs.
func (piq *PaymentIntentQuery) OnlyX(ctx context.Context) *PaymentIntent {
	node, err := piq.Only(ctx)
	if err != nil {
		panic(err)
	}
	return node
}

// OnlyID is like Only, but returns the only PaymentIntent ID in the query.
// Returns a *NotSingularError when more than one PaymentIntent ID is found.
// Returns a *NotFoundError when no entities are found.
func (piq *PaymentIntentQuery) OnlyID(ctx context.Context) (id uuid.UUID, err error) {
	var ids []uuid.UUID
	if ids, err = piq.Limit(2).IDs(setContextOp(ctx, piq.ctx, ent.OpQueryOnlyID)); err != nil {
		return
	}
	switch len(ids) {
	case 1:
		id = ids[0]
	case 0:
		err = &NotFoundError{paymentintent.Label}
	default:
		err = &NotSingularError{paymentintent.Label}
	}
	return
}

// OnlyIDX is like OnlyID, but panics if an error occurs.
func (piq *PaymentIntentQuery) OnlyIDX(ctx context.Context) uuid.UUID {
	id, err := piq.OnlyID(ctx)
	if err != nil {
		panic(err)
	}
	return id
}

// All executes the query and returns a list of PaymentIntents.
func (piq *PaymentIntentQuery) All(ctx context.Context) ([]*PaymentIntent, error) {
	ctx = setContextOp(ctx, piq.ctx, ent.OpQueryAll)
	if err := piq.prepareQuery(ctx); err != nil {
		return nil, err
	}
	qr := querierAll[[]*PaymentIntent, *PaymentIntentQuery]()
	return withInterceptors[[]*PaymentIntent](ctx, piq, qr, piq.inters)
}

// AllX is like All, but panics if an error occurs.
func (piq *PaymentIntentQuery) AllX(ctx context.Context) []*PaymentIntent {
	nodes, err := piq.All(ctx)
	if err != nil {
		panic(err)
	}
	return nodes
}

// IDs executes the query and returns a list of PaymentIntent IDs.
func (piq *PaymentIntentQuery) IDs(ctx context.Context) (ids []uuid.UUID, err error) {
	if piq.ctx.Unique == nil && piq.path != nil {
		piq.Unique(true)
	}
	ctx = setContextOp(ctx, piq.ctx, ent.OpQueryIDs)
	if err = piq.Select(paymentintent.FieldID).Scan(ctx, &ids); err != nil {
		return nil, err
	}
	return ids, nil
}

// IDsX is like IDs, but panics if an error occurs.
func (piq *PaymentIntentQuery) IDsX(ctx context.Context) []uuid.UUID {
	ids, err := piq.IDs(ctx)
	if err != nil {
		panic(err)
	}
	return ids
}

// Count returns the count of the given query.
func (piq *PaymentIntentQuery) Count(ctx context.Context) (int, error) {
	ctx = setContextOp(ctx, piq.ctx, ent.OpQueryCount)
	if err := piq.prepareQuery(ctx); err != nil {
		return 0, err
	}
	return withInterceptors[int](ctx, piq, querierCount[*PaymentIntentQuery](), piq.inters)
}

// CountX is like Count, but panics if an error occurs.
func (piq *PaymentIntentQuery) CountX(ctx context.Context) int {
	count, err := piq.Count(ctx)
	if err != nil {
		panic(err)
	}
	return count
}

// Exist returns true if the query has elements in the graph.
func (piq *PaymentIntentQuery) Exist(ctx context.Context) (bool, error) {
	ctx = setContextOp(ctx, piq.ctx, ent.OpQueryExist)
	switch _, err := piq.FirstID(ctx); {
	case IsNotFound(err):
		return false, nil
	case err != nil:
		return false, fmt.Errorf("ent: check existence: %w", err)
	default:
		return true, nil
	}
}

// ExistX is like Exist, but panics if an error occurs.
func (piq *PaymentIntentQuery) ExistX(ctx context.Context) bool {
	exist, err := piq.Exist(ctx)
	if err != nil {
		panic(err)
	}
	return exist
}

// Clone returns a duplicate of the PaymentIntentQuery builder, including all associated steps. It can be
// used to prepare common query builders and use them differently after the clone is made.
func (piq *PaymentIntentQuery) Clone() *PaymentIntentQuery {
	if piq == nil {
		return nil
	}
	return &PaymentIntentQuery{
		config:               piq.config,
		ctx:                  piq.ctx.Clone(),
		order:                append([]paymentintent.OrderOption{}, piq.order...),
		inters:               append([]Interceptor{}, piq.inters...),
		predicates:           append([]predicate.PaymentIntent{}, piq.predicates...),
		withTransfer:         piq.withTransfer.Clone(),
		withTokenTransaction: piq.withTokenTransaction.Clone(),
		// clone intermediate query.
		sql:  piq.sql.Clone(),
		path: piq.path,
	}
}

// WithTransfer tells the query-builder to eager-load the nodes that are connected to
// the "transfer" edge. The optional arguments are used to configure the query builder of the edge.
func (piq *PaymentIntentQuery) WithTransfer(opts ...func(*TransferQuery)) *PaymentIntentQuery {
	query := (&TransferClient{config: piq.config}).Query()
	for _, opt := range opts {
		opt(query)
	}
	piq.withTransfer = query
	return piq
}

// WithTokenTransaction tells the query-builder to eager-load the nodes that are connected to
// the "token_transaction" edge. The optional arguments are used to configure the query builder of the edge.
func (piq *PaymentIntentQuery) WithTokenTransaction(opts ...func(*TokenTransactionQuery)) *PaymentIntentQuery {
	query := (&TokenTransactionClient{config: piq.config}).Query()
	for _, opt := range opts {
		opt(query)
	}
	piq.withTokenTransaction = query
	return piq
}

// GroupBy is used to group vertices by one or more fields/columns.
// It is often used with aggregate functions, like: count, max, mean, min, sum.
//
// Example:
//
//	var v []struct {
//		CreateTime time.Time `json:"create_time,omitempty"`
//		Count int `json:"count,omitempty"`
//	}
//
//	client.PaymentIntent.Query().
//		GroupBy(paymentintent.FieldCreateTime).
//		Aggregate(ent.Count()).
//		Scan(ctx, &v)
func (piq *PaymentIntentQuery) GroupBy(field string, fields ...string) *PaymentIntentGroupBy {
	piq.ctx.Fields = append([]string{field}, fields...)
	grbuild := &PaymentIntentGroupBy{build: piq}
	grbuild.flds = &piq.ctx.Fields
	grbuild.label = paymentintent.Label
	grbuild.scan = grbuild.Scan
	return grbuild
}

// Select allows the selection one or more fields/columns for the given query,
// instead of selecting all fields in the entity.
//
// Example:
//
//	var v []struct {
//		CreateTime time.Time `json:"create_time,omitempty"`
//	}
//
//	client.PaymentIntent.Query().
//		Select(paymentintent.FieldCreateTime).
//		Scan(ctx, &v)
func (piq *PaymentIntentQuery) Select(fields ...string) *PaymentIntentSelect {
	piq.ctx.Fields = append(piq.ctx.Fields, fields...)
	sbuild := &PaymentIntentSelect{PaymentIntentQuery: piq}
	sbuild.label = paymentintent.Label
	sbuild.flds, sbuild.scan = &piq.ctx.Fields, sbuild.Scan
	return sbuild
}

// Aggregate returns a PaymentIntentSelect configured with the given aggregations.
func (piq *PaymentIntentQuery) Aggregate(fns ...AggregateFunc) *PaymentIntentSelect {
	return piq.Select().Aggregate(fns...)
}

func (piq *PaymentIntentQuery) prepareQuery(ctx context.Context) error {
	for _, inter := range piq.inters {
		if inter == nil {
			return fmt.Errorf("ent: uninitialized interceptor (forgotten import ent/runtime?)")
		}
		if trv, ok := inter.(Traverser); ok {
			if err := trv.Traverse(ctx, piq); err != nil {
				return err
			}
		}
	}
	for _, f := range piq.ctx.Fields {
		if !paymentintent.ValidColumn(f) {
			return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
		}
	}
	if piq.path != nil {
		prev, err := piq.path(ctx)
		if err != nil {
			return err
		}
		piq.sql = prev
	}
	return nil
}

func (piq *PaymentIntentQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*PaymentIntent, error) {
	var (
		nodes       = []*PaymentIntent{}
		_spec       = piq.querySpec()
		loadedTypes = [2]bool{
			piq.withTransfer != nil,
			piq.withTokenTransaction != nil,
		}
	)
	_spec.ScanValues = func(columns []string) ([]any, error) {
		return (*PaymentIntent).scanValues(nil, columns)
	}
	_spec.Assign = func(columns []string, values []any) error {
		node := &PaymentIntent{config: piq.config}
		nodes = append(nodes, node)
		node.Edges.loadedTypes = loadedTypes
		return node.assignValues(columns, values)
	}
	if len(piq.modifiers) > 0 {
		_spec.Modifiers = piq.modifiers
	}
	for i := range hooks {
		hooks[i](ctx, _spec)
	}
	if err := sqlgraph.QueryNodes(ctx, piq.driver, _spec); err != nil {
		return nil, err
	}
	if len(nodes) == 0 {
		return nodes, nil
	}
	if query := piq.withTransfer; query != nil {
		if err := piq.loadTransfer(ctx, query, nodes,
			func(n *PaymentIntent) { n.Edges.Transfer = []*Transfer{} },
			func(n *PaymentIntent, e *Transfer) { n.Edges.Transfer = append(n.Edges.Transfer, e) }); err != nil {
			return nil, err
		}
	}
	if query := piq.withTokenTransaction; query != nil {
		if err := piq.loadTokenTransaction(ctx, query, nodes,
			func(n *PaymentIntent) { n.Edges.TokenTransaction = []*TokenTransaction{} },
			func(n *PaymentIntent, e *TokenTransaction) {
				n.Edges.TokenTransaction = append(n.Edges.TokenTransaction, e)
			}); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

func (piq *PaymentIntentQuery) loadTransfer(ctx context.Context, query *TransferQuery, nodes []*PaymentIntent, init func(*PaymentIntent), assign func(*PaymentIntent, *Transfer)) error {
	fks := make([]driver.Value, 0, len(nodes))
	nodeids := make(map[uuid.UUID]*PaymentIntent)
	for i := range nodes {
		fks = append(fks, nodes[i].ID)
		nodeids[nodes[i].ID] = nodes[i]
		if init != nil {
			init(nodes[i])
		}
	}
	query.withFKs = true
	query.Where(predicate.Transfer(func(s *sql.Selector) {
		s.Where(sql.InValues(s.C(paymentintent.TransferColumn), fks...))
	}))
	neighbors, err := query.All(ctx)
	if err != nil {
		return err
	}
	for _, n := range neighbors {
		fk := n.transfer_payment_intent
		if fk == nil {
			return fmt.Errorf(`foreign-key "transfer_payment_intent" is nil for node %v`, n.ID)
		}
		node, ok := nodeids[*fk]
		if !ok {
			return fmt.Errorf(`unexpected referenced foreign-key "transfer_payment_intent" returned %v for node %v`, *fk, n.ID)
		}
		assign(node, n)
	}
	return nil
}
func (piq *PaymentIntentQuery) loadTokenTransaction(ctx context.Context, query *TokenTransactionQuery, nodes []*PaymentIntent, init func(*PaymentIntent), assign func(*PaymentIntent, *TokenTransaction)) error {
	fks := make([]driver.Value, 0, len(nodes))
	nodeids := make(map[uuid.UUID]*PaymentIntent)
	for i := range nodes {
		fks = append(fks, nodes[i].ID)
		nodeids[nodes[i].ID] = nodes[i]
		if init != nil {
			init(nodes[i])
		}
	}
	query.withFKs = true
	query.Where(predicate.TokenTransaction(func(s *sql.Selector) {
		s.Where(sql.InValues(s.C(paymentintent.TokenTransactionColumn), fks...))
	}))
	neighbors, err := query.All(ctx)
	if err != nil {
		return err
	}
	for _, n := range neighbors {
		fk := n.token_transaction_payment_intent
		if fk == nil {
			return fmt.Errorf(`foreign-key "token_transaction_payment_intent" is nil for node %v`, n.ID)
		}
		node, ok := nodeids[*fk]
		if !ok {
			return fmt.Errorf(`unexpected referenced foreign-key "token_transaction_payment_intent" returned %v for node %v`, *fk, n.ID)
		}
		assign(node, n)
	}
	return nil
}

func (piq *PaymentIntentQuery) sqlCount(ctx context.Context) (int, error) {
	_spec := piq.querySpec()
	if len(piq.modifiers) > 0 {
		_spec.Modifiers = piq.modifiers
	}
	_spec.Node.Columns = piq.ctx.Fields
	if len(piq.ctx.Fields) > 0 {
		_spec.Unique = piq.ctx.Unique != nil && *piq.ctx.Unique
	}
	return sqlgraph.CountNodes(ctx, piq.driver, _spec)
}

func (piq *PaymentIntentQuery) querySpec() *sqlgraph.QuerySpec {
	_spec := sqlgraph.NewQuerySpec(paymentintent.Table, paymentintent.Columns, sqlgraph.NewFieldSpec(paymentintent.FieldID, field.TypeUUID))
	_spec.From = piq.sql
	if unique := piq.ctx.Unique; unique != nil {
		_spec.Unique = *unique
	} else if piq.path != nil {
		_spec.Unique = true
	}
	if fields := piq.ctx.Fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, paymentintent.FieldID)
		for i := range fields {
			if fields[i] != paymentintent.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, fields[i])
			}
		}
	}
	if ps := piq.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if limit := piq.ctx.Limit; limit != nil {
		_spec.Limit = *limit
	}
	if offset := piq.ctx.Offset; offset != nil {
		_spec.Offset = *offset
	}
	if ps := piq.order; len(ps) > 0 {
		_spec.Order = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	return _spec
}

func (piq *PaymentIntentQuery) sqlQuery(ctx context.Context) *sql.Selector {
	builder := sql.Dialect(piq.driver.Dialect())
	t1 := builder.Table(paymentintent.Table)
	columns := piq.ctx.Fields
	if len(columns) == 0 {
		columns = paymentintent.Columns
	}
	selector := builder.Select(t1.Columns(columns...)...).From(t1)
	if piq.sql != nil {
		selector = piq.sql
		selector.Select(selector.Columns(columns...)...)
	}
	if piq.ctx.Unique != nil && *piq.ctx.Unique {
		selector.Distinct()
	}
	for _, m := range piq.modifiers {
		m(selector)
	}
	for _, p := range piq.predicates {
		p(selector)
	}
	for _, p := range piq.order {
		p(selector)
	}
	if offset := piq.ctx.Offset; offset != nil {
		// limit is mandatory for offset clause. We start
		// with default value, and override it below if needed.
		selector.Offset(*offset).Limit(math.MaxInt32)
	}
	if limit := piq.ctx.Limit; limit != nil {
		selector.Limit(*limit)
	}
	return selector
}

// ForUpdate locks the selected rows against concurrent updates, and prevent them from being
// updated, deleted or "selected ... for update" by other sessions, until the transaction is
// either committed or rolled-back.
func (piq *PaymentIntentQuery) ForUpdate(opts ...sql.LockOption) *PaymentIntentQuery {
	if piq.driver.Dialect() == dialect.Postgres {
		piq.Unique(false)
	}
	piq.modifiers = append(piq.modifiers, func(s *sql.Selector) {
		s.ForUpdate(opts...)
	})
	return piq
}

// ForShare behaves similarly to ForUpdate, except that it acquires a shared mode lock
// on any rows that are read. Other sessions can read the rows, but cannot modify them
// until your transaction commits.
func (piq *PaymentIntentQuery) ForShare(opts ...sql.LockOption) *PaymentIntentQuery {
	if piq.driver.Dialect() == dialect.Postgres {
		piq.Unique(false)
	}
	piq.modifiers = append(piq.modifiers, func(s *sql.Selector) {
		s.ForShare(opts...)
	})
	return piq
}

// PaymentIntentGroupBy is the group-by builder for PaymentIntent entities.
type PaymentIntentGroupBy struct {
	selector
	build *PaymentIntentQuery
}

// Aggregate adds the given aggregation functions to the group-by query.
func (pigb *PaymentIntentGroupBy) Aggregate(fns ...AggregateFunc) *PaymentIntentGroupBy {
	pigb.fns = append(pigb.fns, fns...)
	return pigb
}

// Scan applies the selector query and scans the result into the given value.
func (pigb *PaymentIntentGroupBy) Scan(ctx context.Context, v any) error {
	ctx = setContextOp(ctx, pigb.build.ctx, ent.OpQueryGroupBy)
	if err := pigb.build.prepareQuery(ctx); err != nil {
		return err
	}
	return scanWithInterceptors[*PaymentIntentQuery, *PaymentIntentGroupBy](ctx, pigb.build, pigb, pigb.build.inters, v)
}

func (pigb *PaymentIntentGroupBy) sqlScan(ctx context.Context, root *PaymentIntentQuery, v any) error {
	selector := root.sqlQuery(ctx).Select()
	aggregation := make([]string, 0, len(pigb.fns))
	for _, fn := range pigb.fns {
		aggregation = append(aggregation, fn(selector))
	}
	if len(selector.SelectedColumns()) == 0 {
		columns := make([]string, 0, len(*pigb.flds)+len(pigb.fns))
		for _, f := range *pigb.flds {
			columns = append(columns, selector.C(f))
		}
		columns = append(columns, aggregation...)
		selector.Select(columns...)
	}
	selector.GroupBy(selector.Columns(*pigb.flds...)...)
	if err := selector.Err(); err != nil {
		return err
	}
	rows := &sql.Rows{}
	query, args := selector.Query()
	if err := pigb.build.driver.Query(ctx, query, args, rows); err != nil {
		return err
	}
	defer rows.Close()
	return sql.ScanSlice(rows, v)
}

// PaymentIntentSelect is the builder for selecting fields of PaymentIntent entities.
type PaymentIntentSelect struct {
	*PaymentIntentQuery
	selector
}

// Aggregate adds the given aggregation functions to the selector query.
func (pis *PaymentIntentSelect) Aggregate(fns ...AggregateFunc) *PaymentIntentSelect {
	pis.fns = append(pis.fns, fns...)
	return pis
}

// Scan applies the selector query and scans the result into the given value.
func (pis *PaymentIntentSelect) Scan(ctx context.Context, v any) error {
	ctx = setContextOp(ctx, pis.ctx, ent.OpQuerySelect)
	if err := pis.prepareQuery(ctx); err != nil {
		return err
	}
	return scanWithInterceptors[*PaymentIntentQuery, *PaymentIntentSelect](ctx, pis.PaymentIntentQuery, pis, pis.inters, v)
}

func (pis *PaymentIntentSelect) sqlScan(ctx context.Context, root *PaymentIntentQuery, v any) error {
	selector := root.sqlQuery(ctx)
	aggregation := make([]string, 0, len(pis.fns))
	for _, fn := range pis.fns {
		aggregation = append(aggregation, fn(selector))
	}
	switch n := len(*pis.selector.flds); {
	case n == 0 && len(aggregation) > 0:
		selector.Select(aggregation...)
	case n != 0 && len(aggregation) > 0:
		selector.AppendSelect(aggregation...)
	}
	rows := &sql.Rows{}
	query, args := selector.Query()
	if err := pis.driver.Query(ctx, query, args, rows); err != nil {
		return err
	}
	defer rows.Close()
	return sql.ScanSlice(rows, v)
}
