// 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/depositaddress"
	"github.com/lightsparkdev/spark/so/ent/predicate"
	"github.com/lightsparkdev/spark/so/ent/utxo"
)

// UtxoUpdate is the builder for updating Utxo entities.
type UtxoUpdate struct {
	config
	hooks    []Hook
	mutation *UtxoMutation
}

// Where appends a list predicates to the UtxoUpdate builder.
func (uu *UtxoUpdate) Where(ps ...predicate.Utxo) *UtxoUpdate {
	uu.mutation.Where(ps...)
	return uu
}

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

// SetBlockHeight sets the "block_height" field.
func (uu *UtxoUpdate) SetBlockHeight(i int64) *UtxoUpdate {
	uu.mutation.ResetBlockHeight()
	uu.mutation.SetBlockHeight(i)
	return uu
}

// SetNillableBlockHeight sets the "block_height" field if the given value is not nil.
func (uu *UtxoUpdate) SetNillableBlockHeight(i *int64) *UtxoUpdate {
	if i != nil {
		uu.SetBlockHeight(*i)
	}
	return uu
}

// AddBlockHeight adds i to the "block_height" field.
func (uu *UtxoUpdate) AddBlockHeight(i int64) *UtxoUpdate {
	uu.mutation.AddBlockHeight(i)
	return uu
}

// SetDepositAddressID sets the "deposit_address" edge to the DepositAddress entity by ID.
func (uu *UtxoUpdate) SetDepositAddressID(id uuid.UUID) *UtxoUpdate {
	uu.mutation.SetDepositAddressID(id)
	return uu
}

// SetDepositAddress sets the "deposit_address" edge to the DepositAddress entity.
func (uu *UtxoUpdate) SetDepositAddress(d *DepositAddress) *UtxoUpdate {
	return uu.SetDepositAddressID(d.ID)
}

// Mutation returns the UtxoMutation object of the builder.
func (uu *UtxoUpdate) Mutation() *UtxoMutation {
	return uu.mutation
}

// ClearDepositAddress clears the "deposit_address" edge to the DepositAddress entity.
func (uu *UtxoUpdate) ClearDepositAddress() *UtxoUpdate {
	uu.mutation.ClearDepositAddress()
	return uu
}

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (uu *UtxoUpdate) check() error {
	if uu.mutation.DepositAddressCleared() && len(uu.mutation.DepositAddressIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "Utxo.deposit_address"`)
	}
	return nil
}

func (uu *UtxoUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := uu.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(utxo.Table, utxo.Columns, sqlgraph.NewFieldSpec(utxo.FieldID, field.TypeUUID))
	if ps := uu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := uu.mutation.UpdateTime(); ok {
		_spec.SetField(utxo.FieldUpdateTime, field.TypeTime, value)
	}
	if value, ok := uu.mutation.BlockHeight(); ok {
		_spec.SetField(utxo.FieldBlockHeight, field.TypeInt64, value)
	}
	if value, ok := uu.mutation.AddedBlockHeight(); ok {
		_spec.AddField(utxo.FieldBlockHeight, field.TypeInt64, value)
	}
	if uu.mutation.DepositAddressCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   utxo.DepositAddressTable,
			Columns: []string{utxo.DepositAddressColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(depositaddress.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := uu.mutation.DepositAddressIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   utxo.DepositAddressTable,
			Columns: []string{utxo.DepositAddressColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(depositaddress.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, uu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{utxo.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	uu.mutation.done = true
	return n, nil
}

// UtxoUpdateOne is the builder for updating a single Utxo entity.
type UtxoUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *UtxoMutation
}

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

// SetBlockHeight sets the "block_height" field.
func (uuo *UtxoUpdateOne) SetBlockHeight(i int64) *UtxoUpdateOne {
	uuo.mutation.ResetBlockHeight()
	uuo.mutation.SetBlockHeight(i)
	return uuo
}

// SetNillableBlockHeight sets the "block_height" field if the given value is not nil.
func (uuo *UtxoUpdateOne) SetNillableBlockHeight(i *int64) *UtxoUpdateOne {
	if i != nil {
		uuo.SetBlockHeight(*i)
	}
	return uuo
}

// AddBlockHeight adds i to the "block_height" field.
func (uuo *UtxoUpdateOne) AddBlockHeight(i int64) *UtxoUpdateOne {
	uuo.mutation.AddBlockHeight(i)
	return uuo
}

// SetDepositAddressID sets the "deposit_address" edge to the DepositAddress entity by ID.
func (uuo *UtxoUpdateOne) SetDepositAddressID(id uuid.UUID) *UtxoUpdateOne {
	uuo.mutation.SetDepositAddressID(id)
	return uuo
}

// SetDepositAddress sets the "deposit_address" edge to the DepositAddress entity.
func (uuo *UtxoUpdateOne) SetDepositAddress(d *DepositAddress) *UtxoUpdateOne {
	return uuo.SetDepositAddressID(d.ID)
}

// Mutation returns the UtxoMutation object of the builder.
func (uuo *UtxoUpdateOne) Mutation() *UtxoMutation {
	return uuo.mutation
}

// ClearDepositAddress clears the "deposit_address" edge to the DepositAddress entity.
func (uuo *UtxoUpdateOne) ClearDepositAddress() *UtxoUpdateOne {
	uuo.mutation.ClearDepositAddress()
	return uuo
}

// Where appends a list predicates to the UtxoUpdate builder.
func (uuo *UtxoUpdateOne) Where(ps ...predicate.Utxo) *UtxoUpdateOne {
	uuo.mutation.Where(ps...)
	return uuo
}

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

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (uuo *UtxoUpdateOne) check() error {
	if uuo.mutation.DepositAddressCleared() && len(uuo.mutation.DepositAddressIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "Utxo.deposit_address"`)
	}
	return nil
}

func (uuo *UtxoUpdateOne) sqlSave(ctx context.Context) (_node *Utxo, err error) {
	if err := uuo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(utxo.Table, utxo.Columns, sqlgraph.NewFieldSpec(utxo.FieldID, field.TypeUUID))
	id, ok := uuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Utxo.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := uuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, utxo.FieldID)
		for _, f := range fields {
			if !utxo.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != utxo.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := uuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := uuo.mutation.UpdateTime(); ok {
		_spec.SetField(utxo.FieldUpdateTime, field.TypeTime, value)
	}
	if value, ok := uuo.mutation.BlockHeight(); ok {
		_spec.SetField(utxo.FieldBlockHeight, field.TypeInt64, value)
	}
	if value, ok := uuo.mutation.AddedBlockHeight(); ok {
		_spec.AddField(utxo.FieldBlockHeight, field.TypeInt64, value)
	}
	if uuo.mutation.DepositAddressCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   utxo.DepositAddressTable,
			Columns: []string{utxo.DepositAddressColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(depositaddress.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := uuo.mutation.DepositAddressIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   utxo.DepositAddressTable,
			Columns: []string{utxo.DepositAddressColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(depositaddress.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &Utxo{config: uuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, uuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{utxo.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	uuo.mutation.done = true
	return _node, nil
}
