// 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/TSELab/astra/internal/store/ent/predicate"
	"github.com/TSELab/astra/internal/store/ent/principal"
)

// PrincipalUpdate is the builder for updating Principal entities.
type PrincipalUpdate struct {
	config
	hooks    []Hook
	mutation *PrincipalMutation
}

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

// SetAstraID sets the "astra_id" field.
func (_u *PrincipalUpdate) SetAstraID(v string) *PrincipalUpdate {
	_u.mutation.SetAstraID(v)
	return _u
}

// SetNillableAstraID sets the "astra_id" field if the given value is not nil.
func (_u *PrincipalUpdate) SetNillableAstraID(v *string) *PrincipalUpdate {
	if v != nil {
		_u.SetAstraID(*v)
	}
	return _u
}

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

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

// SetTrust sets the "trust" field.
func (_u *PrincipalUpdate) SetTrust(v string) *PrincipalUpdate {
	_u.mutation.SetTrust(v)
	return _u
}

// SetNillableTrust sets the "trust" field if the given value is not nil.
func (_u *PrincipalUpdate) SetNillableTrust(v *string) *PrincipalUpdate {
	if v != nil {
		_u.SetTrust(*v)
	}
	return _u
}

// SetBuilder sets the "builder" field.
func (_u *PrincipalUpdate) SetBuilder(v string) *PrincipalUpdate {
	_u.mutation.SetBuilder(v)
	return _u
}

// SetNillableBuilder sets the "builder" field if the given value is not nil.
func (_u *PrincipalUpdate) SetNillableBuilder(v *string) *PrincipalUpdate {
	if v != nil {
		_u.SetBuilder(*v)
	}
	return _u
}

// SetMetadata sets the "metadata" field.
func (_u *PrincipalUpdate) SetMetadata(v map[string]string) *PrincipalUpdate {
	_u.mutation.SetMetadata(v)
	return _u
}

// ClearMetadata clears the value of the "metadata" field.
func (_u *PrincipalUpdate) ClearMetadata() *PrincipalUpdate {
	_u.mutation.ClearMetadata()
	return _u
}

// SetCompleteness sets the "completeness" field.
func (_u *PrincipalUpdate) SetCompleteness(v principal.Completeness) *PrincipalUpdate {
	_u.mutation.SetCompleteness(v)
	return _u
}

// SetNillableCompleteness sets the "completeness" field if the given value is not nil.
func (_u *PrincipalUpdate) SetNillableCompleteness(v *principal.Completeness) *PrincipalUpdate {
	if v != nil {
		_u.SetCompleteness(*v)
	}
	return _u
}

// SetCreatedAt sets the "created_at" field.
func (_u *PrincipalUpdate) SetCreatedAt(v time.Time) *PrincipalUpdate {
	_u.mutation.SetCreatedAt(v)
	return _u
}

// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (_u *PrincipalUpdate) SetNillableCreatedAt(v *time.Time) *PrincipalUpdate {
	if v != nil {
		_u.SetCreatedAt(*v)
	}
	return _u
}

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *PrincipalUpdate) 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 *PrincipalUpdate) check() error {
	if v, ok := _u.mutation.AstraID(); ok {
		if err := principal.AstraIDValidator(v); err != nil {
			return &ValidationError{Name: "astra_id", err: fmt.Errorf(`ent: validator failed for field "Principal.astra_id": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Completeness(); ok {
		if err := principal.CompletenessValidator(v); err != nil {
			return &ValidationError{Name: "completeness", err: fmt.Errorf(`ent: validator failed for field "Principal.completeness": %w`, err)}
		}
	}
	return nil
}

func (_u *PrincipalUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(principal.Table, principal.Columns, sqlgraph.NewFieldSpec(principal.FieldID, field.TypeInt))
	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.AstraID(); ok {
		_spec.SetField(principal.FieldAstraID, field.TypeString, value)
	}
	if value, ok := _u.mutation.Name(); ok {
		_spec.SetField(principal.FieldName, field.TypeString, value)
	}
	if value, ok := _u.mutation.Trust(); ok {
		_spec.SetField(principal.FieldTrust, field.TypeString, value)
	}
	if value, ok := _u.mutation.Builder(); ok {
		_spec.SetField(principal.FieldBuilder, field.TypeString, value)
	}
	if value, ok := _u.mutation.Metadata(); ok {
		_spec.SetField(principal.FieldMetadata, field.TypeJSON, value)
	}
	if _u.mutation.MetadataCleared() {
		_spec.ClearField(principal.FieldMetadata, field.TypeJSON)
	}
	if value, ok := _u.mutation.Completeness(); ok {
		_spec.SetField(principal.FieldCompleteness, field.TypeEnum, value)
	}
	if value, ok := _u.mutation.CreatedAt(); ok {
		_spec.SetField(principal.FieldCreatedAt, field.TypeTime, value)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{principal.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// PrincipalUpdateOne is the builder for updating a single Principal entity.
type PrincipalUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *PrincipalMutation
}

// SetAstraID sets the "astra_id" field.
func (_u *PrincipalUpdateOne) SetAstraID(v string) *PrincipalUpdateOne {
	_u.mutation.SetAstraID(v)
	return _u
}

// SetNillableAstraID sets the "astra_id" field if the given value is not nil.
func (_u *PrincipalUpdateOne) SetNillableAstraID(v *string) *PrincipalUpdateOne {
	if v != nil {
		_u.SetAstraID(*v)
	}
	return _u
}

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

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

// SetTrust sets the "trust" field.
func (_u *PrincipalUpdateOne) SetTrust(v string) *PrincipalUpdateOne {
	_u.mutation.SetTrust(v)
	return _u
}

// SetNillableTrust sets the "trust" field if the given value is not nil.
func (_u *PrincipalUpdateOne) SetNillableTrust(v *string) *PrincipalUpdateOne {
	if v != nil {
		_u.SetTrust(*v)
	}
	return _u
}

// SetBuilder sets the "builder" field.
func (_u *PrincipalUpdateOne) SetBuilder(v string) *PrincipalUpdateOne {
	_u.mutation.SetBuilder(v)
	return _u
}

// SetNillableBuilder sets the "builder" field if the given value is not nil.
func (_u *PrincipalUpdateOne) SetNillableBuilder(v *string) *PrincipalUpdateOne {
	if v != nil {
		_u.SetBuilder(*v)
	}
	return _u
}

// SetMetadata sets the "metadata" field.
func (_u *PrincipalUpdateOne) SetMetadata(v map[string]string) *PrincipalUpdateOne {
	_u.mutation.SetMetadata(v)
	return _u
}

// ClearMetadata clears the value of the "metadata" field.
func (_u *PrincipalUpdateOne) ClearMetadata() *PrincipalUpdateOne {
	_u.mutation.ClearMetadata()
	return _u
}

// SetCompleteness sets the "completeness" field.
func (_u *PrincipalUpdateOne) SetCompleteness(v principal.Completeness) *PrincipalUpdateOne {
	_u.mutation.SetCompleteness(v)
	return _u
}

// SetNillableCompleteness sets the "completeness" field if the given value is not nil.
func (_u *PrincipalUpdateOne) SetNillableCompleteness(v *principal.Completeness) *PrincipalUpdateOne {
	if v != nil {
		_u.SetCompleteness(*v)
	}
	return _u
}

// SetCreatedAt sets the "created_at" field.
func (_u *PrincipalUpdateOne) SetCreatedAt(v time.Time) *PrincipalUpdateOne {
	_u.mutation.SetCreatedAt(v)
	return _u
}

// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (_u *PrincipalUpdateOne) SetNillableCreatedAt(v *time.Time) *PrincipalUpdateOne {
	if v != nil {
		_u.SetCreatedAt(*v)
	}
	return _u
}

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

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

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *PrincipalUpdateOne) 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 *PrincipalUpdateOne) check() error {
	if v, ok := _u.mutation.AstraID(); ok {
		if err := principal.AstraIDValidator(v); err != nil {
			return &ValidationError{Name: "astra_id", err: fmt.Errorf(`ent: validator failed for field "Principal.astra_id": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Completeness(); ok {
		if err := principal.CompletenessValidator(v); err != nil {
			return &ValidationError{Name: "completeness", err: fmt.Errorf(`ent: validator failed for field "Principal.completeness": %w`, err)}
		}
	}
	return nil
}

func (_u *PrincipalUpdateOne) sqlSave(ctx context.Context) (_node *Principal, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(principal.Table, principal.Columns, sqlgraph.NewFieldSpec(principal.FieldID, field.TypeInt))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Principal.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, principal.FieldID)
		for _, f := range fields {
			if !principal.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != principal.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.AstraID(); ok {
		_spec.SetField(principal.FieldAstraID, field.TypeString, value)
	}
	if value, ok := _u.mutation.Name(); ok {
		_spec.SetField(principal.FieldName, field.TypeString, value)
	}
	if value, ok := _u.mutation.Trust(); ok {
		_spec.SetField(principal.FieldTrust, field.TypeString, value)
	}
	if value, ok := _u.mutation.Builder(); ok {
		_spec.SetField(principal.FieldBuilder, field.TypeString, value)
	}
	if value, ok := _u.mutation.Metadata(); ok {
		_spec.SetField(principal.FieldMetadata, field.TypeJSON, value)
	}
	if _u.mutation.MetadataCleared() {
		_spec.ClearField(principal.FieldMetadata, field.TypeJSON)
	}
	if value, ok := _u.mutation.Completeness(); ok {
		_spec.SetField(principal.FieldCompleteness, field.TypeEnum, value)
	}
	if value, ok := _u.mutation.CreatedAt(); ok {
		_spec.SetField(principal.FieldCreatedAt, field.TypeTime, value)
	}
	_node = &Principal{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{principal.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
