// 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/lightsparkdev/spark/so/ent/pendingsendtransfer"
	"github.com/lightsparkdev/spark/so/ent/predicate"
	"github.com/lightsparkdev/spark/so/ent/schema/schematype"
)

// PendingSendTransferUpdate is the builder for updating PendingSendTransfer entities.
type PendingSendTransferUpdate struct {
	config
	hooks    []Hook
	mutation *PendingSendTransferMutation
}

// Where appends a list predicates to the PendingSendTransferUpdate builder.
func (pstu *PendingSendTransferUpdate) Where(ps ...predicate.PendingSendTransfer) *PendingSendTransferUpdate {
	pstu.mutation.Where(ps...)
	return pstu
}

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

// SetStatus sets the "status" field.
func (pstu *PendingSendTransferUpdate) SetStatus(ssts schematype.PendingSendTransferStatus) *PendingSendTransferUpdate {
	pstu.mutation.SetStatus(ssts)
	return pstu
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (pstu *PendingSendTransferUpdate) SetNillableStatus(ssts *schematype.PendingSendTransferStatus) *PendingSendTransferUpdate {
	if ssts != nil {
		pstu.SetStatus(*ssts)
	}
	return pstu
}

// Mutation returns the PendingSendTransferMutation object of the builder.
func (pstu *PendingSendTransferUpdate) Mutation() *PendingSendTransferMutation {
	return pstu.mutation
}

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (pstu *PendingSendTransferUpdate) check() error {
	if v, ok := pstu.mutation.Status(); ok {
		if err := pendingsendtransfer.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "PendingSendTransfer.status": %w`, err)}
		}
	}
	return nil
}

func (pstu *PendingSendTransferUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := pstu.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(pendingsendtransfer.Table, pendingsendtransfer.Columns, sqlgraph.NewFieldSpec(pendingsendtransfer.FieldID, field.TypeUUID))
	if ps := pstu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := pstu.mutation.UpdateTime(); ok {
		_spec.SetField(pendingsendtransfer.FieldUpdateTime, field.TypeTime, value)
	}
	if value, ok := pstu.mutation.Status(); ok {
		_spec.SetField(pendingsendtransfer.FieldStatus, field.TypeEnum, value)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, pstu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{pendingsendtransfer.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	pstu.mutation.done = true
	return n, nil
}

// PendingSendTransferUpdateOne is the builder for updating a single PendingSendTransfer entity.
type PendingSendTransferUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *PendingSendTransferMutation
}

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

// SetStatus sets the "status" field.
func (pstuo *PendingSendTransferUpdateOne) SetStatus(ssts schematype.PendingSendTransferStatus) *PendingSendTransferUpdateOne {
	pstuo.mutation.SetStatus(ssts)
	return pstuo
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (pstuo *PendingSendTransferUpdateOne) SetNillableStatus(ssts *schematype.PendingSendTransferStatus) *PendingSendTransferUpdateOne {
	if ssts != nil {
		pstuo.SetStatus(*ssts)
	}
	return pstuo
}

// Mutation returns the PendingSendTransferMutation object of the builder.
func (pstuo *PendingSendTransferUpdateOne) Mutation() *PendingSendTransferMutation {
	return pstuo.mutation
}

// Where appends a list predicates to the PendingSendTransferUpdate builder.
func (pstuo *PendingSendTransferUpdateOne) Where(ps ...predicate.PendingSendTransfer) *PendingSendTransferUpdateOne {
	pstuo.mutation.Where(ps...)
	return pstuo
}

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

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (pstuo *PendingSendTransferUpdateOne) check() error {
	if v, ok := pstuo.mutation.Status(); ok {
		if err := pendingsendtransfer.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "PendingSendTransfer.status": %w`, err)}
		}
	}
	return nil
}

func (pstuo *PendingSendTransferUpdateOne) sqlSave(ctx context.Context) (_node *PendingSendTransfer, err error) {
	if err := pstuo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(pendingsendtransfer.Table, pendingsendtransfer.Columns, sqlgraph.NewFieldSpec(pendingsendtransfer.FieldID, field.TypeUUID))
	id, ok := pstuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "PendingSendTransfer.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := pstuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, pendingsendtransfer.FieldID)
		for _, f := range fields {
			if !pendingsendtransfer.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != pendingsendtransfer.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := pstuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := pstuo.mutation.UpdateTime(); ok {
		_spec.SetField(pendingsendtransfer.FieldUpdateTime, field.TypeTime, value)
	}
	if value, ok := pstuo.mutation.Status(); ok {
		_spec.SetField(pendingsendtransfer.FieldStatus, field.TypeEnum, value)
	}
	_node = &PendingSendTransfer{config: pstuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, pstuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{pendingsendtransfer.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	pstuo.mutation.done = true
	return _node, nil
}
