// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/google/uuid"
	"github.com/in-toto/archivista/ent/dsse"
	"github.com/in-toto/archivista/ent/predicate"
	"github.com/in-toto/archivista/ent/signature"
	"github.com/in-toto/archivista/ent/timestamp"
)

// SignatureUpdate is the builder for updating Signature entities.
type SignatureUpdate struct {
	config
	hooks    []Hook
	mutation *SignatureMutation
}

// Where appends a list predicates to the SignatureUpdate builder.
func (_u *SignatureUpdate) Where(ps ...predicate.Signature) *SignatureUpdate {
	_u.mutation.Where(ps...)
	return _u
}

// SetKeyID sets the "key_id" field.
func (_u *SignatureUpdate) SetKeyID(v string) *SignatureUpdate {
	_u.mutation.SetKeyID(v)
	return _u
}

// SetNillableKeyID sets the "key_id" field if the given value is not nil.
func (_u *SignatureUpdate) SetNillableKeyID(v *string) *SignatureUpdate {
	if v != nil {
		_u.SetKeyID(*v)
	}
	return _u
}

// SetSignature sets the "signature" field.
func (_u *SignatureUpdate) SetSignature(v string) *SignatureUpdate {
	_u.mutation.SetSignature(v)
	return _u
}

// SetNillableSignature sets the "signature" field if the given value is not nil.
func (_u *SignatureUpdate) SetNillableSignature(v *string) *SignatureUpdate {
	if v != nil {
		_u.SetSignature(*v)
	}
	return _u
}

// SetDsseID sets the "dsse" edge to the Dsse entity by ID.
func (_u *SignatureUpdate) SetDsseID(id uuid.UUID) *SignatureUpdate {
	_u.mutation.SetDsseID(id)
	return _u
}

// SetNillableDsseID sets the "dsse" edge to the Dsse entity by ID if the given value is not nil.
func (_u *SignatureUpdate) SetNillableDsseID(id *uuid.UUID) *SignatureUpdate {
	if id != nil {
		_u = _u.SetDsseID(*id)
	}
	return _u
}

// SetDsse sets the "dsse" edge to the Dsse entity.
func (_u *SignatureUpdate) SetDsse(v *Dsse) *SignatureUpdate {
	return _u.SetDsseID(v.ID)
}

// AddTimestampIDs adds the "timestamps" edge to the Timestamp entity by IDs.
func (_u *SignatureUpdate) AddTimestampIDs(ids ...uuid.UUID) *SignatureUpdate {
	_u.mutation.AddTimestampIDs(ids...)
	return _u
}

