// 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/common/keys"
	"github.com/lightsparkdev/spark/so/ent/depositaddress"
	"github.com/lightsparkdev/spark/so/ent/predicate"
	"github.com/lightsparkdev/spark/so/ent/schema/schematype"
	"github.com/lightsparkdev/spark/so/ent/tree"
	"github.com/lightsparkdev/spark/so/ent/treenode"
)

// TreeUpdate is the builder for updating Tree entities.
type TreeUpdate struct {
	config
	hooks    []Hook
	mutation *TreeMutation
}

// Where appends a list predicates to the TreeUpdate builder.
func (tu *TreeUpdate) Where(ps ...predicate.Tree) *TreeUpdate {
	tu.mutation.Where(ps...)
	return tu
}

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

// SetOwnerIdentityPubkey sets the "owner_identity_pubkey" field.
func (tu *TreeUpdate) SetOwnerIdentityPubkey(k keys.Public) *TreeUpdate {
	tu.mutation.SetOwnerIdentityPubkey(k)
	return tu
}

// SetNillableOwnerIdentityPubkey sets the "owner_identity_pubkey" field if the given value is not nil.
func (tu *TreeUpdate) SetNillableOwnerIdentityPubkey(k *keys.Public) *TreeUpdate {
	if k != nil {
		tu.SetOwnerIdentityPubkey(*k)
	}
	return tu
}

