// 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/attestation"
	"github.com/in-toto/archivista/ent/attestationcollection"
	"github.com/in-toto/archivista/ent/predicate"
	"github.com/in-toto/archivista/ent/statement"
)

// AttestationCollectionUpdate is the builder for updating AttestationCollection entities.
type AttestationCollectionUpdate struct {
	config
	hooks    []Hook
	mutation *AttestationCollectionMutation
}

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

// SetName sets the "name" field.
func (_u *AttestationCollectionUpdate) SetName(v string) *AttestationCollectionUpdate {
	_u.mutation.SetName(v)
	return _u
}

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

// AddAttestationIDs adds the "attestations" edge to the Attestation entity by IDs.
func (_u *AttestationCollectionUpdate) AddAttestationIDs(ids ...uuid.UUID) *AttestationCollectionUpdate {
	_u.mutation.AddAttestationIDs(ids...)
	return _u
}

// AddAttestations adds the "attestations" edges to the Attestation entity.
func (_u *AttestationCollectionUpdate) AddAttestations(v ...*Attestation) *AttestationCollectionUpdate {
	ids := make([]uuid.UUID, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddAttestationIDs(ids...)
}

// SetStatementID sets the "statement" edge to the Statement entity by ID.
func (_u *AttestationCollectionUpdate) SetStatementID(id uuid.UUID) *AttestationCollectionUpdate {
	_u.mutation.SetStatementID(id)
	return _u
}

// SetStatement sets the "statement" edge to the Statement entity.
func (_u *AttestationCollectionUpdate) SetStatement(v *Statement) *AttestationCollectionUpdate {
	return _u.SetStatementID(v.ID)
}

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

// ClearAttestations clears all "attestations" edges to the Attestation entity.
func (_u *AttestationCollectionUpdate) ClearAttestations() *AttestationCollectionUpdate {
	_u.mutation.ClearAttestations()
	return _u
}

// RemoveAttestationIDs removes the "attestations" edge to Attestation entities by IDs.
func (_u *AttestationCollectionUpdate) RemoveAttestationIDs(ids ...uuid.UUID) *AttestationCollectionUpdate {
	_u.mutation.RemoveAttestationIDs(ids...)
	return _u
}

// RemoveAttestations removes "attestations" edges to Attestation entities.
func (_u *AttestationCollectionUpdate) RemoveAttestations(v ...*Attestation) *AttestationCollectionUpdate {
	ids := make([]uuid.UUID, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveAttestationIDs(ids...)
}

// ClearStatement clears the "statement" edge to the Statement entity.
func (_u *AttestationCollectionUpdate) ClearStatement() *AttestationCollectionUpdate {
	_u.mutation.ClearStatement()
	return _u
}

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *AttestationCollectionUpdate) 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 *AttestationCollectionUpdate) check() error {
	if v, ok := _u.mutation.Name(); ok {
		if err := attestationcollection.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "AttestationCollection.name": %w`, err)}
		}
	}
	if _u.mutation.StatementCleared() && len(_u.mutation.StatementIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "AttestationCollection.statement"`)
	}
	return nil
}

