// 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/in-toto/archivista/ent/predicate"
	"github.com/in-toto/archivista/ent/signature"
	"github.com/in-toto/archivista/ent/timestamp"
)

// TimestampUpdate is the builder for updating Timestamp entities.
type TimestampUpdate struct {
	config
	hooks    []Hook
	mutation *TimestampMutation
}

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

// SetType sets the "type" field.
func (_u *TimestampUpdate) SetType(v string) *TimestampUpdate {
	_u.mutation.SetType(v)
	return _u
}

// SetNillableType sets the "type" field if the given value is not nil.
func (_u *TimestampUpdate) SetNillableType(v *string) *TimestampUpdate {
	if v != nil {
		_u.SetType(*v)
	}
	return _u
}

// SetTimestamp sets the "timestamp" field.
func (_u *TimestampUpdate) SetTimestamp(v time.Time) *TimestampUpdate {
	_u.mutation.SetTimestamp(v)
	return _u
}

// SetNillableTimestamp sets the "timestamp" field if the given value is not nil.
func (_u *TimestampUpdate) SetNillableTimestamp(v *time.Time) *TimestampUpdate {
	if v != nil {
		_u.SetTimestamp(*v)
	}
	return _u
}

// SetSignatureID sets the "signature" edge to the Signature entity by ID.
func (_u *TimestampUpdate) SetSignatureID(id uuid.UUID) *TimestampUpdate {
	_u.mutation.SetSignatureID(id)
	return _u
}

// SetNillableSignatureID sets the "signature" edge to the Signature entity by ID if the given value is not nil.
func (_u *TimestampUpdate) SetNillableSignatureID(id *uuid.UUID) *TimestampUpdate {
	if id != nil {
		_u = _u.SetSignatureID(*id)
	}
	return _u
}

// SetSignature sets the "signature" edge to the Signature entity.
func (_u *TimestampUpdate) SetSignature(v *Signature) *TimestampUpdate {
	return _u.SetSignatureID(v.ID)
}

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

// ClearSignature clears the "signature" edge to the Signature entity.
func (_u *TimestampUpdate) ClearSignature() *TimestampUpdate {
	_u.mutation.ClearSignature()
	return _u
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (_u *TimestampUpdate) 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 *TimestampUpdate) SaveX(ctx context.Context) int {
	affected, err := _u.Save(ctx)
	if err != nil {
		panic(err)
	}
	return affected
}

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

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

func (_u *TimestampUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	_spec := sqlgraph.NewUpdateSpec(timestamp.Table, timestamp.Columns, sqlgraph.NewFieldSpec(timestamp.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.GetType(); ok {
		_spec.SetField(timestamp.FieldType, field.TypeString, value)
	}
	if value, ok := _u.mutation.Timestamp(); ok {
		_spec.SetField(timestamp.FieldTimestamp, field.TypeTime, value)
	}
	if _u.mutation.SignatureCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   timestamp.SignatureTable,
			Columns: []string{timestamp.SignatureColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(signature.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.SignatureIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   timestamp.SignatureTable,
			Columns: []string{timestamp.SignatureColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(signature.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{timestamp.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// TimestampUpdateOne is the builder for updating a single Timestamp entity.
type TimestampUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *TimestampMutation
}

// SetType sets the "type" field.
func (_u *TimestampUpdateOne) SetType(v string) *TimestampUpdateOne {
	_u.mutation.SetType(v)
	return _u
}

// SetNillableType sets the "type" field if the given value is not nil.
func (_u *TimestampUpdateOne) SetNillableType(v *string) *TimestampUpdateOne {
	if v != nil {
		_u.SetType(*v)
	}
	return _u
}

// SetTimestamp sets the "timestamp" field.
func (_u *TimestampUpdateOne) SetTimestamp(v time.Time) *TimestampUpdateOne {
	_u.mutation.SetTimestamp(v)
	return _u
}

// SetNillableTimestamp sets the "timestamp" field if the given value is not nil.
func (_u *TimestampUpdateOne) SetNillableTimestamp(v *time.Time) *TimestampUpdateOne {
	if v != nil {
		_u.SetTimestamp(*v)
	}
	return _u
}

// SetSignatureID sets the "signature" edge to the Signature entity by ID.
func (_u *TimestampUpdateOne) SetSignatureID(id uuid.UUID) *TimestampUpdateOne {
	_u.mutation.SetSignatureID(id)
	return _u
}

// SetNillableSignatureID sets the "signature" edge to the Signature entity by ID if the given value is not nil.
func (_u *TimestampUpdateOne) SetNillableSignatureID(id *uuid.UUID) *TimestampUpdateOne {
	if id != nil {
		_u = _u.SetSignatureID(*id)
	}
	return _u
}

// SetSignature sets the "signature" edge to the Signature entity.
func (_u *TimestampUpdateOne) SetSignature(v *Signature) *TimestampUpdateOne {
	return _u.SetSignatureID(v.ID)
}

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

// ClearSignature clears the "signature" edge to the Signature entity.
func (_u *TimestampUpdateOne) ClearSignature() *TimestampUpdateOne {
	_u.mutation.ClearSignature()
	return _u
}

// Where appends a list predicates to the TimestampUpdate builder.
func (_u *TimestampUpdateOne) Where(ps ...predicate.Timestamp) *TimestampUpdateOne {
	_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 *TimestampUpdateOne) Select(field string, fields ...string) *TimestampUpdateOne {
	_u.fields = append([]string{field}, fields...)
	return _u
}

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

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

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

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

func (_u *TimestampUpdateOne) sqlSave(ctx context.Context) (_node *Timestamp, err error) {
	_spec := sqlgraph.NewUpdateSpec(timestamp.Table, timestamp.Columns, sqlgraph.NewFieldSpec(timestamp.FieldID, field.TypeUUID))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Timestamp.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, timestamp.FieldID)
		for _, f := range fields {
			if !timestamp.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != timestamp.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.GetType(); ok {
		_spec.SetField(timestamp.FieldType, field.TypeString, value)
	}
	if value, ok := _u.mutation.Timestamp(); ok {
		_spec.SetField(timestamp.FieldTimestamp, field.TypeTime, value)
	}
	if _u.mutation.SignatureCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   timestamp.SignatureTable,
			Columns: []string{timestamp.SignatureColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(signature.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.SignatureIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   timestamp.SignatureTable,
			Columns: []string{timestamp.SignatureColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(signature.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &Timestamp{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{timestamp.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