// SetStatus sets the "status" field.
func (tu *TreeUpdate) SetStatus(ss schematype.TreeStatus) *TreeUpdate {
	tu.mutation.SetStatus(ss)
	return tu
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (tu *TreeUpdate) SetNillableStatus(ss *schematype.TreeStatus) *TreeUpdate {
	if ss != nil {
		tu.SetStatus(*ss)
	}
	return tu
}

// SetNetwork sets the "network" field.
func (tu *TreeUpdate) SetNetwork(s schematype.Network) *TreeUpdate {
	tu.mutation.SetNetwork(s)
	return tu
}

// SetNillableNetwork sets the "network" field if the given value is not nil.
func (tu *TreeUpdate) SetNillableNetwork(s *schematype.Network) *TreeUpdate {
	if s != nil {
		tu.SetNetwork(*s)
	}
	return tu
}

// SetBaseTxid sets the "base_txid" field.
func (tu *TreeUpdate) SetBaseTxid(b []byte) *TreeUpdate {
	tu.mutation.SetBaseTxid(b)
	return tu
}

// SetVout sets the "vout" field.
func (tu *TreeUpdate) SetVout(i int16) *TreeUpdate {
	tu.mutation.ResetVout()
	tu.mutation.SetVout(i)
	return tu
}

// SetNillableVout sets the "vout" field if the given value is not nil.
func (tu *TreeUpdate) SetNillableVout(i *int16) *TreeUpdate {
	if i != nil {
		tu.SetVout(*i)
	}
	return tu
}

// AddVout adds i to the "vout" field.
func (tu *TreeUpdate) AddVout(i int16) *TreeUpdate {
	tu.mutation.AddVout(i)
	return tu
}

// SetRootID sets the "root" edge to the TreeNode entity by ID.
func (tu *TreeUpdate) SetRootID(id uuid.UUID) *TreeUpdate {
	tu.mutation.SetRootID(id)
	return tu
}

// SetNillableRootID sets the "root" edge to the TreeNode entity by ID if the given value is not nil.
func (tu *TreeUpdate) SetNillableRootID(id *uuid.UUID) *TreeUpdate {
	if id != nil {
		tu = tu.SetRootID(*id)
	}
	return tu
}

// SetRoot sets the "root" edge to the TreeNode entity.
func (tu *TreeUpdate) SetRoot(t *TreeNode) *TreeUpdate {
	return tu.SetRootID(t.ID)
}

// AddNodeIDs adds the "nodes" edge to the TreeNode entity by IDs.
func (tu *TreeUpdate) AddNodeIDs(ids ...uuid.UUID) *TreeUpdate {
	tu.mutation.AddNodeIDs(ids...)
	return tu
}

// AddNodes adds the "nodes" edges to the TreeNode entity.
func (tu *TreeUpdate) AddNodes(t ...*TreeNode) *TreeUpdate {
	ids := make([]uuid.UUID, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return tu.AddNodeIDs(ids...)
}

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

// SetNillableDepositAddressID sets the "deposit_address" edge to the DepositAddress entity by ID if the given value is not nil.
func (tu *TreeUpdate) SetNillableDepositAddressID(id *uuid.UUID) *TreeUpdate {
	if id != nil {
		tu = tu.SetDepositAddressID(*id)
	}
	return tu
}

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

// Mutation returns the TreeMutation object of the builder.
func (tu *TreeUpdate) Mutation() *TreeMutation {
	return tu.mutation
}

// ClearRoot clears the "root" edge to the TreeNode entity.
func (tu *TreeUpdate) ClearRoot() *TreeUpdate {
	tu.mutation.ClearRoot()
	return tu
}

// ClearNodes clears all "nodes" edges to the TreeNode entity.
func (tu *TreeUpdate) ClearNodes() *TreeUpdate {
	tu.mutation.ClearNodes()
	return tu
}

// RemoveNodeIDs removes the "nodes" edge to TreeNode entities by IDs.
func (tu *TreeUpdate) RemoveNodeIDs(ids ...uuid.UUID) *TreeUpdate {
	tu.mutation.RemoveNodeIDs(ids...)
	return tu
}

// RemoveNodes removes "nodes" edges to TreeNode entities.
func (tu *TreeUpdate) RemoveNodes(t ...*TreeNode) *TreeUpdate {
	ids := make([]uuid.UUID, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return tu.RemoveNodeIDs(ids...)
}

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

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (tu *TreeUpdate) check() error {
	if v, ok := tu.mutation.Status(); ok {
		if err := tree.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "Tree.status": %w`, err)}
		}
	}
	if v, ok := tu.mutation.Network(); ok {
		if err := tree.NetworkValidator(v); err != nil {
			return &ValidationError{Name: "network", err: fmt.Errorf(`ent: validator failed for field "Tree.network": %w`, err)}
		}
	}
	if v, ok := tu.mutation.BaseTxid(); ok {
		if err := tree.BaseTxidValidator(v); err != nil {
			return &ValidationError{Name: "base_txid", err: fmt.Errorf(`ent: validator failed for field "Tree.base_txid": %w`, err)}
		}
	}
	if v, ok := tu.mutation.Vout(); ok {
		if err := tree.VoutValidator(v); err != nil {
			return &ValidationError{Name: "vout", err: fmt.Errorf(`ent: validator failed for field "Tree.vout": %w`, err)}
		}
	}
	return nil
}

func (tu *TreeUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := tu.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(tree.Table, tree.Columns, sqlgraph.NewFieldSpec(tree.FieldID, field.TypeUUID))
	if ps := tu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := tu.mutation.UpdateTime(); ok {
		_spec.SetField(tree.FieldUpdateTime, field.TypeTime, value)
	}
	if value, ok := tu.mutation.OwnerIdentityPubkey(); ok {
		_spec.SetField(tree.FieldOwnerIdentityPubkey, field.TypeBytes, value)
	}
	if value, ok := tu.mutation.Status(); ok {
		_spec.SetField(tree.FieldStatus, field.TypeEnum, value)
	}
	if value, ok := tu.mutation.Network(); ok {
		_spec.SetField(tree.FieldNetwork, field.TypeEnum, value)
	}
	if value, ok := tu.mutation.BaseTxid(); ok {
		_spec.SetField(tree.FieldBaseTxid, field.TypeBytes, value)
	}
	if value, ok := tu.mutation.Vout(); ok {
		_spec.SetField(tree.FieldVout, field.TypeInt16, value)
	}
	if value, ok := tu.mutation.AddedVout(); ok {
		_spec.AddField(tree.FieldVout, field.TypeInt16, value)
	}
	if tu.mutation.RootCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   tree.RootTable,
			Columns: []string{tree.RootColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(treenode.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tu.mutation.RootIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   tree.RootTable,
			Columns: []string{tree.RootColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(treenode.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if tu.mutation.NodesCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   tree.NodesTable,
			Columns: []string{tree.NodesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(treenode.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tu.mutation.RemovedNodesIDs(); len(nodes) > 0 && !tu.mutation.NodesCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   tree.NodesTable,
			Columns: []string{tree.NodesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(treenode.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 := tu.mutation.NodesIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   tree.NodesTable,
			Columns: []string{tree.NodesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(treenode.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if tu.mutation.DepositAddressCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   tree.DepositAddressTable,
			Columns: []string{tree.DepositAddressColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(depositaddress.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tu.mutation.DepositAddressIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   tree.DepositAddressTable,
			Columns: []string{tree.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, tu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{tree.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	tu.mutation.done = true
	return n, nil
}

// TreeUpdateOne is the builder for updating a single Tree entity.
type TreeUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *TreeMutation
}

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

// SetOwnerIdentityPubkey sets the "owner_identity_pubkey" field.
func (tuo *TreeUpdateOne) SetOwnerIdentityPubkey(k keys.Public) *TreeUpdateOne {
	tuo.mutation.SetOwnerIdentityPubkey(k)
	return tuo
}

// SetNillableOwnerIdentityPubkey sets the "owner_identity_pubkey" field if the given value is not nil.
func (tuo *TreeUpdateOne) SetNillableOwnerIdentityPubkey(k *keys.Public) *TreeUpdateOne {
	if k != nil {
		tuo.SetOwnerIdentityPubkey(*k)
	}
	return tuo
}

// SetStatus sets the "status" field.
func (tuo *TreeUpdateOne) SetStatus(ss schematype.TreeStatus) *TreeUpdateOne {
	tuo.mutation.SetStatus(ss)
	return tuo
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (tuo *TreeUpdateOne) SetNillableStatus(ss *schematype.TreeStatus) *TreeUpdateOne {
	if ss != nil {
		tuo.SetStatus(*ss)
	}
	return tuo
}

// SetNetwork sets the "network" field.
func (tuo *TreeUpdateOne) SetNetwork(s schematype.Network) *TreeUpdateOne {
	tuo.mutation.SetNetwork(s)
	return tuo
}

// SetNillableNetwork sets the "network" field if the given value is not nil.
func (tuo *TreeUpdateOne) SetNillableNetwork(s *schematype.Network) *TreeUpdateOne {
	if s != nil {
		tuo.SetNetwork(*s)
	}
	return tuo
}

// SetBaseTxid sets the "base_txid" field.
func (tuo *TreeUpdateOne) SetBaseTxid(b []byte) *TreeUpdateOne {
	tuo.mutation.SetBaseTxid(b)
	return tuo
}

// SetVout sets the "vout" field.
func (tuo *TreeUpdateOne) SetVout(i int16) *TreeUpdateOne {
	tuo.mutation.ResetVout()
	tuo.mutation.SetVout(i)
	return tuo
}

// SetNillableVout sets the "vout" field if the given value is not nil.
func (tuo *TreeUpdateOne) SetNillableVout(i *int16) *TreeUpdateOne {
	if i != nil {
		tuo.SetVout(*i)
	}
	return tuo
}

// AddVout adds i to the "vout" field.
func (tuo *TreeUpdateOne) AddVout(i int16) *TreeUpdateOne {
	tuo.mutation.AddVout(i)
	return tuo
}

// SetRootID sets the "root" edge to the TreeNode entity by ID.
func (tuo *TreeUpdateOne) SetRootID(id uuid.UUID) *TreeUpdateOne {
	tuo.mutation.SetRootID(id)
	return tuo
}

// SetNillableRootID sets the "root" edge to the TreeNode entity by ID if the given value is not nil.
func (tuo *TreeUpdateOne) SetNillableRootID(id *uuid.UUID) *TreeUpdateOne {
	if id != nil {
		tuo = tuo.SetRootID(*id)
	}
	return tuo
}

// SetRoot sets the "root" edge to the TreeNode entity.
func (tuo *TreeUpdateOne) SetRoot(t *TreeNode) *TreeUpdateOne {
	return tuo.SetRootID(t.ID)
}

// AddNodeIDs adds the "nodes" edge to the TreeNode entity by IDs.
func (tuo *TreeUpdateOne) AddNodeIDs(ids ...uuid.UUID) *TreeUpdateOne {
	tuo.mutation.AddNodeIDs(ids...)
	return tuo
}

// AddNodes adds the "nodes" edges to the TreeNode entity.
func (tuo *TreeUpdateOne) AddNodes(t ...*TreeNode) *TreeUpdateOne {
	ids := make([]uuid.UUID, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return tuo.AddNodeIDs(ids...)
}

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

// SetNillableDepositAddressID sets the "deposit_address" edge to the DepositAddress entity by ID if the given value is not nil.
func (tuo *TreeUpdateOne) SetNillableDepositAddressID(id *uuid.UUID) *TreeUpdateOne {
	if id != nil {
		tuo = tuo.SetDepositAddressID(*id)
	}
	return tuo
}

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

// Mutation returns the TreeMutation object of the builder.
func (tuo *TreeUpdateOne) Mutation() *TreeMutation {
	return tuo.mutation
}

// ClearRoot clears the "root" edge to the TreeNode entity.
func (tuo *TreeUpdateOne) ClearRoot() *TreeUpdateOne {
	tuo.mutation.ClearRoot()
	return tuo
}

// ClearNodes clears all "nodes" edges to the TreeNode entity.
func (tuo *TreeUpdateOne) ClearNodes() *TreeUpdateOne {
	tuo.mutation.ClearNodes()
	return tuo
}

// RemoveNodeIDs removes the "nodes" edge to TreeNode entities by IDs.
func (tuo *TreeUpdateOne) RemoveNodeIDs(ids ...uuid.UUID) *TreeUpdateOne {
	tuo.mutation.RemoveNodeIDs(ids...)
	return tuo
}

// RemoveNodes removes "nodes" edges to TreeNode entities.
func (tuo *TreeUpdateOne) RemoveNodes(t ...*TreeNode) *TreeUpdateOne {
	ids := make([]uuid.UUID, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return tuo.RemoveNodeIDs(ids...)
}

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

// Where appends a list predicates to the TreeUpdate builder.
func (tuo *TreeUpdateOne) Where(ps ...predicate.Tree) *TreeUpdateOne {
	tuo.mutation.Where(ps...)
	return tuo
}

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

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (tuo *TreeUpdateOne) check() error {
	if v, ok := tuo.mutation.Status(); ok {
		if err := tree.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "Tree.status": %w`, err)}
		}
	}
	if v, ok := tuo.mutation.Network(); ok {
		if err := tree.NetworkValidator(v); err != nil {
			return &ValidationError{Name: "network", err: fmt.Errorf(`ent: validator failed for field "Tree.network": %w`, err)}
		}
	}
	if v, ok := tuo.mutation.BaseTxid(); ok {
		if err := tree.BaseTxidValidator(v); err != nil {
			return &ValidationError{Name: "base_txid", err: fmt.Errorf(`ent: validator failed for field "Tree.base_txid": %w`, err)}
		}
	}
	if v, ok := tuo.mutation.Vout(); ok {
		if err := tree.VoutValidator(v); err != nil {
			return &ValidationError{Name: "vout", err: fmt.Errorf(`ent: validator failed for field "Tree.vout": %w`, err)}
		}
	}
	return nil
}

