// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"

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

// PaymentIntentUpdate is the builder for updating PaymentIntent entities.
type PaymentIntentUpdate struct {
	config
	hooks    []Hook
	mutation *PaymentIntentMutation
}

// Where appends a list predicates to the PaymentIntentUpdate builder.
func (piu *PaymentIntentUpdate) Where(ps ...predicate.PaymentIntent) *PaymentIntentUpdate {
	piu.mutation.Where(ps...)
	return piu
}

// SetUpdateTime sets the "update_time" field.
func (piu *PaymentIntentUpdate) SetUpdateTime(t time.Time) *PaymentIntentUpdate {
	piu.mutation.SetUpdateTime(t)
	return piu
}

// AddTransferIDs adds the "transfer" edge to the Transfer entity by IDs.
func (piu *PaymentIntentUpdate) AddTransferIDs(ids ...uuid.UUID) *PaymentIntentUpdate {
	piu.mutation.AddTransferIDs(ids...)
	return piu
}

// AddTransfer adds the "transfer" edges to the Transfer entity.
func (piu *PaymentIntentUpdate) AddTransfer(t ...*Transfer) *PaymentIntentUpdate {
	ids := make([]uuid.UUID, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return piu.AddTransferIDs(ids...)
}

// AddTokenTransactionIDs adds the "token_transaction" edge to the TokenTransaction entity by IDs.
func (piu *PaymentIntentUpdate) AddTokenTransactionIDs(ids ...uuid.UUID) *PaymentIntentUpdate {
	piu.mutation.AddTokenTransactionIDs(ids...)
	return piu
}

// AddTokenTransaction adds the "token_transaction" edges to the TokenTransaction entity.
func (piu *PaymentIntentUpdate) AddTokenTransaction(t ...*TokenTransaction) *PaymentIntentUpdate {
	ids := make([]uuid.UUID, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return piu.AddTokenTransactionIDs(ids...)
}

// Mutation returns the PaymentIntentMutation object of the builder.
func (piu *PaymentIntentUpdate) Mutation() *PaymentIntentMutation {
	return piu.mutation
}

// ClearTransfer clears all "transfer" edges to the Transfer entity.
func (piu *PaymentIntentUpdate) ClearTransfer() *PaymentIntentUpdate {
	piu.mutation.ClearTransfer()
	return piu
}

// RemoveTransferIDs removes the "transfer" edge to Transfer entities by IDs.
func (piu *PaymentIntentUpdate) RemoveTransferIDs(ids ...uuid.UUID) *PaymentIntentUpdate {
	piu.mutation.RemoveTransferIDs(ids...)
	return piu
}

// RemoveTransfer removes "transfer" edges to Transfer entities.
func (piu *PaymentIntentUpdate) RemoveTransfer(t ...*Transfer) *PaymentIntentUpdate {
	ids := make([]uuid.UUID, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return piu.RemoveTransferIDs(ids...)
}

// ClearTokenTransaction clears all "token_transaction" edges to the TokenTransaction entity.
func (piu *PaymentIntentUpdate) ClearTokenTransaction() *PaymentIntentUpdate {
	piu.mutation.ClearTokenTransaction()
	return piu
}

// RemoveTokenTransactionIDs removes the "token_transaction" edge to TokenTransaction entities by IDs.
func (piu *PaymentIntentUpdate) RemoveTokenTransactionIDs(ids ...uuid.UUID) *PaymentIntentUpdate {
	piu.mutation.RemoveTokenTransactionIDs(ids...)
	return piu
}

// RemoveTokenTransaction removes "token_transaction" edges to TokenTransaction entities.
func (piu *PaymentIntentUpdate) RemoveTokenTransaction(t ...*TokenTransaction) *PaymentIntentUpdate {
	ids := make([]uuid.UUID, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return piu.RemoveTokenTransactionIDs(ids...)
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (piu *PaymentIntentUpdate) Save(ctx context.Context) (int, error) {
	if err := piu.defaults(); err != nil {
		return 0, err
	}
	return withHooks(ctx, piu.sqlSave, piu.mutation, piu.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (piu *PaymentIntentUpdate) SaveX(ctx context.Context) int {
	affected, err := piu.Save(ctx)
	if err != nil {
		panic(err)
	}
	return affected
}

// Exec executes the query.
func (piu *PaymentIntentUpdate) Exec(ctx context.Context) error {
	_, err := piu.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (piu *PaymentIntentUpdate) ExecX(ctx context.Context) {
	if err := piu.Exec(ctx); err != nil {
		panic(err)
	}
}

// defaults sets the default values of the builder before save.
func (piu *PaymentIntentUpdate) defaults() error {
	if _, ok := piu.mutation.UpdateTime(); !ok {
		if paymentintent.UpdateDefaultUpdateTime == nil {
			return fmt.Errorf("ent: uninitialized paymentintent.UpdateDefaultUpdateTime (forgotten import ent/runtime?)")
		}
		v := paymentintent.UpdateDefaultUpdateTime()
		piu.mutation.SetUpdateTime(v)
	}
	return nil
}

func (piu *PaymentIntentUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(paymentintent.Table, paymentintent.Columns, sqlgraph.NewFieldSpec(paymentintent.FieldID, field.TypeUUID))
	if ps := piu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := piu.mutation.UpdateTime(); ok {
		_spec.SetField(paymentintent.FieldUpdateTime, field.TypeTime, value)
	}
	if piu.mutation.TransferCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   paymentintent.TransferTable,
			Columns: []string{paymentintent.TransferColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(transfer.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := piu.mutation.RemovedTransferIDs(); len(nodes) > 0 && !piu.mutation.TransferCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   paymentintent.TransferTable,
			Columns: []string{paymentintent.TransferColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(transfer.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := piu.mutation.TransferIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   paymentintent.TransferTable,
			Columns: []string{paymentintent.TransferColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(transfer.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if piu.mutation.TokenTransactionCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   paymentintent.TokenTransactionTable,
			Columns: []string{paymentintent.TokenTransactionColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(tokentransaction.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := piu.mutation.RemovedTokenTransactionIDs(); len(nodes) > 0 && !piu.mutation.TokenTransactionCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   paymentintent.TokenTransactionTable,
			Columns: []string{paymentintent.TokenTransactionColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(tokentransaction.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := piu.mutation.TokenTransactionIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   paymentintent.TokenTransactionTable,
			Columns: []string{paymentintent.TokenTransactionColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(tokentransaction.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, piu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{paymentintent.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	piu.mutation.done = true
	return n, nil
}

// PaymentIntentUpdateOne is the builder for updating a single PaymentIntent entity.
type PaymentIntentUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *PaymentIntentMutation
}

// SetUpdateTime sets the "update_time" field.
func (piuo *PaymentIntentUpdateOne) SetUpdateTime(t time.Time) *PaymentIntentUpdateOne {
	piuo.mutation.SetUpdateTime(t)
	return piuo
}

// AddTransferIDs adds the "transfer" edge to the Transfer entity by IDs.
func (piuo *PaymentIntentUpdateOne) AddTransferIDs(ids ...uuid.UUID) *PaymentIntentUpdateOne {
	piuo.mutation.AddTransferIDs(ids...)
	return piuo
}

// AddTransfer adds the "transfer" edges to the Transfer entity.
func (piuo *PaymentIntentUpdateOne) AddTransfer(t ...*Transfer) *PaymentIntentUpdateOne {
	ids := make([]uuid.UUID, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return piuo.AddTransferIDs(ids...)
}

// AddTokenTransactionIDs adds the "token_transaction" edge to the TokenTransaction entity by IDs.
func (piuo *PaymentIntentUpdateOne) AddTokenTransactionIDs(ids ...uuid.UUID) *PaymentIntentUpdateOne {
	piuo.mutation.AddTokenTransactionIDs(ids...)
	return piuo
}

// AddTokenTransaction adds the "token_transaction" edges to the TokenTransaction entity.
func (piuo *PaymentIntentUpdateOne) AddTokenTransaction(t ...*TokenTransaction) *PaymentIntentUpdateOne {
	ids := make([]uuid.UUID, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return piuo.AddTokenTransactionIDs(ids...)
}

// Mutation returns the PaymentIntentMutation object of the builder.
func (piuo *PaymentIntentUpdateOne) Mutation() *PaymentIntentMutation {
	return piuo.mutation
}

// ClearTransfer clears all "transfer" edges to the Transfer entity.
func (piuo *PaymentIntentUpdateOne) ClearTransfer() *PaymentIntentUpdateOne {
	piuo.mutation.ClearTransfer()
	return piuo
}

// RemoveTransferIDs removes the "transfer" edge to Transfer entities by IDs.
func (piuo *PaymentIntentUpdateOne) RemoveTransferIDs(ids ...uuid.UUID) *PaymentIntentUpdateOne {
	piuo.mutation.RemoveTransferIDs(ids...)
	return piuo
}

// RemoveTransfer removes "transfer" edges to Transfer entities.
func (piuo *PaymentIntentUpdateOne) RemoveTransfer(t ...*Transfer) *PaymentIntentUpdateOne {
	ids := make([]uuid.UUID, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return piuo.RemoveTransferIDs(ids...)
}

// ClearTokenTransaction clears all "token_transaction" edges to the TokenTransaction entity.
func (piuo *PaymentIntentUpdateOne) ClearTokenTransaction() *PaymentIntentUpdateOne {
	piuo.mutation.ClearTokenTransaction()
	return piuo
}

// RemoveTokenTransactionIDs removes the "token_transaction" edge to TokenTransaction entities by IDs.
func (piuo *PaymentIntentUpdateOne) RemoveTokenTransactionIDs(ids ...uuid.UUID) *PaymentIntentUpdateOne {
	piuo.mutation.RemoveTokenTransactionIDs(ids...)
	return piuo
}

// RemoveTokenTransaction removes "token_transaction" edges to TokenTransaction entities.
func (piuo *PaymentIntentUpdateOne) RemoveTokenTransaction(t ...*TokenTransaction) *PaymentIntentUpdateOne {
	ids := make([]uuid.UUID, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return piuo.RemoveTokenTransactionIDs(ids...)
}

// Where appends a list predicates to the PaymentIntentUpdate builder.
func (piuo *PaymentIntentUpdateOne) Where(ps ...predicate.PaymentIntent) *PaymentIntentUpdateOne {
	piuo.mutation.Where(ps...)
	return piuo
}

// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (piuo *PaymentIntentUpdateOne) Select(field string, fields ...string) *PaymentIntentUpdateOne {
	piuo.fields = append([]string{field}, fields...)
	return piuo
}

// Save executes the query and returns the updated PaymentIntent entity.
func (piuo *PaymentIntentUpdateOne) Save(ctx context.Context) (*PaymentIntent, error) {
	if err := piuo.defaults(); err != nil {
		return nil, err
	}
	return withHooks(ctx, piuo.sqlSave, piuo.mutation, piuo.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (piuo *PaymentIntentUpdateOne) SaveX(ctx context.Context) *PaymentIntent {
	node, err := piuo.Save(ctx)
	if err != nil {
		panic(err)
	}
	return node
}

// Exec executes the query on the entity.
func (piuo *PaymentIntentUpdateOne) Exec(ctx context.Context) error {
	_, err := piuo.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (piuo *PaymentIntentUpdateOne) ExecX(ctx context.Context) {
	if err := piuo.Exec(ctx); err != nil {
		panic(err)
	}
}

// defaults sets the default values of the builder before save.
func (piuo *PaymentIntentUpdateOne) defaults() error {
	if _, ok := piuo.mutation.UpdateTime(); !ok {
		if paymentintent.UpdateDefaultUpdateTime == nil {
			return fmt.Errorf("ent: uninitialized paymentintent.UpdateDefaultUpdateTime (forgotten import ent/runtime?)")
		}
		v := paymentintent.UpdateDefaultUpdateTime()
		piuo.mutation.SetUpdateTime(v)
	}
	return nil
}

func (piuo *PaymentIntentUpdateOne) sqlSave(ctx context.Context) (_node *PaymentIntent, err error) {
	_spec := sqlgraph.NewUpdateSpec(paymentintent.Table, paymentintent.Columns, sqlgraph.NewFieldSpec(paymentintent.FieldID, field.TypeUUID))
	id, ok := piuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "PaymentIntent.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := piuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, paymentintent.FieldID)
		for _, f := range fields {
			if !paymentintent.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != paymentintent.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := piuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := piuo.mutation.UpdateTime(); ok {
		_spec.SetField(paymentintent.FieldUpdateTime, field.TypeTime, value)
	}
	if piuo.mutation.TransferCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   paymentintent.TransferTable,
			Columns: []string{paymentintent.TransferColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(transfer.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := piuo.mutation.RemovedTransferIDs(); len(nodes) > 0 && !piuo.mutation.TransferCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   paymentintent.TransferTable,
			Columns: []string{paymentintent.TransferColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(transfer.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := piuo.mutation.TransferIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   paymentintent.TransferTable,
			Columns: []string{paymentintent.TransferColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(transfer.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if piuo.mutation.TokenTransactionCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   paymentintent.TokenTransactionTable,
			Columns: []string{paymentintent.TokenTransactionColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(tokentransaction.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := piuo.mutation.RemovedTokenTransactionIDs(); len(nodes) > 0 && !piuo.mutation.TokenTransactionCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   paymentintent.TokenTransactionTable,
			Columns: []string{paymentintent.TokenTransactionColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(tokentransaction.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := piuo.mutation.TokenTransactionIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   paymentintent.TokenTransactionTable,
			Columns: []string{paymentintent.TokenTransactionColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(tokentransaction.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &PaymentIntent{config: piuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, piuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{paymentintent.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	piuo.mutation.done = true
	return _node, nil
}
