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

// ArtifactUpdate is the builder for updating Artifact entities.
type ArtifactUpdate struct {
	config
	hooks    []Hook
	mutation *ArtifactMutation
}

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

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

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

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

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

// SetKind sets the "kind" field.
func (_u *ArtifactUpdate) SetKind(v string) *ArtifactUpdate {
	_u.mutation.SetKind(v)
	return _u
}

// SetNillableKind sets the "kind" field if the given value is not nil.
func (_u *ArtifactUpdate) SetNillableKind(v *string) *ArtifactUpdate {
	if v != nil {
		_u.SetKind(*v)
	}
	return _u
}

// SetURI sets the "uri" field.
func (_u *ArtifactUpdate) SetURI(v string) *ArtifactUpdate {
	_u.mutation.SetURI(v)
	return _u
}

// SetNillableURI sets the "uri" field if the given value is not nil.
func (_u *ArtifactUpdate) SetNillableURI(v *string) *ArtifactUpdate {
	if v != nil {
		_u.SetURI(*v)
	}
	return _u
}

// SetVersion sets the "version" field.
func (_u *ArtifactUpdate) SetVersion(v string) *ArtifactUpdate {
	_u.mutation.SetVersion(v)
	return _u
}

// SetNillableVersion sets the "version" field if the given value is not nil.
func (_u *ArtifactUpdate) SetNillableVersion(v *string) *ArtifactUpdate {
	if v != nil {
		_u.SetVersion(*v)
	}
	return _u
}

// SetHash sets the "hash" field.
func (_u *ArtifactUpdate) SetHash(v string) *ArtifactUpdate {
	_u.mutation.SetHash(v)
	return _u
}

// SetNillableHash sets the "hash" field if the given value is not nil.
func (_u *ArtifactUpdate) SetNillableHash(v *string) *ArtifactUpdate {
	if v != nil {
		_u.SetHash(*v)
	}
	return _u
}

// SetSize sets the "size" field.
func (_u *ArtifactUpdate) SetSize(v int64) *ArtifactUpdate {
	_u.mutation.ResetSize()
	_u.mutation.SetSize(v)
	return _u
}

// SetNillableSize sets the "size" field if the given value is not nil.
func (_u *ArtifactUpdate) SetNillableSize(v *int64) *ArtifactUpdate {
	if v != nil {
		_u.SetSize(*v)
	}
	return _u
}

// AddSize adds value to the "size" field.
func (_u *ArtifactUpdate) AddSize(v int64) *ArtifactUpdate {
	_u.mutation.AddSize(v)
	return _u
}

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

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

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

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

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

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

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

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

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

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

