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

// PreimageShareUpdate is the builder for updating PreimageShare entities.
type PreimageShareUpdate struct {
	config
	hooks    []Hook
	mutation *PreimageShareMutation
}

// Where appends a list predicates to the PreimageShareUpdate builder.
func (psu *PreimageShareUpdate) Where(ps ...predicate.PreimageShare) *PreimageShareUpdate {
	psu.mutation.Where(ps...)
	return psu
}

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

// SetPreimageRequestID sets the "preimage_request" edge to the PreimageRequest entity by ID.
func (psu *PreimageShareUpdate) SetPreimageRequestID(id uuid.UUID) *PreimageShareUpdate {
	psu.mutation.SetPreimageRequestID(id)
	return psu
}

// SetNillablePreimageRequestID sets the "preimage_request" edge to the PreimageRequest entity by ID if the given value is not nil.
func (psu *PreimageShareUpdate) SetNillablePreimageRequestID(id *uuid.UUID) *PreimageShareUpdate {
	if id != nil {
		psu = psu.SetPreimageRequestID(*id)
	}
	return psu
}

// SetPreimageRequest sets the "preimage_request" edge to the PreimageRequest entity.
func (psu *PreimageShareUpdate) SetPreimageRequest(p *PreimageRequest) *PreimageShareUpdate {
	return psu.SetPreimageRequestID(p.ID)
}

// Mutation returns the PreimageShareMutation object of the builder.
func (psu *PreimageShareUpdate) Mutation() *PreimageShareMutation {
	return psu.mutation
}

// ClearPreimageRequest clears the "preimage_request" edge to the PreimageRequest entity.
func (psu *PreimageShareUpdate) ClearPreimageRequest() *PreimageShareUpdate {
	psu.mutation.ClearPreimageRequest()
	return psu
}

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

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

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

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

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

func (psu *PreimageShareUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(preimageshare.Table, preimageshare.Columns, sqlgraph.NewFieldSpec(preimageshare.FieldID, field.TypeUUID))
	if ps := psu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := psu.mutation.UpdateTime(); ok {
		_spec.SetField(preimageshare.FieldUpdateTime, field.TypeTime, value)
	}
	if psu.mutation.PreimageRequestCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   preimageshare.PreimageRequestTable,
			Columns: []string{preimageshare.PreimageRequestColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(preimagerequest.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := psu.mutation.PreimageRequestIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   preimageshare.PreimageRequestTable,
			Columns: []string{preimageshare.PreimageRequestColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(preimagerequest.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, psu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{preimageshare.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	psu.mutation.done = true
	return n, nil
}

// PreimageShareUpdateOne is the builder for updating a single PreimageShare entity.
type PreimageShareUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *PreimageShareMutation
}

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

// SetPreimageRequestID sets the "preimage_request" edge to the PreimageRequest entity by ID.
func (psuo *PreimageShareUpdateOne) SetPreimageRequestID(id uuid.UUID) *PreimageShareUpdateOne {
	psuo.mutation.SetPreimageRequestID(id)
	return psuo
}

// SetNillablePreimageRequestID sets the "preimage_request" edge to the PreimageRequest entity by ID if the given value is not nil.
func (psuo *PreimageShareUpdateOne) SetNillablePreimageRequestID(id *uuid.UUID) *PreimageShareUpdateOne {
	if id != nil {
		psuo = psuo.SetPreimageRequestID(*id)
	}
	return psuo
}

// SetPreimageRequest sets the "preimage_request" edge to the PreimageRequest entity.
func (psuo *PreimageShareUpdateOne) SetPreimageRequest(p *PreimageRequest) *PreimageShareUpdateOne {
	return psuo.SetPreimageRequestID(p.ID)
}

// Mutation returns the PreimageShareMutation object of the builder.
func (psuo *PreimageShareUpdateOne) Mutation() *PreimageShareMutation {
	return psuo.mutation
}

// ClearPreimageRequest clears the "preimage_request" edge to the PreimageRequest entity.
func (psuo *PreimageShareUpdateOne) ClearPreimageRequest() *PreimageShareUpdateOne {
	psuo.mutation.ClearPreimageRequest()
	return psuo
}

// Where appends a list predicates to the PreimageShareUpdate builder.
func (psuo *PreimageShareUpdateOne) Where(ps ...predicate.PreimageShare) *PreimageShareUpdateOne {
	psuo.mutation.Where(ps...)
	return psuo
}

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

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

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

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

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

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

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