// 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/AvengeMedia/dankcalendar/core/ent/predicate"
	"github.com/AvengeMedia/dankcalendar/core/ent/reminderstate"
)

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

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

// SetCalendarID sets the "calendar_id" field.
func (_u *ReminderStateUpdate) SetCalendarID(v string) *ReminderStateUpdate {
	_u.mutation.SetCalendarID(v)
	return _u
}

// SetNillableCalendarID sets the "calendar_id" field if the given value is not nil.
func (_u *ReminderStateUpdate) SetNillableCalendarID(v *string) *ReminderStateUpdate {
	if v != nil {
		_u.SetCalendarID(*v)
	}
	return _u
}

// SetUID sets the "uid" field.
func (_u *ReminderStateUpdate) SetUID(v string) *ReminderStateUpdate {
	_u.mutation.SetUID(v)
	return _u
}

// SetNillableUID sets the "uid" field if the given value is not nil.
func (_u *ReminderStateUpdate) SetNillableUID(v *string) *ReminderStateUpdate {
	if v != nil {
		_u.SetUID(*v)
	}
	return _u
}

// SetOccurrenceStart sets the "occurrence_start" field.
func (_u *ReminderStateUpdate) SetOccurrenceStart(v time.Time) *ReminderStateUpdate {
	_u.mutation.SetOccurrenceStart(v)
	return _u
}

// SetNillableOccurrenceStart sets the "occurrence_start" field if the given value is not nil.
func (_u *ReminderStateUpdate) SetNillableOccurrenceStart(v *time.Time) *ReminderStateUpdate {
	if v != nil {
		_u.SetOccurrenceStart(*v)
	}
	return _u
}

// SetMinutes sets the "minutes" field.
func (_u *ReminderStateUpdate) SetMinutes(v int) *ReminderStateUpdate {
	_u.mutation.ResetMinutes()
	_u.mutation.SetMinutes(v)
	return _u
}

// SetNillableMinutes sets the "minutes" field if the given value is not nil.
func (_u *ReminderStateUpdate) SetNillableMinutes(v *int) *ReminderStateUpdate {
	if v != nil {
		_u.SetMinutes(*v)
	}
	return _u
}

// AddMinutes adds value to the "minutes" field.
func (_u *ReminderStateUpdate) AddMinutes(v int) *ReminderStateUpdate {
	_u.mutation.AddMinutes(v)
	return _u
}

// SetFiredAt sets the "fired_at" field.
func (_u *ReminderStateUpdate) SetFiredAt(v time.Time) *ReminderStateUpdate {
	_u.mutation.SetFiredAt(v)
	return _u
}

// SetNillableFiredAt sets the "fired_at" field if the given value is not nil.
func (_u *ReminderStateUpdate) SetNillableFiredAt(v *time.Time) *ReminderStateUpdate {
	if v != nil {
		_u.SetFiredAt(*v)
	}
	return _u
}

// SetSnoozedUntil sets the "snoozed_until" field.
func (_u *ReminderStateUpdate) SetSnoozedUntil(v time.Time) *ReminderStateUpdate {
	_u.mutation.SetSnoozedUntil(v)
	return _u
}

// SetNillableSnoozedUntil sets the "snoozed_until" field if the given value is not nil.
func (_u *ReminderStateUpdate) SetNillableSnoozedUntil(v *time.Time) *ReminderStateUpdate {
	if v != nil {
		_u.SetSnoozedUntil(*v)
	}
	return _u
}

