// Code generated by ent, DO NOT EDIT.

package models

import (
	"context"
	"errors"
	"fmt"
	"juwan-backend/app/dispute/rpc/internal/models/disputetimeline"
	"juwan-backend/app/dispute/rpc/internal/models/predicate"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
)

// DisputeTimelineUpdate is the builder for updating DisputeTimeline entities.
type DisputeTimelineUpdate struct {
	config
	hooks    []Hook
	mutation *DisputeTimelineMutation
}

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

// SetDisputeID sets the "dispute_id" field.
func (_u *DisputeTimelineUpdate) SetDisputeID(v int64) *DisputeTimelineUpdate {
	_u.mutation.ResetDisputeID()
	_u.mutation.SetDisputeID(v)
	return _u
}

// SetNillableDisputeID sets the "dispute_id" field if the given value is not nil.
func (_u *DisputeTimelineUpdate) SetNillableDisputeID(v *int64) *DisputeTimelineUpdate {
	if v != nil {
		_u.SetDisputeID(*v)
	}
	return _u
}

// AddDisputeID adds value to the "dispute_id" field.
func (_u *DisputeTimelineUpdate) AddDisputeID(v int64) *DisputeTimelineUpdate {
	_u.mutation.AddDisputeID(v)
	return _u
}

// SetEventType sets the "event_type" field.
func (_u *DisputeTimelineUpdate) SetEventType(v string) *DisputeTimelineUpdate {
	_u.mutation.SetEventType(v)
	return _u
}

// SetNillableEventType sets the "event_type" field if the given value is not nil.
func (_u *DisputeTimelineUpdate) SetNillableEventType(v *string) *DisputeTimelineUpdate {
	if v != nil {
		_u.SetEventType(*v)
	}
	return _u
}

// SetActorID sets the "actor_id" field.
func (_u *DisputeTimelineUpdate) SetActorID(v int64) *DisputeTimelineUpdate {
	_u.mutation.ResetActorID()
	_u.mutation.SetActorID(v)
	return _u
}

// SetNillableActorID sets the "actor_id" field if the given value is not nil.
func (_u *DisputeTimelineUpdate) SetNillableActorID(v *int64) *DisputeTimelineUpdate {
	if v != nil {
		_u.SetActorID(*v)
	}
	return _u
}

// AddActorID adds value to the "actor_id" field.
func (_u *DisputeTimelineUpdate) AddActorID(v int64) *DisputeTimelineUpdate {
	_u.mutation.AddActorID(v)
	return _u
}

// ClearActorID clears the value of the "actor_id" field.
func (_u *DisputeTimelineUpdate) ClearActorID() *DisputeTimelineUpdate {
	_u.mutation.ClearActorID()
	return _u
}

// SetActorName sets the "actor_name" field.
func (_u *DisputeTimelineUpdate) SetActorName(v string) *DisputeTimelineUpdate {
	_u.mutation.SetActorName(v)
	return _u
}

// SetNillableActorName sets the "actor_name" field if the given value is not nil.
func (_u *DisputeTimelineUpdate) SetNillableActorName(v *string) *DisputeTimelineUpdate {
	if v != nil {
		_u.SetActorName(*v)
	}
	return _u
}

// ClearActorName clears the value of the "actor_name" field.
func (_u *DisputeTimelineUpdate) ClearActorName() *DisputeTimelineUpdate {
	_u.mutation.ClearActorName()
	return _u
}

// SetDetails sets the "details" field.
func (_u *DisputeTimelineUpdate) SetDetails(v map[string]interface{}) *DisputeTimelineUpdate {
	_u.mutation.SetDetails(v)
	return _u
}