func (_u *AttestationCollectionUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(attestationcollection.Table, attestationcollection.Columns, sqlgraph.NewFieldSpec(attestationcollection.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.Name(); ok {
		_spec.SetField(attestationcollection.FieldName, field.TypeString, value)
	}
	if _u.mutation.AttestationsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   attestationcollection.AttestationsTable,
			Columns: []string{attestationcollection.AttestationsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(attestation.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedAttestationsIDs(); len(nodes) > 0 && !_u.mutation.AttestationsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   attestationcollection.AttestationsTable,
			Columns: []string{attestationcollection.AttestationsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(attestation.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.AttestationsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   attestationcollection.AttestationsTable,
			Columns: []string{attestationcollection.AttestationsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(attestation.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.StatementCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   attestationcollection.StatementTable,
			Columns: []string{attestationcollection.StatementColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(statement.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.StatementIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   attestationcollection.StatementTable,
			Columns: []string{attestationcollection.StatementColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(statement.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{attestationcollection.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// AttestationCollectionUpdateOne is the builder for updating a single AttestationCollection entity.
type AttestationCollectionUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *AttestationCollectionMutation
}

// SetName sets the "name" field.
func (_u *AttestationCollectionUpdateOne) SetName(v string) *AttestationCollectionUpdateOne {
	_u.mutation.SetName(v)
	return _u
}

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

// AddAttestationIDs adds the "attestations" edge to the Attestation entity by IDs.
func (_u *AttestationCollectionUpdateOne) AddAttestationIDs(ids ...uuid.UUID) *AttestationCollectionUpdateOne {
	_u.mutation.AddAttestationIDs(ids...)
	return _u
}

// AddAttestations adds the "attestations" edges to the Attestation entity.
func (_u *AttestationCollectionUpdateOne) AddAttestations(v ...*Attestation) *AttestationCollectionUpdateOne {
	ids := make([]uuid.UUID, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddAttestationIDs(ids...)
}

// SetStatementID sets the "statement" edge to the Statement entity by ID.
func (_u *AttestationCollectionUpdateOne) SetStatementID(id uuid.UUID) *AttestationCollectionUpdateOne {
	_u.mutation.SetStatementID(id)
	return _u
}

// SetStatement sets the "statement" edge to the Statement entity.
func (_u *AttestationCollectionUpdateOne) SetStatement(v *Statement) *AttestationCollectionUpdateOne {
	return _u.SetStatementID(v.ID)
}

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

// ClearAttestations clears all "attestations" edges to the Attestation entity.
func (_u *AttestationCollectionUpdateOne) ClearAttestations() *AttestationCollectionUpdateOne {
	_u.mutation.ClearAttestations()
	return _u
}

// RemoveAttestationIDs removes the "attestations" edge to Attestation entities by IDs.
func (_u *AttestationCollectionUpdateOne) RemoveAttestationIDs(ids ...uuid.UUID) *AttestationCollectionUpdateOne {
	_u.mutation.RemoveAttestationIDs(ids...)
	return _u
}

// RemoveAttestations removes "attestations" edges to Attestation entities.
func (_u *AttestationCollectionUpdateOne) RemoveAttestations(v ...*Attestation) *AttestationCollectionUpdateOne {
	ids := make([]uuid.UUID, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveAttestationIDs(ids...)
}

// ClearStatement clears the "statement" edge to the Statement entity.
func (_u *AttestationCollectionUpdateOne) ClearStatement() *AttestationCollectionUpdateOne {
	_u.mutation.ClearStatement()
	return _u
}

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

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *AttestationCollectionUpdateOne) 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 *AttestationCollectionUpdateOne) check() error {
	if v, ok := _u.mutation.Name(); ok {
		if err := attestationcollection.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "AttestationCollection.name": %w`, err)}
		}
	}
	if _u.mutation.StatementCleared() && len(_u.mutation.StatementIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "AttestationCollection.statement"`)
	}
	return nil
}

func (_u *AttestationCollectionUpdateOne) sqlSave(ctx context.Context) (_node *AttestationCollection, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(attestationcollection.Table, attestationcollection.Columns, sqlgraph.NewFieldSpec(attestationcollection.FieldID, field.TypeUUID))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "AttestationCollection.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, attestationcollection.FieldID)
		for _, f := range fields {
			if !attestationcollection.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != attestationcollection.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.Name(); ok {
		_spec.SetField(attestationcollection.FieldName, field.TypeString, value)
	}
	if _u.mutation.AttestationsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   attestationcollection.AttestationsTable,
			Columns: []string{attestationcollection.AttestationsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(attestation.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedAttestationsIDs(); len(nodes) > 0 && !_u.mutation.AttestationsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   attestationcollection.AttestationsTable,
			Columns: []string{attestationcollection.AttestationsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(attestation.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.AttestationsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   attestationcollection.AttestationsTable,
			Columns: []string{attestationcollection.AttestationsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(attestation.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.StatementCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   attestationcollection.StatementTable,
			Columns: []string{attestationcollection.StatementColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(statement.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.StatementIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   attestationcollection.StatementTable,
			Columns: []string{attestationcollection.StatementColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(statement.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &AttestationCollection{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{attestationcollection.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
