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

// BlockHeightUpdate is the builder for updating BlockHeight entities.
type BlockHeightUpdate struct {
	config
	hooks    []Hook
	mutation *BlockHeightMutation
}

// Where appends a list predicates to the BlockHeightUpdate builder.
func (bhu *BlockHeightUpdate) Where(ps ...predicate.BlockHeight) *BlockHeightUpdate {
	bhu.mutation.Where(ps...)
	return bhu
}

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

// SetHeight sets the "height" field.
func (bhu *BlockHeightUpdate) SetHeight(i int64) *BlockHeightUpdate {
	bhu.mutation.ResetHeight()
	bhu.mutation.SetHeight(i)
	return bhu
}

// SetNillableHeight sets the "height" field if the given value is not nil.
func (bhu *BlockHeightUpdate) SetNillableHeight(i *int64) *BlockHeightUpdate {
	if i != nil {
		bhu.SetHeight(*i)
	}
	return bhu
}

// AddHeight adds i to the "height" field.
func (bhu *BlockHeightUpdate) AddHeight(i int64) *BlockHeightUpdate {
	bhu.mutation.AddHeight(i)
	return bhu
}

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

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

// Mutation returns the BlockHeightMutation object of the builder.
func (bhu *BlockHeightUpdate) Mutation() *BlockHeightMutation {
	return bhu.mutation
}

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (bhu *BlockHeightUpdate) check() error {
	if v, ok := bhu.mutation.Network(); ok {
		if err := blockheight.NetworkValidator(v); err != nil {
			return &ValidationError{Name: "network", err: fmt.Errorf(`ent: validator failed for field "BlockHeight.network": %w`, err)}
		}
	}
	return nil
}

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

// BlockHeightUpdateOne is the builder for updating a single BlockHeight entity.
type BlockHeightUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *BlockHeightMutation
}

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

// SetHeight sets the "height" field.
func (bhuo *BlockHeightUpdateOne) SetHeight(i int64) *BlockHeightUpdateOne {
	bhuo.mutation.ResetHeight()
	bhuo.mutation.SetHeight(i)
	return bhuo
}

// SetNillableHeight sets the "height" field if the given value is not nil.
func (bhuo *BlockHeightUpdateOne) SetNillableHeight(i *int64) *BlockHeightUpdateOne {
	if i != nil {
		bhuo.SetHeight(*i)
	}
	return bhuo
}

// AddHeight adds i to the "height" field.
func (bhuo *BlockHeightUpdateOne) AddHeight(i int64) *BlockHeightUpdateOne {
	bhuo.mutation.AddHeight(i)
	return bhuo
}

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

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

// Mutation returns the BlockHeightMutation object of the builder.
func (bhuo *BlockHeightUpdateOne) Mutation() *BlockHeightMutation {
	return bhuo.mutation
}

// Where appends a list predicates to the BlockHeightUpdate builder.
func (bhuo *BlockHeightUpdateOne) Where(ps ...predicate.BlockHeight) *BlockHeightUpdateOne {
	bhuo.mutation.Where(ps...)
	return bhuo
}

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

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (bhuo *BlockHeightUpdateOne) check() error {
	if v, ok := bhuo.mutation.Network(); ok {
		if err := blockheight.NetworkValidator(v); err != nil {
			return &ValidationError{Name: "network", err: fmt.Errorf(`ent: validator failed for field "BlockHeight.network": %w`, err)}
		}
	}
	return nil
}

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