// 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/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/predicate"
	"github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/robotaccount"
	"github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/workflow"
	"github.com/google/uuid"
)

// RobotAccountUpdate is the builder for updating RobotAccount entities.
type RobotAccountUpdate struct {
	config
	hooks     []Hook
	mutation  *RobotAccountMutation
	modifiers []func(*sql.UpdateBuilder)
}

// Where appends a list predicates to the RobotAccountUpdate builder.
func (rau *RobotAccountUpdate) Where(ps ...predicate.RobotAccount) *RobotAccountUpdate {
	rau.mutation.Where(ps...)
	return rau
}

// SetName sets the "name" field.
func (rau *RobotAccountUpdate) SetName(s string) *RobotAccountUpdate {
	rau.mutation.SetName(s)
	return rau
}

// SetNillableName sets the "name" field if the given value is not nil.
func (rau *RobotAccountUpdate) SetNillableName(s *string) *RobotAccountUpdate {
	if s != nil {
		rau.SetName(*s)
	}
	return rau
}

// SetRevokedAt sets the "revoked_at" field.
func (rau *RobotAccountUpdate) SetRevokedAt(t time.Time) *RobotAccountUpdate {
	rau.mutation.SetRevokedAt(t)
	return rau
}

// SetNillableRevokedAt sets the "revoked_at" field if the given value is not nil.
func (rau *RobotAccountUpdate) SetNillableRevokedAt(t *time.Time) *RobotAccountUpdate {
	if t != nil {
		rau.SetRevokedAt(*t)
	}
	return rau
}

// ClearRevokedAt clears the value of the "revoked_at" field.
func (rau *RobotAccountUpdate) ClearRevokedAt() *RobotAccountUpdate {
	rau.mutation.ClearRevokedAt()
	return rau
}

// SetWorkflowID sets the "workflow" edge to the Workflow entity by ID.
func (rau *RobotAccountUpdate) SetWorkflowID(id uuid.UUID) *RobotAccountUpdate {
	rau.mutation.SetWorkflowID(id)
	return rau
}

// SetNillableWorkflowID sets the "workflow" edge to the Workflow entity by ID if the given value is not nil.
func (rau *RobotAccountUpdate) SetNillableWorkflowID(id *uuid.UUID) *RobotAccountUpdate {
	if id != nil {
		rau = rau.SetWorkflowID(*id)
	}
	return rau
}

// SetWorkflow sets the "workflow" edge to the Workflow entity.
func (rau *RobotAccountUpdate) SetWorkflow(w *Workflow) *RobotAccountUpdate {
	return rau.SetWorkflowID(w.ID)
}

// Mutation returns the RobotAccountMutation object of the builder.
func (rau *RobotAccountUpdate) Mutation() *RobotAccountMutation {
	return rau.mutation
}

// ClearWorkflow clears the "workflow" edge to the Workflow entity.
func (rau *RobotAccountUpdate) ClearWorkflow() *RobotAccountUpdate {
	rau.mutation.ClearWorkflow()
	return rau
}

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

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

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

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

// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (rau *RobotAccountUpdate) Modify(modifiers ...func(u *sql.UpdateBuilder)) *RobotAccountUpdate {
	rau.modifiers = append(rau.modifiers, modifiers...)
	return rau
}

