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

// CooperativeExitUpdate is the builder for updating CooperativeExit entities.
type CooperativeExitUpdate struct {
	config
	hooks    []Hook
	mutation *CooperativeExitMutation
}

// Where appends a list predicates to the CooperativeExitUpdate builder.
func (ceu *CooperativeExitUpdate) Where(ps ...predicate.CooperativeExit) *CooperativeExitUpdate {
	ceu.mutation.Where(ps...)
	return ceu
}

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

// SetConfirmationHeight sets the "confirmation_height" field.
func (ceu *CooperativeExitUpdate) SetConfirmationHeight(i int64) *CooperativeExitUpdate {
	ceu.mutation.ResetConfirmationHeight()
	ceu.mutation.SetConfirmationHeight(i)
	return ceu
}

// SetNillableConfirmationHeight sets the "confirmation_height" field if the given value is not nil.
func (ceu *CooperativeExitUpdate) SetNillableConfirmationHeight(i *int64) *CooperativeExitUpdate {
	if i != nil {
		ceu.SetConfirmationHeight(*i)
	}
	return ceu
}

// AddConfirmationHeight adds i to the "confirmation_height" field.
func (ceu *CooperativeExitUpdate) AddConfirmationHeight(i int64) *CooperativeExitUpdate {
	ceu.mutation.AddConfirmationHeight(i)
	return ceu
}

// ClearConfirmationHeight clears the value of the "confirmation_height" field.
func (ceu *CooperativeExitUpdate) ClearConfirmationHeight() *CooperativeExitUpdate {
	ceu.mutation.ClearConfirmationHeight()
	return ceu
}

// SetTransferID sets the "transfer" edge to the Transfer entity by ID.
func (ceu *CooperativeExitUpdate) SetTransferID(id uuid.UUID) *CooperativeExitUpdate {
	ceu.mutation.SetTransferID(id)
	return ceu
}

// SetTransfer sets the "transfer" edge to the Transfer entity.
func (ceu *CooperativeExitUpdate) SetTransfer(t *Transfer) *CooperativeExitUpdate {
	return ceu.SetTransferID(t.ID)
}

// Mutation returns the CooperativeExitMutation object of the builder.
func (ceu *CooperativeExitUpdate) Mutation() *CooperativeExitMutation {
	return ceu.mutation
}

// ClearTransfer clears the "transfer" edge to the Transfer entity.
func (ceu *CooperativeExitUpdate) ClearTransfer() *CooperativeExitUpdate {
	ceu.mutation.ClearTransfer()
	return ceu
}

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (ceu *CooperativeExitUpdate) check() error {
	if ceu.mutation.TransferCleared() && len(ceu.mutation.TransferIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "CooperativeExit.transfer"`)
	}
	return nil
}

func (ceu *CooperativeExitUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := ceu.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(cooperativeexit.Table, cooperativeexit.Columns, sqlgraph.NewFieldSpec(cooperativeexit.FieldID, field.TypeUUID))
	if ps := ceu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := ceu.mutation.UpdateTime(); ok {
		_spec.SetField(cooperativeexit.FieldUpdateTime, field.TypeTime, value)
	}
	if value, ok := ceu.mutation.ConfirmationHeight(); ok {
		_spec.SetField(cooperativeexit.FieldConfirmationHeight, field.TypeInt64, value)
	}
	if value, ok := ceu.mutation.AddedConfirmationHeight(); ok {
		_spec.AddField(cooperativeexit.FieldConfirmationHeight, field.TypeInt64, value)
	}
	if ceu.mutation.ConfirmationHeightCleared() {
		_spec.ClearField(cooperativeexit.FieldConfirmationHeight, field.TypeInt64)
	}
	if ceu.mutation.TransferCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   cooperativeexit.TransferTable,
			Columns: []string{cooperativeexit.TransferColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(transfer.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := ceu.mutation.TransferIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   cooperativeexit.TransferTable,
			Columns: []string{cooperativeexit.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, ceu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{cooperativeexit.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	ceu.mutation.done = true
	return n, nil
}

// CooperativeExitUpdateOne is the builder for updating a single CooperativeExit entity.
type CooperativeExitUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *CooperativeExitMutation
}

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

// SetConfirmationHeight sets the "confirmation_height" field.
func (ceuo *CooperativeExitUpdateOne) SetConfirmationHeight(i int64) *CooperativeExitUpdateOne {
	ceuo.mutation.ResetConfirmationHeight()
	ceuo.mutation.SetConfirmationHeight(i)
	return ceuo
}

// SetNillableConfirmationHeight sets the "confirmation_height" field if the given value is not nil.
func (ceuo *CooperativeExitUpdateOne) SetNillableConfirmationHeight(i *int64) *CooperativeExitUpdateOne {
	if i != nil {
		ceuo.SetConfirmationHeight(*i)
	}
	return ceuo
}

// AddConfirmationHeight adds i to the "confirmation_height" field.
func (ceuo *CooperativeExitUpdateOne) AddConfirmationHeight(i int64) *CooperativeExitUpdateOne {
	ceuo.mutation.AddConfirmationHeight(i)
	return ceuo
}

// ClearConfirmationHeight clears the value of the "confirmation_height" field.
func (ceuo *CooperativeExitUpdateOne) ClearConfirmationHeight() *CooperativeExitUpdateOne {
	ceuo.mutation.ClearConfirmationHeight()
	return ceuo
}

// SetTransferID sets the "transfer" edge to the Transfer entity by ID.
func (ceuo *CooperativeExitUpdateOne) SetTransferID(id uuid.UUID) *CooperativeExitUpdateOne {
	ceuo.mutation.SetTransferID(id)
	return ceuo
}

// SetTransfer sets the "transfer" edge to the Transfer entity.
func (ceuo *CooperativeExitUpdateOne) SetTransfer(t *Transfer) *CooperativeExitUpdateOne {
	return ceuo.SetTransferID(t.ID)
}

// Mutation returns the CooperativeExitMutation object of the builder.
func (ceuo *CooperativeExitUpdateOne) Mutation() *CooperativeExitMutation {
	return ceuo.mutation
}

// ClearTransfer clears the "transfer" edge to the Transfer entity.
func (ceuo *CooperativeExitUpdateOne) ClearTransfer() *CooperativeExitUpdateOne {
	ceuo.mutation.ClearTransfer()
	return ceuo
}

// Where appends a list predicates to the CooperativeExitUpdate builder.
func (ceuo *CooperativeExitUpdateOne) Where(ps ...predicate.CooperativeExit) *CooperativeExitUpdateOne {
	ceuo.mutation.Where(ps...)
	return ceuo
}

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

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (ceuo *CooperativeExitUpdateOne) check() error {
	if ceuo.mutation.TransferCleared() && len(ceuo.mutation.TransferIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "CooperativeExit.transfer"`)
	}
	return nil
}

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