func (_u *ArtifactUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(artifact.Table, artifact.Columns, sqlgraph.NewFieldSpec(artifact.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(artifact.FieldAstraID, field.TypeString, value)
	}
	if value, ok := _u.mutation.Name(); ok {
		_spec.SetField(artifact.FieldName, field.TypeString, value)
	}
	if value, ok := _u.mutation.Kind(); ok {
		_spec.SetField(artifact.FieldKind, field.TypeString, value)
	}
	if value, ok := _u.mutation.URI(); ok {
		_spec.SetField(artifact.FieldURI, field.TypeString, value)
	}
	if value, ok := _u.mutation.Version(); ok {
		_spec.SetField(artifact.FieldVersion, field.TypeString, value)
	}
	if value, ok := _u.mutation.Hash(); ok {
		_spec.SetField(artifact.FieldHash, field.TypeString, value)
	}
	if value, ok := _u.mutation.Size(); ok {
		_spec.SetField(artifact.FieldSize, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedSize(); ok {
		_spec.AddField(artifact.FieldSize, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.Metadata(); ok {
		_spec.SetField(artifact.FieldMetadata, field.TypeJSON, value)
	}
	if _u.mutation.MetadataCleared() {
		_spec.ClearField(artifact.FieldMetadata, field.TypeJSON)
	}
	if value, ok := _u.mutation.Completeness(); ok {
		_spec.SetField(artifact.FieldCompleteness, field.TypeEnum, value)
	}
	if value, ok := _u.mutation.CreatedAt(); ok {
		_spec.SetField(artifact.FieldCreatedAt, field.TypeTime, value)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{artifact.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// ArtifactUpdateOne is the builder for updating a single Artifact entity.
type ArtifactUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *ArtifactMutation
}

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

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

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

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

// SetKind sets the "kind" field.
func (_u *ArtifactUpdateOne) SetKind(v string) *ArtifactUpdateOne {
	_u.mutation.SetKind(v)
	return _u
}

// SetNillableKind sets the "kind" field if the given value is not nil.
func (_u *ArtifactUpdateOne) SetNillableKind(v *string) *ArtifactUpdateOne {
	if v != nil {
		_u.SetKind(*v)
	}
	return _u
}

// SetURI sets the "uri" field.
func (_u *ArtifactUpdateOne) SetURI(v string) *ArtifactUpdateOne {
	_u.mutation.SetURI(v)
	return _u
}

// SetNillableURI sets the "uri" field if the given value is not nil.
func (_u *ArtifactUpdateOne) SetNillableURI(v *string) *ArtifactUpdateOne {
	if v != nil {
		_u.SetURI(*v)
	}
	return _u
}

// SetVersion sets the "version" field.
func (_u *ArtifactUpdateOne) SetVersion(v string) *ArtifactUpdateOne {
	_u.mutation.SetVersion(v)
	return _u
}

// SetNillableVersion sets the "version" field if the given value is not nil.
func (_u *ArtifactUpdateOne) SetNillableVersion(v *string) *ArtifactUpdateOne {
	if v != nil {
		_u.SetVersion(*v)
	}
	return _u
}

// SetHash sets the "hash" field.
func (_u *ArtifactUpdateOne) SetHash(v string) *ArtifactUpdateOne {
	_u.mutation.SetHash(v)
	return _u
}

// SetNillableHash sets the "hash" field if the given value is not nil.
func (_u *ArtifactUpdateOne) SetNillableHash(v *string) *ArtifactUpdateOne {
	if v != nil {
		_u.SetHash(*v)
	}
	return _u
}

// SetSize sets the "size" field.
func (_u *ArtifactUpdateOne) SetSize(v int64) *ArtifactUpdateOne {
	_u.mutation.ResetSize()
	_u.mutation.SetSize(v)
	return _u
}

// SetNillableSize sets the "size" field if the given value is not nil.
func (_u *ArtifactUpdateOne) SetNillableSize(v *int64) *ArtifactUpdateOne {
	if v != nil {
		_u.SetSize(*v)
	}
	return _u
}

// AddSize adds value to the "size" field.
func (_u *ArtifactUpdateOne) AddSize(v int64) *ArtifactUpdateOne {
	_u.mutation.AddSize(v)
	return _u
}

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

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

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

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

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

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

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

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

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

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

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

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

func (_u *ArtifactUpdateOne) sqlSave(ctx context.Context) (_node *Artifact, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(artifact.Table, artifact.Columns, sqlgraph.NewFieldSpec(artifact.FieldID, field.TypeInt))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Artifact.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, artifact.FieldID)
		for _, f := range fields {
			if !artifact.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != artifact.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(artifact.FieldAstraID, field.TypeString, value)
	}
	if value, ok := _u.mutation.Name(); ok {
		_spec.SetField(artifact.FieldName, field.TypeString, value)
	}
	if value, ok := _u.mutation.Kind(); ok {
		_spec.SetField(artifact.FieldKind, field.TypeString, value)
	}
	if value, ok := _u.mutation.URI(); ok {
		_spec.SetField(artifact.FieldURI, field.TypeString, value)
	}
	if value, ok := _u.mutation.Version(); ok {
		_spec.SetField(artifact.FieldVersion, field.TypeString, value)
	}
	if value, ok := _u.mutation.Hash(); ok {
		_spec.SetField(artifact.FieldHash, field.TypeString, value)
	}
	if value, ok := _u.mutation.Size(); ok {
		_spec.SetField(artifact.FieldSize, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedSize(); ok {
		_spec.AddField(artifact.FieldSize, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.Metadata(); ok {
		_spec.SetField(artifact.FieldMetadata, field.TypeJSON, value)
	}
	if _u.mutation.MetadataCleared() {
		_spec.ClearField(artifact.FieldMetadata, field.TypeJSON)
	}
	if value, ok := _u.mutation.Completeness(); ok {
		_spec.SetField(artifact.FieldCompleteness, field.TypeEnum, value)
	}
	if value, ok := _u.mutation.CreatedAt(); ok {
		_spec.SetField(artifact.FieldCreatedAt, field.TypeTime, value)
	}
	_node = &Artifact{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{artifact.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
