// 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/predicate"
	"github.com/lightsparkdev/spark/so/ent/sparkinvoice"
	"github.com/lightsparkdev/spark/so/ent/tokentransaction"
	"github.com/lightsparkdev/spark/so/ent/transfer"
)

// SparkInvoiceUpdate is the builder for updating SparkInvoice entities.
type SparkInvoiceUpdate struct {
	config
	hooks    []Hook
	mutation *SparkInvoiceMutation
}

// Where appends a list predicates to the SparkInvoiceUpdate builder.
func (siu *SparkInvoiceUpdate) Where(ps ...predicate.SparkInvoice) *SparkInvoiceUpdate {
	siu.mutation.Where(ps...)
	return siu
}

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

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

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

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

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

// Mutation returns the SparkInvoiceMutation object of the builder.
func (siu *SparkInvoiceUpdate) Mutation() *SparkInvoiceMutation {
	return siu.mutation
}

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

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

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

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

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

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

// Save executes the query and returns the number of nodes affected by the update operation.
func (siu *SparkInvoiceUpdate) Save(ctx context.Context) (int, error) {
	siu.defaults()
	return withHooks(ctx, siu.sqlSave, siu.mutation, siu.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (siu *SparkInvoiceUpdate) defaults() {
	if _, ok := siu.mutation.UpdateTime(); !ok {
		v := sparkinvoice.UpdateDefaultUpdateTime()
		siu.mutation.SetUpdateTime(v)
	}
}

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

// SparkInvoiceUpdateOne is the builder for updating a single SparkInvoice entity.
type SparkInvoiceUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *SparkInvoiceMutation
}

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

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

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

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

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

// Mutation returns the SparkInvoiceMutation object of the builder.
func (siuo *SparkInvoiceUpdateOne) Mutation() *SparkInvoiceMutation {
	return siuo.mutation
}

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

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

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

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

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

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

// Where appends a list predicates to the SparkInvoiceUpdate builder.
func (siuo *SparkInvoiceUpdateOne) Where(ps ...predicate.SparkInvoice) *SparkInvoiceUpdateOne {
	siuo.mutation.Where(ps...)
	return siuo
}

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

// Save executes the query and returns the updated SparkInvoice entity.
func (siuo *SparkInvoiceUpdateOne) Save(ctx context.Context) (*SparkInvoice, error) {
	siuo.defaults()
	return withHooks(ctx, siuo.sqlSave, siuo.mutation, siuo.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (siuo *SparkInvoiceUpdateOne) defaults() {
	if _, ok := siuo.mutation.UpdateTime(); !ok {
		v := sparkinvoice.UpdateDefaultUpdateTime()
		siuo.mutation.SetUpdateTime(v)
	}
}

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