func (rau *RobotAccountUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(robotaccount.Table, robotaccount.Columns, sqlgraph.NewFieldSpec(robotaccount.FieldID, field.TypeUUID))
	if ps := rau.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := rau.mutation.Name(); ok {
		_spec.SetField(robotaccount.FieldName, field.TypeString, value)
	}
	if value, ok := rau.mutation.RevokedAt(); ok {
		_spec.SetField(robotaccount.FieldRevokedAt, field.TypeTime, value)
	}
	if rau.mutation.RevokedAtCleared() {
		_spec.ClearField(robotaccount.FieldRevokedAt, field.TypeTime)
	}
	if rau.mutation.WorkflowCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   robotaccount.WorkflowTable,
			Columns: []string{robotaccount.WorkflowColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(workflow.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := rau.mutation.WorkflowIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   robotaccount.WorkflowTable,
			Columns: []string{robotaccount.WorkflowColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(workflow.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_spec.AddModifiers(rau.modifiers...)
	if n, err = sqlgraph.UpdateNodes(ctx, rau.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{robotaccount.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	rau.mutation.done = true
	return n, nil
}

// RobotAccountUpdateOne is the builder for updating a single RobotAccount entity.
type RobotAccountUpdateOne struct {
	config
	fields    []string
	hooks     []Hook
	mutation  *RobotAccountMutation
	modifiers []func(*sql.UpdateBuilder)
}

// SetName sets the "name" field.
func (rauo *RobotAccountUpdateOne) SetName(s string) *RobotAccountUpdateOne {
	rauo.mutation.SetName(s)
	return rauo
}

// SetNillableName sets the "name" field if the given value is not nil.
func (rauo *RobotAccountUpdateOne) SetNillableName(s *string) *RobotAccountUpdateOne {
	if s != nil {
		rauo.SetName(*s)
	}
	return rauo
}

// SetRevokedAt sets the "revoked_at" field.
func (rauo *RobotAccountUpdateOne) SetRevokedAt(t time.Time) *RobotAccountUpdateOne {
	rauo.mutation.SetRevokedAt(t)
	return rauo
}

// SetNillableRevokedAt sets the "revoked_at" field if the given value is not nil.
func (rauo *RobotAccountUpdateOne) SetNillableRevokedAt(t *time.Time) *RobotAccountUpdateOne {
	if t != nil {
		rauo.SetRevokedAt(*t)
	}
	return rauo
}

// ClearRevokedAt clears the value of the "revoked_at" field.
func (rauo *RobotAccountUpdateOne) ClearRevokedAt() *RobotAccountUpdateOne {
	rauo.mutation.ClearRevokedAt()
	return rauo
}

// SetWorkflowID sets the "workflow" edge to the Workflow entity by ID.
func (rauo *RobotAccountUpdateOne) SetWorkflowID(id uuid.UUID) *RobotAccountUpdateOne {
	rauo.mutation.SetWorkflowID(id)
	return rauo
}

// SetNillableWorkflowID sets the "workflow" edge to the Workflow entity by ID if the given value is not nil.
func (rauo *RobotAccountUpdateOne) SetNillableWorkflowID(id *uuid.UUID) *RobotAccountUpdateOne {
	if id != nil {
		rauo = rauo.SetWorkflowID(*id)
	}
	return rauo
}

// SetWorkflow sets the "workflow" edge to the Workflow entity.
func (rauo *RobotAccountUpdateOne) SetWorkflow(w *Workflow) *RobotAccountUpdateOne {
	return rauo.SetWorkflowID(w.ID)
}

// Mutation returns the RobotAccountMutation object of the builder.
func (rauo *RobotAccountUpdateOne) Mutation() *RobotAccountMutation {
	return rauo.mutation
}

// ClearWorkflow clears the "workflow" edge to the Workflow entity.
func (rauo *RobotAccountUpdateOne) ClearWorkflow() *RobotAccountUpdateOne {
	rauo.mutation.ClearWorkflow()
	return rauo
}

// Where appends a list predicates to the RobotAccountUpdate builder.
func (rauo *RobotAccountUpdateOne) Where(ps ...predicate.RobotAccount) *RobotAccountUpdateOne {
	rauo.mutation.Where(ps...)
	return rauo
}

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

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

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

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

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

// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (rauo *RobotAccountUpdateOne) Modify(modifiers ...func(u *sql.UpdateBuilder)) *RobotAccountUpdateOne {
	rauo.modifiers = append(rauo.modifiers, modifiers...)
	return rauo
}

func (rauo *RobotAccountUpdateOne) sqlSave(ctx context.Context) (_node *RobotAccount, err error) {
	_spec := sqlgraph.NewUpdateSpec(robotaccount.Table, robotaccount.Columns, sqlgraph.NewFieldSpec(robotaccount.FieldID, field.TypeUUID))
	id, ok := rauo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "RobotAccount.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := rauo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, robotaccount.FieldID)
		for _, f := range fields {
			if !robotaccount.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != robotaccount.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := rauo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := rauo.mutation.Name(); ok {
		_spec.SetField(robotaccount.FieldName, field.TypeString, value)
	}
	if value, ok := rauo.mutation.RevokedAt(); ok {
		_spec.SetField(robotaccount.FieldRevokedAt, field.TypeTime, value)
	}
	if rauo.mutation.RevokedAtCleared() {
		_spec.ClearField(robotaccount.FieldRevokedAt, field.TypeTime)
	}
	if rauo.mutation.WorkflowCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   robotaccount.WorkflowTable,
			Columns: []string{robotaccount.WorkflowColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(workflow.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := rauo.mutation.WorkflowIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   robotaccount.WorkflowTable,
			Columns: []string{robotaccount.WorkflowColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(workflow.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_spec.AddModifiers(rauo.modifiers...)
	_node = &RobotAccount{config: rauo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, rauo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{robotaccount.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	rauo.mutation.done = true
	return _node, nil
}