// ClearDetails clears the value of the "details" field.
func (_u *DisputeTimelineUpdate) ClearDetails() *DisputeTimelineUpdate {
	_u.mutation.ClearDetails()
	return _u
}

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *DisputeTimelineUpdate) 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 *DisputeTimelineUpdate) check() error {
	if v, ok := _u.mutation.EventType(); ok {
		if err := disputetimeline.EventTypeValidator(v); err != nil {
			return &ValidationError{Name: "event_type", err: fmt.Errorf(`models: validator failed for field "DisputeTimeline.event_type": %w`, err)}
		}
	}
	if v, ok := _u.mutation.ActorName(); ok {
		if err := disputetimeline.ActorNameValidator(v); err != nil {
			return &ValidationError{Name: "actor_name", err: fmt.Errorf(`models: validator failed for field "DisputeTimeline.actor_name": %w`, err)}
		}
	}
	return nil
}

func (_u *DisputeTimelineUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(disputetimeline.Table, disputetimeline.Columns, sqlgraph.NewFieldSpec(disputetimeline.FieldID, field.TypeInt64))
	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.DisputeID(); ok {
		_spec.SetField(disputetimeline.FieldDisputeID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedDisputeID(); ok {
		_spec.AddField(disputetimeline.FieldDisputeID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.EventType(); ok {
		_spec.SetField(disputetimeline.FieldEventType, field.TypeString, value)
	}
	if value, ok := _u.mutation.ActorID(); ok {
		_spec.SetField(disputetimeline.FieldActorID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedActorID(); ok {
		_spec.AddField(disputetimeline.FieldActorID, field.TypeInt64, value)
	}
	if _u.mutation.ActorIDCleared() {
		_spec.ClearField(disputetimeline.FieldActorID, field.TypeInt64)
	}
	if value, ok := _u.mutation.ActorName(); ok {
		_spec.SetField(disputetimeline.FieldActorName, field.TypeString, value)
	}
	if _u.mutation.ActorNameCleared() {
		_spec.ClearField(disputetimeline.FieldActorName, field.TypeString)
	}
	if value, ok := _u.mutation.Details(); ok {
		_spec.SetField(disputetimeline.FieldDetails, field.TypeJSON, value)
	}
	if _u.mutation.DetailsCleared() {
		_spec.ClearField(disputetimeline.FieldDetails, field.TypeJSON)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{disputetimeline.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// DisputeTimelineUpdateOne is the builder for updating a single DisputeTimeline entity.
type DisputeTimelineUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *DisputeTimelineMutation
}

// SetDisputeID sets the "dispute_id" field.
func (_u *DisputeTimelineUpdateOne) SetDisputeID(v int64) *DisputeTimelineUpdateOne {
	_u.mutation.ResetDisputeID()
	_u.mutation.SetDisputeID(v)
	return _u
}

// SetNillableDisputeID sets the "dispute_id" field if the given value is not nil.
func (_u *DisputeTimelineUpdateOne) SetNillableDisputeID(v *int64) *DisputeTimelineUpdateOne {
	if v != nil {
		_u.SetDisputeID(*v)
	}
	return _u
}

// AddDisputeID adds value to the "dispute_id" field.
func (_u *DisputeTimelineUpdateOne) AddDisputeID(v int64) *DisputeTimelineUpdateOne {
	_u.mutation.AddDisputeID(v)
	return _u
}

// SetEventType sets the "event_type" field.
func (_u *DisputeTimelineUpdateOne) SetEventType(v string) *DisputeTimelineUpdateOne {
	_u.mutation.SetEventType(v)
	return _u
}

// SetNillableEventType sets the "event_type" field if the given value is not nil.
func (_u *DisputeTimelineUpdateOne) SetNillableEventType(v *string) *DisputeTimelineUpdateOne {
	if v != nil {
		_u.SetEventType(*v)
	}
	return _u
}

// SetActorID sets the "actor_id" field.
func (_u *DisputeTimelineUpdateOne) SetActorID(v int64) *DisputeTimelineUpdateOne {
	_u.mutation.ResetActorID()
	_u.mutation.SetActorID(v)
	return _u
}

// SetNillableActorID sets the "actor_id" field if the given value is not nil.
func (_u *DisputeTimelineUpdateOne) SetNillableActorID(v *int64) *DisputeTimelineUpdateOne {
	if v != nil {
		_u.SetActorID(*v)
	}
	return _u
}

// AddActorID adds value to the "actor_id" field.
func (_u *DisputeTimelineUpdateOne) AddActorID(v int64) *DisputeTimelineUpdateOne {
	_u.mutation.AddActorID(v)
	return _u
}

// ClearActorID clears the value of the "actor_id" field.
func (_u *DisputeTimelineUpdateOne) ClearActorID() *DisputeTimelineUpdateOne {
	_u.mutation.ClearActorID()
	return _u
}

// SetActorName sets the "actor_name" field.
func (_u *DisputeTimelineUpdateOne) SetActorName(v string) *DisputeTimelineUpdateOne {
	_u.mutation.SetActorName(v)
	return _u
}

// SetNillableActorName sets the "actor_name" field if the given value is not nil.
func (_u *DisputeTimelineUpdateOne) SetNillableActorName(v *string) *DisputeTimelineUpdateOne {
	if v != nil {
		_u.SetActorName(*v)
	}
	return _u
}

// ClearActorName clears the value of the "actor_name" field.
func (_u *DisputeTimelineUpdateOne) ClearActorName() *DisputeTimelineUpdateOne {
	_u.mutation.ClearActorName()
	return _u
}

// SetDetails sets the "details" field.
func (_u *DisputeTimelineUpdateOne) SetDetails(v map[string]interface{}) *DisputeTimelineUpdateOne {
	_u.mutation.SetDetails(v)
	return _u
}

// ClearDetails clears the value of the "details" field.
func (_u *DisputeTimelineUpdateOne) ClearDetails() *DisputeTimelineUpdateOne {
	_u.mutation.ClearDetails()
	return _u
}

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

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

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *DisputeTimelineUpdateOne) 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 *DisputeTimelineUpdateOne) check() error {
	if v, ok := _u.mutation.EventType(); ok {
		if err := disputetimeline.EventTypeValidator(v); err != nil {
			return &ValidationError{Name: "event_type", err: fmt.Errorf(`models: validator failed for field "DisputeTimeline.event_type": %w`, err)}
		}
	}
	if v, ok := _u.mutation.ActorName(); ok {
		if err := disputetimeline.ActorNameValidator(v); err != nil {
			return &ValidationError{Name: "actor_name", err: fmt.Errorf(`models: validator failed for field "DisputeTimeline.actor_name": %w`, err)}
		}
	}
	return nil
}

func (_u *DisputeTimelineUpdateOne) sqlSave(ctx context.Context) (_node *DisputeTimeline, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(disputetimeline.Table, disputetimeline.Columns, sqlgraph.NewFieldSpec(disputetimeline.FieldID, field.TypeInt64))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`models: missing "DisputeTimeline.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, disputetimeline.FieldID)
		for _, f := range fields {
			if !disputetimeline.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("models: invalid field %q for query", f)}
			}
			if f != disputetimeline.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.DisputeID(); ok {
		_spec.SetField(disputetimeline.FieldDisputeID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedDisputeID(); ok {
		_spec.AddField(disputetimeline.FieldDisputeID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.EventType(); ok {
		_spec.SetField(disputetimeline.FieldEventType, field.TypeString, value)
	}
	if value, ok := _u.mutation.ActorID(); ok {
		_spec.SetField(disputetimeline.FieldActorID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedActorID(); ok {
		_spec.AddField(disputetimeline.FieldActorID, field.TypeInt64, value)
	}
	if _u.mutation.ActorIDCleared() {
		_spec.ClearField(disputetimeline.FieldActorID, field.TypeInt64)
	}
	if value, ok := _u.mutation.ActorName(); ok {
		_spec.SetField(disputetimeline.FieldActorName, field.TypeString, value)
	}
	if _u.mutation.ActorNameCleared() {
		_spec.ClearField(disputetimeline.FieldActorName, field.TypeString)
	}
	if value, ok := _u.mutation.Details(); ok {
		_spec.SetField(disputetimeline.FieldDetails, field.TypeJSON, value)
	}
	if _u.mutation.DetailsCleared() {
		_spec.ClearField(disputetimeline.FieldDetails, field.TypeJSON)
	}
	_node = &DisputeTimeline{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{disputetimeline.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
