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

// PreimageRequestUpdate is the builder for updating PreimageRequest entities.
type PreimageRequestUpdate struct {
	config
	hooks    []Hook
	mutation *PreimageRequestMutation
}

// Where appends a list predicates to the PreimageRequestUpdate builder.
func (pru *PreimageRequestUpdate) Where(ps ...predicate.PreimageRequest) *PreimageRequestUpdate {
	pru.mutation.Where(ps...)
	return pru
}

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

// SetPaymentHash sets the "payment_hash" field.
func (pru *PreimageRequestUpdate) SetPaymentHash(b []byte) *PreimageRequestUpdate {
	pru.mutation.SetPaymentHash(b)
	return pru
}

// SetStatus sets the "status" field.
func (pru *PreimageRequestUpdate) SetStatus(srs schematype.PreimageRequestStatus) *PreimageRequestUpdate {
	pru.mutation.SetStatus(srs)
	return pru
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (pru *PreimageRequestUpdate) SetNillableStatus(srs *schematype.PreimageRequestStatus) *PreimageRequestUpdate {
	if srs != nil {
		pru.SetStatus(*srs)
	}
	return pru
}

// SetReceiverIdentityPubkey sets the "receiver_identity_pubkey" field.
func (pru *PreimageRequestUpdate) SetReceiverIdentityPubkey(k keys.Public) *PreimageRequestUpdate {
	pru.mutation.SetReceiverIdentityPubkey(k)
	return pru
}

// SetNillableReceiverIdentityPubkey sets the "receiver_identity_pubkey" field if the given value is not nil.
func (pru *PreimageRequestUpdate) SetNillableReceiverIdentityPubkey(k *keys.Public) *PreimageRequestUpdate {
	if k != nil {
		pru.SetReceiverIdentityPubkey(*k)
	}
	return pru
}

// ClearReceiverIdentityPubkey clears the value of the "receiver_identity_pubkey" field.
func (pru *PreimageRequestUpdate) ClearReceiverIdentityPubkey() *PreimageRequestUpdate {
	pru.mutation.ClearReceiverIdentityPubkey()
	return pru
}

// SetPreimage sets the "preimage" field.
func (pru *PreimageRequestUpdate) SetPreimage(b []byte) *PreimageRequestUpdate {
	pru.mutation.SetPreimage(b)
	return pru
}

// ClearPreimage clears the value of the "preimage" field.
func (pru *PreimageRequestUpdate) ClearPreimage() *PreimageRequestUpdate {
	pru.mutation.ClearPreimage()
	return pru
}

// AddTransactionIDs adds the "transactions" edge to the UserSignedTransaction entity by IDs.
func (pru *PreimageRequestUpdate) AddTransactionIDs(ids ...uuid.UUID) *PreimageRequestUpdate {
	pru.mutation.AddTransactionIDs(ids...)
	return pru
}

// AddTransactions adds the "transactions" edges to the UserSignedTransaction entity.
func (pru *PreimageRequestUpdate) AddTransactions(u ...*UserSignedTransaction) *PreimageRequestUpdate {
	ids := make([]uuid.UUID, len(u))
	for i := range u {
		ids[i] = u[i].ID
	}
	return pru.AddTransactionIDs(ids...)
}

// SetPreimageSharesID sets the "preimage_shares" edge to the PreimageShare entity by ID.
func (pru *PreimageRequestUpdate) SetPreimageSharesID(id uuid.UUID) *PreimageRequestUpdate {
	pru.mutation.SetPreimageSharesID(id)
	return pru
}

// SetNillablePreimageSharesID sets the "preimage_shares" edge to the PreimageShare entity by ID if the given value is not nil.
func (pru *PreimageRequestUpdate) SetNillablePreimageSharesID(id *uuid.UUID) *PreimageRequestUpdate {
	if id != nil {
		pru = pru.SetPreimageSharesID(*id)
	}
	return pru
}

// SetPreimageShares sets the "preimage_shares" edge to the PreimageShare entity.
func (pru *PreimageRequestUpdate) SetPreimageShares(p *PreimageShare) *PreimageRequestUpdate {
	return pru.SetPreimageSharesID(p.ID)
}

// SetTransfersID sets the "transfers" edge to the Transfer entity by ID.
func (pru *PreimageRequestUpdate) SetTransfersID(id uuid.UUID) *PreimageRequestUpdate {
	pru.mutation.SetTransfersID(id)
	return pru
}

// SetNillableTransfersID sets the "transfers" edge to the Transfer entity by ID if the given value is not nil.
func (pru *PreimageRequestUpdate) SetNillableTransfersID(id *uuid.UUID) *PreimageRequestUpdate {
	if id != nil {
		pru = pru.SetTransfersID(*id)
	}
	return pru
}

// SetTransfers sets the "transfers" edge to the Transfer entity.
func (pru *PreimageRequestUpdate) SetTransfers(t *Transfer) *PreimageRequestUpdate {
	return pru.SetTransfersID(t.ID)
}

// Mutation returns the PreimageRequestMutation object of the builder.
func (pru *PreimageRequestUpdate) Mutation() *PreimageRequestMutation {
	return pru.mutation
}

// ClearTransactions clears all "transactions" edges to the UserSignedTransaction entity.
func (pru *PreimageRequestUpdate) ClearTransactions() *PreimageRequestUpdate {
	pru.mutation.ClearTransactions()
	return pru
}

// RemoveTransactionIDs removes the "transactions" edge to UserSignedTransaction entities by IDs.
func (pru *PreimageRequestUpdate) RemoveTransactionIDs(ids ...uuid.UUID) *PreimageRequestUpdate {
	pru.mutation.RemoveTransactionIDs(ids...)
	return pru
}

// RemoveTransactions removes "transactions" edges to UserSignedTransaction entities.
func (pru *PreimageRequestUpdate) RemoveTransactions(u ...*UserSignedTransaction) *PreimageRequestUpdate {
	ids := make([]uuid.UUID, len(u))
	for i := range u {
		ids[i] = u[i].ID
	}
	return pru.RemoveTransactionIDs(ids...)
}

// ClearPreimageShares clears the "preimage_shares" edge to the PreimageShare entity.
func (pru *PreimageRequestUpdate) ClearPreimageShares() *PreimageRequestUpdate {
	pru.mutation.ClearPreimageShares()
	return pru
}

// ClearTransfers clears the "transfers" edge to the Transfer entity.
func (pru *PreimageRequestUpdate) ClearTransfers() *PreimageRequestUpdate {
	pru.mutation.ClearTransfers()
	return pru
}

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

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

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

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

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

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

func (pru *PreimageRequestUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := pru.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(preimagerequest.Table, preimagerequest.Columns, sqlgraph.NewFieldSpec(preimagerequest.FieldID, field.TypeUUID))
	if ps := pru.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := pru.mutation.UpdateTime(); ok {
		_spec.SetField(preimagerequest.FieldUpdateTime, field.TypeTime, value)
	}
	if value, ok := pru.mutation.PaymentHash(); ok {
		_spec.SetField(preimagerequest.FieldPaymentHash, field.TypeBytes, value)
	}
	if value, ok := pru.mutation.Status(); ok {
		_spec.SetField(preimagerequest.FieldStatus, field.TypeEnum, value)
	}
	if value, ok := pru.mutation.ReceiverIdentityPubkey(); ok {
		_spec.SetField(preimagerequest.FieldReceiverIdentityPubkey, field.TypeBytes, value)
	}
	if pru.mutation.ReceiverIdentityPubkeyCleared() {
		_spec.ClearField(preimagerequest.FieldReceiverIdentityPubkey, field.TypeBytes)
	}
	if value, ok := pru.mutation.Preimage(); ok {
		_spec.SetField(preimagerequest.FieldPreimage, field.TypeBytes, value)
	}
	if pru.mutation.PreimageCleared() {
		_spec.ClearField(preimagerequest.FieldPreimage, field.TypeBytes)
	}
	if pru.mutation.TransactionsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   preimagerequest.TransactionsTable,
			Columns: []string{preimagerequest.TransactionsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(usersignedtransaction.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := pru.mutation.RemovedTransactionsIDs(); len(nodes) > 0 && !pru.mutation.TransactionsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   preimagerequest.TransactionsTable,
			Columns: []string{preimagerequest.TransactionsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(usersignedtransaction.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 := pru.mutation.TransactionsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   preimagerequest.TransactionsTable,
			Columns: []string{preimagerequest.TransactionsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(usersignedtransaction.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if pru.mutation.PreimageSharesCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   preimagerequest.PreimageSharesTable,
			Columns: []string{preimagerequest.PreimageSharesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(preimageshare.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := pru.mutation.PreimageSharesIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   preimagerequest.PreimageSharesTable,
			Columns: []string{preimagerequest.PreimageSharesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(preimageshare.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if pru.mutation.TransfersCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   preimagerequest.TransfersTable,
			Columns: []string{preimagerequest.TransfersColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(transfer.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := pru.mutation.TransfersIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   preimagerequest.TransfersTable,
			Columns: []string{preimagerequest.TransfersColumn},
			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, pru.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{preimagerequest.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	pru.mutation.done = true
	return n, nil
}

// PreimageRequestUpdateOne is the builder for updating a single PreimageRequest entity.
type PreimageRequestUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *PreimageRequestMutation
}

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

// SetPaymentHash sets the "payment_hash" field.
func (pruo *PreimageRequestUpdateOne) SetPaymentHash(b []byte) *PreimageRequestUpdateOne {
	pruo.mutation.SetPaymentHash(b)
	return pruo
}

// SetStatus sets the "status" field.
func (pruo *PreimageRequestUpdateOne) SetStatus(srs schematype.PreimageRequestStatus) *PreimageRequestUpdateOne {
	pruo.mutation.SetStatus(srs)
	return pruo
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (pruo *PreimageRequestUpdateOne) SetNillableStatus(srs *schematype.PreimageRequestStatus) *PreimageRequestUpdateOne {
	if srs != nil {
		pruo.SetStatus(*srs)
	}
	return pruo
}

// SetReceiverIdentityPubkey sets the "receiver_identity_pubkey" field.
func (pruo *PreimageRequestUpdateOne) SetReceiverIdentityPubkey(k keys.Public) *PreimageRequestUpdateOne {
	pruo.mutation.SetReceiverIdentityPubkey(k)
	return pruo
}

// SetNillableReceiverIdentityPubkey sets the "receiver_identity_pubkey" field if the given value is not nil.
func (pruo *PreimageRequestUpdateOne) SetNillableReceiverIdentityPubkey(k *keys.Public) *PreimageRequestUpdateOne {
	if k != nil {
		pruo.SetReceiverIdentityPubkey(*k)
	}
	return pruo
}

// ClearReceiverIdentityPubkey clears the value of the "receiver_identity_pubkey" field.
func (pruo *PreimageRequestUpdateOne) ClearReceiverIdentityPubkey() *PreimageRequestUpdateOne {
	pruo.mutation.ClearReceiverIdentityPubkey()
	return pruo
}

// SetPreimage sets the "preimage" field.
func (pruo *PreimageRequestUpdateOne) SetPreimage(b []byte) *PreimageRequestUpdateOne {
	pruo.mutation.SetPreimage(b)
	return pruo
}

// ClearPreimage clears the value of the "preimage" field.
func (pruo *PreimageRequestUpdateOne) ClearPreimage() *PreimageRequestUpdateOne {
	pruo.mutation.ClearPreimage()
	return pruo
}

// AddTransactionIDs adds the "transactions" edge to the UserSignedTransaction entity by IDs.
func (pruo *PreimageRequestUpdateOne) AddTransactionIDs(ids ...uuid.UUID) *PreimageRequestUpdateOne {
	pruo.mutation.AddTransactionIDs(ids...)
	return pruo
}

// AddTransactions adds the "transactions" edges to the UserSignedTransaction entity.
func (pruo *PreimageRequestUpdateOne) AddTransactions(u ...*UserSignedTransaction) *PreimageRequestUpdateOne {
	ids := make([]uuid.UUID, len(u))
	for i := range u {
		ids[i] = u[i].ID
	}
	return pruo.AddTransactionIDs(ids...)
}

// SetPreimageSharesID sets the "preimage_shares" edge to the PreimageShare entity by ID.
func (pruo *PreimageRequestUpdateOne) SetPreimageSharesID(id uuid.UUID) *PreimageRequestUpdateOne {
	pruo.mutation.SetPreimageSharesID(id)
	return pruo
}

// SetNillablePreimageSharesID sets the "preimage_shares" edge to the PreimageShare entity by ID if the given value is not nil.
func (pruo *PreimageRequestUpdateOne) SetNillablePreimageSharesID(id *uuid.UUID) *PreimageRequestUpdateOne {
	if id != nil {
		pruo = pruo.SetPreimageSharesID(*id)
	}
	return pruo
}

// SetPreimageShares sets the "preimage_shares" edge to the PreimageShare entity.
func (pruo *PreimageRequestUpdateOne) SetPreimageShares(p *PreimageShare) *PreimageRequestUpdateOne {
	return pruo.SetPreimageSharesID(p.ID)
}

// SetTransfersID sets the "transfers" edge to the Transfer entity by ID.
func (pruo *PreimageRequestUpdateOne) SetTransfersID(id uuid.UUID) *PreimageRequestUpdateOne {
	pruo.mutation.SetTransfersID(id)
	return pruo
}

// SetNillableTransfersID sets the "transfers" edge to the Transfer entity by ID if the given value is not nil.
func (pruo *PreimageRequestUpdateOne) SetNillableTransfersID(id *uuid.UUID) *PreimageRequestUpdateOne {
	if id != nil {
		pruo = pruo.SetTransfersID(*id)
	}
	return pruo
}

// SetTransfers sets the "transfers" edge to the Transfer entity.
func (pruo *PreimageRequestUpdateOne) SetTransfers(t *Transfer) *PreimageRequestUpdateOne {
	return pruo.SetTransfersID(t.ID)
}

// Mutation returns the PreimageRequestMutation object of the builder.
func (pruo *PreimageRequestUpdateOne) Mutation() *PreimageRequestMutation {
	return pruo.mutation
}

// ClearTransactions clears all "transactions" edges to the UserSignedTransaction entity.
func (pruo *PreimageRequestUpdateOne) ClearTransactions() *PreimageRequestUpdateOne {
	pruo.mutation.ClearTransactions()
	return pruo
}

// RemoveTransactionIDs removes the "transactions" edge to UserSignedTransaction entities by IDs.
func (pruo *PreimageRequestUpdateOne) RemoveTransactionIDs(ids ...uuid.UUID) *PreimageRequestUpdateOne {
	pruo.mutation.RemoveTransactionIDs(ids...)
	return pruo
}

// RemoveTransactions removes "transactions" edges to UserSignedTransaction entities.
func (pruo *PreimageRequestUpdateOne) RemoveTransactions(u ...*UserSignedTransaction) *PreimageRequestUpdateOne {
	ids := make([]uuid.UUID, len(u))
	for i := range u {
		ids[i] = u[i].ID
	}
	return pruo.RemoveTransactionIDs(ids...)
}

// ClearPreimageShares clears the "preimage_shares" edge to the PreimageShare entity.
func (pruo *PreimageRequestUpdateOne) ClearPreimageShares() *PreimageRequestUpdateOne {
	pruo.mutation.ClearPreimageShares()
	return pruo
}

// ClearTransfers clears the "transfers" edge to the Transfer entity.
func (pruo *PreimageRequestUpdateOne) ClearTransfers() *PreimageRequestUpdateOne {
	pruo.mutation.ClearTransfers()
	return pruo
}

// Where appends a list predicates to the PreimageRequestUpdate builder.
func (pruo *PreimageRequestUpdateOne) Where(ps ...predicate.PreimageRequest) *PreimageRequestUpdateOne {
	pruo.mutation.Where(ps...)
	return pruo
}

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

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

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

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

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

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

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

func (pruo *PreimageRequestUpdateOne) sqlSave(ctx context.Context) (_node *PreimageRequest, err error) {
	if err := pruo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(preimagerequest.Table, preimagerequest.Columns, sqlgraph.NewFieldSpec(preimagerequest.FieldID, field.TypeUUID))
	id, ok := pruo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "PreimageRequest.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := pruo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, preimagerequest.FieldID)
		for _, f := range fields {
			if !preimagerequest.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != preimagerequest.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := pruo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := pruo.mutation.UpdateTime(); ok {
		_spec.SetField(preimagerequest.FieldUpdateTime, field.TypeTime, value)
	}
	if value, ok := pruo.mutation.PaymentHash(); ok {
		_spec.SetField(preimagerequest.FieldPaymentHash, field.TypeBytes, value)
	}
	if value, ok := pruo.mutation.Status(); ok {
		_spec.SetField(preimagerequest.FieldStatus, field.TypeEnum, value)
	}
	if value, ok := pruo.mutation.ReceiverIdentityPubkey(); ok {
		_spec.SetField(preimagerequest.FieldReceiverIdentityPubkey, field.TypeBytes, value)
	}
	if pruo.mutation.ReceiverIdentityPubkeyCleared() {
		_spec.ClearField(preimagerequest.FieldReceiverIdentityPubkey, field.TypeBytes)
	}
	if value, ok := pruo.mutation.Preimage(); ok {
		_spec.SetField(preimagerequest.FieldPreimage, field.TypeBytes, value)
	}
	if pruo.mutation.PreimageCleared() {
		_spec.ClearField(preimagerequest.FieldPreimage, field.TypeBytes)
	}
	if pruo.mutation.TransactionsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   preimagerequest.TransactionsTable,
			Columns: []string{preimagerequest.TransactionsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(usersignedtransaction.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := pruo.mutation.RemovedTransactionsIDs(); len(nodes) > 0 && !pruo.mutation.TransactionsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   preimagerequest.TransactionsTable,
			Columns: []string{preimagerequest.TransactionsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(usersignedtransaction.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 := pruo.mutation.TransactionsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: true,
			Table:   preimagerequest.TransactionsTable,
			Columns: []string{preimagerequest.TransactionsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(usersignedtransaction.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if pruo.mutation.PreimageSharesCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   preimagerequest.PreimageSharesTable,
			Columns: []string{preimagerequest.PreimageSharesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(preimageshare.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := pruo.mutation.PreimageSharesIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   preimagerequest.PreimageSharesTable,
			Columns: []string{preimagerequest.PreimageSharesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(preimageshare.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if pruo.mutation.TransfersCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   preimagerequest.TransfersTable,
			Columns: []string{preimagerequest.TransfersColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(transfer.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := pruo.mutation.TransfersIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   preimagerequest.TransfersTable,
			Columns: []string{preimagerequest.TransfersColumn},
			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 = &PreimageRequest{config: pruo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, pruo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{preimagerequest.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	pruo.mutation.done = true
	return _node, nil
}