// ClearSnoozedUntil clears the value of the "snoozed_until" field.
func (_u *ReminderStateUpdate) ClearSnoozedUntil() *ReminderStateUpdate {
	_u.mutation.ClearSnoozedUntil()
	return _u
}

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *ReminderStateUpdate) 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 *ReminderStateUpdate) check() error {
	if v, ok := _u.mutation.CalendarID(); ok {
		if err := reminderstate.CalendarIDValidator(v); err != nil {
			return &ValidationError{Name: "calendar_id", err: fmt.Errorf(`ent: validator failed for field "ReminderState.calendar_id": %w`, err)}
		}
	}
	if v, ok := _u.mutation.UID(); ok {
		if err := reminderstate.UIDValidator(v); err != nil {
			return &ValidationError{Name: "uid", err: fmt.Errorf(`ent: validator failed for field "ReminderState.uid": %w`, err)}
		}
	}
	return nil
}

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

func (_u *ReminderStateUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(reminderstate.Table, reminderstate.Columns, sqlgraph.NewFieldSpec(reminderstate.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.CalendarID(); ok {
		_spec.SetField(reminderstate.FieldCalendarID, field.TypeString, value)
	}
	if value, ok := _u.mutation.UID(); ok {
		_spec.SetField(reminderstate.FieldUID, field.TypeString, value)
	}
	if value, ok := _u.mutation.OccurrenceStart(); ok {
		_spec.SetField(reminderstate.FieldOccurrenceStart, field.TypeTime, value)
	}
	if value, ok := _u.mutation.Minutes(); ok {
		_spec.SetField(reminderstate.FieldMinutes, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedMinutes(); ok {
		_spec.AddField(reminderstate.FieldMinutes, field.TypeInt, value)
	}
	if value, ok := _u.mutation.FiredAt(); ok {
		_spec.SetField(reminderstate.FieldFiredAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.SnoozedUntil(); ok {
		_spec.SetField(reminderstate.FieldSnoozedUntil, field.TypeTime, value)
	}
	if _u.mutation.SnoozedUntilCleared() {
		_spec.ClearField(reminderstate.FieldSnoozedUntil, field.TypeTime)
	}
	_spec.AddModifiers(_u.modifiers...)
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{reminderstate.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

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

// SetCalendarID sets the "calendar_id" field.
func (_u *ReminderStateUpdateOne) SetCalendarID(v string) *ReminderStateUpdateOne {
	_u.mutation.SetCalendarID(v)
	return _u
}

// SetNillableCalendarID sets the "calendar_id" field if the given value is not nil.
func (_u *ReminderStateUpdateOne) SetNillableCalendarID(v *string) *ReminderStateUpdateOne {
	if v != nil {
		_u.SetCalendarID(*v)
	}
	return _u
}

// SetUID sets the "uid" field.
func (_u *ReminderStateUpdateOne) SetUID(v string) *ReminderStateUpdateOne {
	_u.mutation.SetUID(v)
	return _u
}

// SetNillableUID sets the "uid" field if the given value is not nil.
func (_u *ReminderStateUpdateOne) SetNillableUID(v *string) *ReminderStateUpdateOne {
	if v != nil {
		_u.SetUID(*v)
	}
	return _u
}

// SetOccurrenceStart sets the "occurrence_start" field.
func (_u *ReminderStateUpdateOne) SetOccurrenceStart(v time.Time) *ReminderStateUpdateOne {
	_u.mutation.SetOccurrenceStart(v)
	return _u
}

// SetNillableOccurrenceStart sets the "occurrence_start" field if the given value is not nil.
func (_u *ReminderStateUpdateOne) SetNillableOccurrenceStart(v *time.Time) *ReminderStateUpdateOne {
	if v != nil {
		_u.SetOccurrenceStart(*v)
	}
	return _u
}

// SetMinutes sets the "minutes" field.
func (_u *ReminderStateUpdateOne) SetMinutes(v int) *ReminderStateUpdateOne {
	_u.mutation.ResetMinutes()
	_u.mutation.SetMinutes(v)
	return _u
}

// SetNillableMinutes sets the "minutes" field if the given value is not nil.
func (_u *ReminderStateUpdateOne) SetNillableMinutes(v *int) *ReminderStateUpdateOne {
	if v != nil {
		_u.SetMinutes(*v)
	}
	return _u
}

// AddMinutes adds value to the "minutes" field.
func (_u *ReminderStateUpdateOne) AddMinutes(v int) *ReminderStateUpdateOne {
	_u.mutation.AddMinutes(v)
	return _u
}

// SetFiredAt sets the "fired_at" field.
func (_u *ReminderStateUpdateOne) SetFiredAt(v time.Time) *ReminderStateUpdateOne {
	_u.mutation.SetFiredAt(v)
	return _u
}

// SetNillableFiredAt sets the "fired_at" field if the given value is not nil.
func (_u *ReminderStateUpdateOne) SetNillableFiredAt(v *time.Time) *ReminderStateUpdateOne {
	if v != nil {
		_u.SetFiredAt(*v)
	}
	return _u
}

// SetSnoozedUntil sets the "snoozed_until" field.
func (_u *ReminderStateUpdateOne) SetSnoozedUntil(v time.Time) *ReminderStateUpdateOne {
	_u.mutation.SetSnoozedUntil(v)
	return _u
}

// SetNillableSnoozedUntil sets the "snoozed_until" field if the given value is not nil.
func (_u *ReminderStateUpdateOne) SetNillableSnoozedUntil(v *time.Time) *ReminderStateUpdateOne {
	if v != nil {
		_u.SetSnoozedUntil(*v)
	}
	return _u
}

// ClearSnoozedUntil clears the value of the "snoozed_until" field.
func (_u *ReminderStateUpdateOne) ClearSnoozedUntil() *ReminderStateUpdateOne {
	_u.mutation.ClearSnoozedUntil()
	return _u
}

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

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

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *ReminderStateUpdateOne) 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 *ReminderStateUpdateOne) check() error {
	if v, ok := _u.mutation.CalendarID(); ok {
		if err := reminderstate.CalendarIDValidator(v); err != nil {
			return &ValidationError{Name: "calendar_id", err: fmt.Errorf(`ent: validator failed for field "ReminderState.calendar_id": %w`, err)}
		}
	}
	if v, ok := _u.mutation.UID(); ok {
		if err := reminderstate.UIDValidator(v); err != nil {
			return &ValidationError{Name: "uid", err: fmt.Errorf(`ent: validator failed for field "ReminderState.uid": %w`, err)}
		}
	}
	return nil
}

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

func (_u *ReminderStateUpdateOne) sqlSave(ctx context.Context) (_node *ReminderState, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(reminderstate.Table, reminderstate.Columns, sqlgraph.NewFieldSpec(reminderstate.FieldID, field.TypeInt))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "ReminderState.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, reminderstate.FieldID)
		for _, f := range fields {
			if !reminderstate.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != reminderstate.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.CalendarID(); ok {
		_spec.SetField(reminderstate.FieldCalendarID, field.TypeString, value)
	}
	if value, ok := _u.mutation.UID(); ok {
		_spec.SetField(reminderstate.FieldUID, field.TypeString, value)
	}
	if value, ok := _u.mutation.OccurrenceStart(); ok {
		_spec.SetField(reminderstate.FieldOccurrenceStart, field.TypeTime, value)
	}
	if value, ok := _u.mutation.Minutes(); ok {
		_spec.SetField(reminderstate.FieldMinutes, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedMinutes(); ok {
		_spec.AddField(reminderstate.FieldMinutes, field.TypeInt, value)
	}
	if value, ok := _u.mutation.FiredAt(); ok {
		_spec.SetField(reminderstate.FieldFiredAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.SnoozedUntil(); ok {
		_spec.SetField(reminderstate.FieldSnoozedUntil, field.TypeTime, value)
	}
	if _u.mutation.SnoozedUntilCleared() {
		_spec.ClearField(reminderstate.FieldSnoozedUntil, field.TypeTime)
	}
	_spec.AddModifiers(_u.modifiers...)
	_node = &ReminderState{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{reminderstate.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