func (tuo *TreeUpdateOne) sqlSave(ctx context.Context) (_node *Tree, err error) {
	if err := tuo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(tree.Table, tree.Columns, sqlgraph.NewFieldSpec(tree.FieldID, field.TypeUUID))
	id, ok := tuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Tree.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := tuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, tree.FieldID)
		for _, f := range fields {
			if !tree.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != tree.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := tuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := tuo.mutation.UpdateTime(); ok {
		_spec.SetField(tree.FieldUpdateTime, field.TypeTime, value)
	}
	if value, ok := tuo.mutation.OwnerIdentityPubkey(); ok {
		_spec.SetField(tree.FieldOwnerIdentityPubkey, field.TypeBytes, value)
	}
	if value, ok := tuo.mutation.Status(); ok {
		_spec.SetField(tree.FieldStatus, field.TypeEnum, value)
	}
	if value, ok := tuo.mutation.Network(); ok {
		_spec.SetField(tree.FieldNetwork, field.TypeEnum, value)
	}
	if value, ok := tuo.mutation.BaseTxid(); ok {
		_spec.SetField(tree.FieldBaseTxid, field.TypeBytes, value)
	}
	if value, ok := tuo.mutation.Vout(); ok {
		_spec.SetField(tree.FieldVout, field.TypeInt16, value)
	}
	if value, ok := tuo.mutation.AddedVout(); ok {
		_spec.AddField(tree.FieldVout, field.TypeInt16, value)
	}
	if tuo.mutation.RootCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   tree.RootTable,
			Columns: []string{tree.RootColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(treenode.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tuo.mutation.RootIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   tree.RootTable,
			Columns: []string{tree.RootColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(treenode.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if tuo.mutation.NodesCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   tree.NodesTable,
			Columns: []string{tree.NodesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(treenode.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tuo.mutation.RemovedNodesIDs(); len(nodes) > 0 && !tuo.mutation.NodesCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   tree.NodesTable,
			Columns: []string{tree.NodesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(treenode.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 := tuo.mutation.NodesIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   tree.NodesTable,
			Columns: []string{tree.NodesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(treenode.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if tuo.mutation.DepositAddressCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   tree.DepositAddressTable,
			Columns: []string{tree.DepositAddressColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(depositaddress.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := tuo.mutation.DepositAddressIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   tree.DepositAddressTable,
			Columns: []string{tree.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 = &Tree{config: tuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, tuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{tree.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	tuo.mutation.done = true
	return _node, nil
}