// AddTimestamps adds the "timestamps" edges to the Timestamp entity.
func (_u *SignatureUpdate) AddTimestamps(v ...*Timestamp) *SignatureUpdate {
	ids := make([]uuid.UUID, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddTimestampIDs(ids...)
}

// Mutation returns the SignatureMutation object of the builder.
func (_u *SignatureUpdate) Mutation() *SignatureMutation {
	return _u.mutation
}

// ClearDsse clears the "dsse" edge to the Dsse entity.
func (_u *SignatureUpdate) ClearDsse() *SignatureUpdate {
	_u.mutation.ClearDsse()
	return _u
}

// ClearTimestamps clears all "timestamps" edges to the Timestamp entity.
func (_u *SignatureUpdate) ClearTimestamps() *SignatureUpdate {
	_u.mutation.ClearTimestamps()
	return _u
}

// RemoveTimestampIDs removes the "timestamps" edge to Timestamp entities by IDs.
func (_u *SignatureUpdate) RemoveTimestampIDs(ids ...uuid.UUID) *SignatureUpdate {
	_u.mutation.RemoveTimestampIDs(ids...)
	return _u
}

// RemoveTimestamps removes "timestamps" edges to Timestamp entities.
func (_u *SignatureUpdate) RemoveTimestamps(v ...*Timestamp) *SignatureUpdate {
	ids := make([]uuid.UUID, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveTimestampIDs(ids...)
}

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (_u *SignatureUpdate) check() error {
	if v, ok := _u.mutation.Signature(); ok {
		if err := signature.SignatureValidator(v); err != nil {
			return &ValidationError{Name: "signature", err: fmt.Errorf(`ent: validator failed for field "Signature.signature": %w`, err)}
		}
	}
	return nil
}

func (_u *SignatureUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(signature.Table, signature.Columns, sqlgraph.NewFieldSpec(signature.FieldID, field.TypeUUID))
	if ps := _u.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := _u.mutation.KeyID(); ok {
		_spec.SetField(signature.FieldKeyID, field.TypeString, value)
	}
	if value, ok := _u.mutation.Signature(); ok {
		_spec.SetField(signature.FieldSignature, field.TypeString, value)
	}
	if _u.mutation.DsseCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   signature.DsseTable,
			Columns: []string{signature.DsseColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(dsse.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.DsseIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   signature.DsseTable,
			Columns: []string{signature.DsseColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(dsse.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.TimestampsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   signature.TimestampsTable,
			Columns: []string{signature.TimestampsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(timestamp.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedTimestampsIDs(); len(nodes) > 0 && !_u.mutation.TimestampsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   signature.TimestampsTable,
			Columns: []string{signature.TimestampsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(timestamp.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 := _u.mutation.TimestampsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   signature.TimestampsTable,
			Columns: []string{signature.TimestampsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(timestamp.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{signature.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// SignatureUpdateOne is the builder for updating a single Signature entity.
type SignatureUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *SignatureMutation
}

// SetKeyID sets the "key_id" field.
func (_u *SignatureUpdateOne) SetKeyID(v string) *SignatureUpdateOne {
	_u.mutation.SetKeyID(v)
	return _u
}

// SetNillableKeyID sets the "key_id" field if the given value is not nil.
func (_u *SignatureUpdateOne) SetNillableKeyID(v *string) *SignatureUpdateOne {
	if v != nil {
		_u.SetKeyID(*v)
	}
	return _u
}

// SetSignature sets the "signature" field.
func (_u *SignatureUpdateOne) SetSignature(v string) *SignatureUpdateOne {
	_u.mutation.SetSignature(v)
	return _u
}

// SetNillableSignature sets the "signature" field if the given value is not nil.
func (_u *SignatureUpdateOne) SetNillableSignature(v *string) *SignatureUpdateOne {
	if v != nil {
		_u.SetSignature(*v)
	}
	return _u
}

// SetDsseID sets the "dsse" edge to the Dsse entity by ID.
func (_u *SignatureUpdateOne) SetDsseID(id uuid.UUID) *SignatureUpdateOne {
	_u.mutation.SetDsseID(id)
	return _u
}

// SetNillableDsseID sets the "dsse" edge to the Dsse entity by ID if the given value is not nil.
func (_u *SignatureUpdateOne) SetNillableDsseID(id *uuid.UUID) *SignatureUpdateOne {
	if id != nil {
		_u = _u.SetDsseID(*id)
	}
	return _u
}

// SetDsse sets the "dsse" edge to the Dsse entity.
func (_u *SignatureUpdateOne) SetDsse(v *Dsse) *SignatureUpdateOne {
	return _u.SetDsseID(v.ID)
}

// AddTimestampIDs adds the "timestamps" edge to the Timestamp entity by IDs.
func (_u *SignatureUpdateOne) AddTimestampIDs(ids ...uuid.UUID) *SignatureUpdateOne {
	_u.mutation.AddTimestampIDs(ids...)
	return _u
}

// AddTimestamps adds the "timestamps" edges to the Timestamp entity.
func (_u *SignatureUpdateOne) AddTimestamps(v ...*Timestamp) *SignatureUpdateOne {
	ids := make([]uuid.UUID, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddTimestampIDs(ids...)
}

// Mutation returns the SignatureMutation object of the builder.
func (_u *SignatureUpdateOne) Mutation() *SignatureMutation {
	return _u.mutation
}

// ClearDsse clears the "dsse" edge to the Dsse entity.
func (_u *SignatureUpdateOne) ClearDsse() *SignatureUpdateOne {
	_u.mutation.ClearDsse()
	return _u
}

// ClearTimestamps clears all "timestamps" edges to the Timestamp entity.
func (_u *SignatureUpdateOne) ClearTimestamps() *SignatureUpdateOne {
	_u.mutation.ClearTimestamps()
	return _u
}

// RemoveTimestampIDs removes the "timestamps" edge to Timestamp entities by IDs.
func (_u *SignatureUpdateOne) RemoveTimestampIDs(ids ...uuid.UUID) *SignatureUpdateOne {
	_u.mutation.RemoveTimestampIDs(ids...)
	return _u
}

// RemoveTimestamps removes "timestamps" edges to Timestamp entities.
func (_u *SignatureUpdateOne) RemoveTimestamps(v ...*Timestamp) *SignatureUpdateOne {
	ids := make([]uuid.UUID, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveTimestampIDs(ids...)
}

// Where appends a list predicates to the SignatureUpdate builder.
func (_u *SignatureUpdateOne) Where(ps ...predicate.Signature) *SignatureUpdateOne {
	_u.mutation.Where(ps...)
	return _u
}

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

// Save executes the query and returns the updated Signature entity.
func (_u *SignatureUpdateOne) Save(ctx context.Context) (*Signature, error) {
	return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}

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

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

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

// check runs all checks and user-defined validators on the builder.
func (_u *SignatureUpdateOne) check() error {
	if v, ok := _u.mutation.Signature(); ok {
		if err := signature.SignatureValidator(v); err != nil {
			return &ValidationError{Name: "signature", err: fmt.Errorf(`ent: validator failed for field "Signature.signature": %w`, err)}
		}
	}
	return nil
}

func (_u *SignatureUpdateOne) sqlSave(ctx context.Context) (_node *Signature, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(signature.Table, signature.Columns, sqlgraph.NewFieldSpec(signature.FieldID, field.TypeUUID))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Signature.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := _u.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, signature.FieldID)
		for _, f := range fields {
			if !signature.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != signature.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := _u.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := _u.mutation.KeyID(); ok {
		_spec.SetField(signature.FieldKeyID, field.TypeString, value)
	}
	if value, ok := _u.mutation.Signature(); ok {
		_spec.SetField(signature.FieldSignature, field.TypeString, value)
	}
	if _u.mutation.DsseCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   signature.DsseTable,
			Columns: []string{signature.DsseColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(dsse.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.DsseIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   signature.DsseTable,
			Columns: []string{signature.DsseColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(dsse.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.TimestampsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   signature.TimestampsTable,
			Columns: []string{signature.TimestampsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(timestamp.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedTimestampsIDs(); len(nodes) > 0 && !_u.mutation.TimestampsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   signature.TimestampsTable,
			Columns: []string{signature.TimestampsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(timestamp.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 := _u.mutation.TimestampsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   signature.TimestampsTable,
			Columns: []string{signature.TimestampsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(timestamp.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &Signature{config: _u.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{signature.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
