// 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/dialect/sql/sqljson"
	"entgo.io/ent/schema/field"
	"github.com/AvengeMedia/dankcalendar/core/ent/calendar"
	"github.com/AvengeMedia/dankcalendar/core/ent/predicate"
	"github.com/AvengeMedia/dankcalendar/core/ent/task"
)

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

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

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

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

// SetRemoteID sets the "remote_id" field.
func (_u *TaskUpdate) SetRemoteID(v string) *TaskUpdate {
	_u.mutation.SetRemoteID(v)
	return _u
}

// SetNillableRemoteID sets the "remote_id" field if the given value is not nil.
func (_u *TaskUpdate) SetNillableRemoteID(v *string) *TaskUpdate {
	if v != nil {
		_u.SetRemoteID(*v)
	}
	return _u
}

// ClearRemoteID clears the value of the "remote_id" field.
func (_u *TaskUpdate) ClearRemoteID() *TaskUpdate {
	_u.mutation.ClearRemoteID()
	return _u
}

// SetEtag sets the "etag" field.
func (_u *TaskUpdate) SetEtag(v string) *TaskUpdate {
	_u.mutation.SetEtag(v)
	return _u
}

// SetNillableEtag sets the "etag" field if the given value is not nil.
func (_u *TaskUpdate) SetNillableEtag(v *string) *TaskUpdate {
	if v != nil {
		_u.SetEtag(*v)
	}
	return _u
}

// ClearEtag clears the value of the "etag" field.
func (_u *TaskUpdate) ClearEtag() *TaskUpdate {
	_u.mutation.ClearEtag()
	return _u
}

// SetSummary sets the "summary" field.
func (_u *TaskUpdate) SetSummary(v string) *TaskUpdate {
	_u.mutation.SetSummary(v)
	return _u
}

// SetNillableSummary sets the "summary" field if the given value is not nil.
func (_u *TaskUpdate) SetNillableSummary(v *string) *TaskUpdate {
	if v != nil {
		_u.SetSummary(*v)
	}
	return _u
}

// SetDescription sets the "description" field.
func (_u *TaskUpdate) SetDescription(v string) *TaskUpdate {
	_u.mutation.SetDescription(v)
	return _u
}

// SetNillableDescription sets the "description" field if the given value is not nil.
func (_u *TaskUpdate) SetNillableDescription(v *string) *TaskUpdate {
	if v != nil {
		_u.SetDescription(*v)
	}
	return _u
}

// ClearDescription clears the value of the "description" field.
func (_u *TaskUpdate) ClearDescription() *TaskUpdate {
	_u.mutation.ClearDescription()
	return _u
}

// SetLocation sets the "location" field.
func (_u *TaskUpdate) SetLocation(v string) *TaskUpdate {
	_u.mutation.SetLocation(v)
	return _u
}

// SetNillableLocation sets the "location" field if the given value is not nil.
func (_u *TaskUpdate) SetNillableLocation(v *string) *TaskUpdate {
	if v != nil {
		_u.SetLocation(*v)
	}
	return _u
}

// ClearLocation clears the value of the "location" field.
func (_u *TaskUpdate) ClearLocation() *TaskUpdate {
	_u.mutation.ClearLocation()
	return _u
}

// SetStatus sets the "status" field.
func (_u *TaskUpdate) SetStatus(v task.Status) *TaskUpdate {
	_u.mutation.SetStatus(v)
	return _u
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (_u *TaskUpdate) SetNillableStatus(v *task.Status) *TaskUpdate {
	if v != nil {
		_u.SetStatus(*v)
	}
	return _u
}

// SetPriority sets the "priority" field.
func (_u *TaskUpdate) SetPriority(v int) *TaskUpdate {
	_u.mutation.ResetPriority()
	_u.mutation.SetPriority(v)
	return _u
}

// SetNillablePriority sets the "priority" field if the given value is not nil.
func (_u *TaskUpdate) SetNillablePriority(v *int) *TaskUpdate {
	if v != nil {
		_u.SetPriority(*v)
	}
	return _u
}

// AddPriority adds value to the "priority" field.
func (_u *TaskUpdate) AddPriority(v int) *TaskUpdate {
	_u.mutation.AddPriority(v)
	return _u
}

// SetPercentComplete sets the "percent_complete" field.
func (_u *TaskUpdate) SetPercentComplete(v int) *TaskUpdate {
	_u.mutation.ResetPercentComplete()
	_u.mutation.SetPercentComplete(v)
	return _u
}

// SetNillablePercentComplete sets the "percent_complete" field if the given value is not nil.
func (_u *TaskUpdate) SetNillablePercentComplete(v *int) *TaskUpdate {
	if v != nil {
		_u.SetPercentComplete(*v)
	}
	return _u
}

// AddPercentComplete adds value to the "percent_complete" field.
func (_u *TaskUpdate) AddPercentComplete(v int) *TaskUpdate {
	_u.mutation.AddPercentComplete(v)
	return _u
}

// SetDue sets the "due" field.
func (_u *TaskUpdate) SetDue(v time.Time) *TaskUpdate {
	_u.mutation.SetDue(v)
	return _u
}

// SetNillableDue sets the "due" field if the given value is not nil.
func (_u *TaskUpdate) SetNillableDue(v *time.Time) *TaskUpdate {
	if v != nil {
		_u.SetDue(*v)
	}
	return _u
}

// ClearDue clears the value of the "due" field.
func (_u *TaskUpdate) ClearDue() *TaskUpdate {
	_u.mutation.ClearDue()
	return _u
}

// SetStart sets the "start" field.
func (_u *TaskUpdate) SetStart(v time.Time) *TaskUpdate {
	_u.mutation.SetStart(v)
	return _u
}

// SetNillableStart sets the "start" field if the given value is not nil.
func (_u *TaskUpdate) SetNillableStart(v *time.Time) *TaskUpdate {
	if v != nil {
		_u.SetStart(*v)
	}
	return _u
}

// ClearStart clears the value of the "start" field.
func (_u *TaskUpdate) ClearStart() *TaskUpdate {
	_u.mutation.ClearStart()
	return _u
}

// SetCompleted sets the "completed" field.
func (_u *TaskUpdate) SetCompleted(v time.Time) *TaskUpdate {
	_u.mutation.SetCompleted(v)
	return _u
}

// SetNillableCompleted sets the "completed" field if the given value is not nil.
func (_u *TaskUpdate) SetNillableCompleted(v *time.Time) *TaskUpdate {
	if v != nil {
		_u.SetCompleted(*v)
	}
	return _u
}

// ClearCompleted clears the value of the "completed" field.
func (_u *TaskUpdate) ClearCompleted() *TaskUpdate {
	_u.mutation.ClearCompleted()
	return _u
}

// SetAllDay sets the "all_day" field.
func (_u *TaskUpdate) SetAllDay(v bool) *TaskUpdate {
	_u.mutation.SetAllDay(v)
	return _u
}

// SetNillableAllDay sets the "all_day" field if the given value is not nil.
func (_u *TaskUpdate) SetNillableAllDay(v *bool) *TaskUpdate {
	if v != nil {
		_u.SetAllDay(*v)
	}
	return _u
}

// SetDueTz sets the "due_tz" field.
func (_u *TaskUpdate) SetDueTz(v string) *TaskUpdate {
	_u.mutation.SetDueTz(v)
	return _u
}

// SetNillableDueTz sets the "due_tz" field if the given value is not nil.
func (_u *TaskUpdate) SetNillableDueTz(v *string) *TaskUpdate {
	if v != nil {
		_u.SetDueTz(*v)
	}
	return _u
}

// ClearDueTz clears the value of the "due_tz" field.
func (_u *TaskUpdate) ClearDueTz() *TaskUpdate {
	_u.mutation.ClearDueTz()
	return _u
}

// SetStartTz sets the "start_tz" field.
func (_u *TaskUpdate) SetStartTz(v string) *TaskUpdate {
	_u.mutation.SetStartTz(v)
	return _u
}

// SetNillableStartTz sets the "start_tz" field if the given value is not nil.
func (_u *TaskUpdate) SetNillableStartTz(v *string) *TaskUpdate {
	if v != nil {
		_u.SetStartTz(*v)
	}
	return _u
}

// ClearStartTz clears the value of the "start_tz" field.
func (_u *TaskUpdate) ClearStartTz() *TaskUpdate {
	_u.mutation.ClearStartTz()
	return _u
}

// SetParentUID sets the "parent_uid" field.
func (_u *TaskUpdate) SetParentUID(v string) *TaskUpdate {
	_u.mutation.SetParentUID(v)
	return _u
}

// SetNillableParentUID sets the "parent_uid" field if the given value is not nil.
func (_u *TaskUpdate) SetNillableParentUID(v *string) *TaskUpdate {
	if v != nil {
		_u.SetParentUID(*v)
	}
	return _u
}

// ClearParentUID clears the value of the "parent_uid" field.
func (_u *TaskUpdate) ClearParentUID() *TaskUpdate {
	_u.mutation.ClearParentUID()
	return _u
}

// SetRecurrence sets the "recurrence" field.
func (_u *TaskUpdate) SetRecurrence(v map[string]interface{}) *TaskUpdate {
	_u.mutation.SetRecurrence(v)
	return _u
}

// ClearRecurrence clears the value of the "recurrence" field.
func (_u *TaskUpdate) ClearRecurrence() *TaskUpdate {
	_u.mutation.ClearRecurrence()
	return _u
}

// SetReminders sets the "reminders" field.
func (_u *TaskUpdate) SetReminders(v []map[string]interface{}) *TaskUpdate {
	_u.mutation.SetReminders(v)
	return _u
}

// AppendReminders appends value to the "reminders" field.
func (_u *TaskUpdate) AppendReminders(v []map[string]interface{}) *TaskUpdate {
	_u.mutation.AppendReminders(v)
	return _u
}

// ClearReminders clears the value of the "reminders" field.
func (_u *TaskUpdate) ClearReminders() *TaskUpdate {
	_u.mutation.ClearReminders()
	return _u
}

// SetCategories sets the "categories" field.
func (_u *TaskUpdate) SetCategories(v []string) *TaskUpdate {
	_u.mutation.SetCategories(v)
	return _u
}

// AppendCategories appends value to the "categories" field.
func (_u *TaskUpdate) AppendCategories(v []string) *TaskUpdate {
	_u.mutation.AppendCategories(v)
	return _u
}

// ClearCategories clears the value of the "categories" field.
func (_u *TaskUpdate) ClearCategories() *TaskUpdate {
	_u.mutation.ClearCategories()
	return _u
}

// SetRawIcs sets the "raw_ics" field.
func (_u *TaskUpdate) SetRawIcs(v string) *TaskUpdate {
	_u.mutation.SetRawIcs(v)
	return _u
}

// SetNillableRawIcs sets the "raw_ics" field if the given value is not nil.
func (_u *TaskUpdate) SetNillableRawIcs(v *string) *TaskUpdate {
	if v != nil {
		_u.SetRawIcs(*v)
	}
	return _u
}

// ClearRawIcs clears the value of the "raw_ics" field.
func (_u *TaskUpdate) ClearRawIcs() *TaskUpdate {
	_u.mutation.ClearRawIcs()
	return _u
}

// SetCreated sets the "created" field.
func (_u *TaskUpdate) SetCreated(v time.Time) *TaskUpdate {
	_u.mutation.SetCreated(v)
	return _u
}

// SetNillableCreated sets the "created" field if the given value is not nil.
func (_u *TaskUpdate) SetNillableCreated(v *time.Time) *TaskUpdate {
	if v != nil {
		_u.SetCreated(*v)
	}
	return _u
}

// SetUpdated sets the "updated" field.
func (_u *TaskUpdate) SetUpdated(v time.Time) *TaskUpdate {
	_u.mutation.SetUpdated(v)
	return _u
}

// SetCalendarID sets the "calendar" edge to the Calendar entity by ID.
func (_u *TaskUpdate) SetCalendarID(id string) *TaskUpdate {
	_u.mutation.SetCalendarID(id)
	return _u
}

// SetCalendar sets the "calendar" edge to the Calendar entity.
func (_u *TaskUpdate) SetCalendar(v *Calendar) *TaskUpdate {
	return _u.SetCalendarID(v.ID)
}

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

// ClearCalendar clears the "calendar" edge to the Calendar entity.
func (_u *TaskUpdate) ClearCalendar() *TaskUpdate {
	_u.mutation.ClearCalendar()
	return _u
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (_u *TaskUpdate) Save(ctx context.Context) (int, error) {
	_u.defaults()
	return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *TaskUpdate) ExecX(ctx context.Context) {
	if err := _u.Exec(ctx); err != nil {
		panic(err)
	}
}

// defaults sets the default values of the builder before save.
func (_u *TaskUpdate) defaults() {
	if _, ok := _u.mutation.Updated(); !ok {
		v := task.UpdateDefaultUpdated()
		_u.mutation.SetUpdated(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_u *TaskUpdate) check() error {
	if v, ok := _u.mutation.UID(); ok {
		if err := task.UIDValidator(v); err != nil {
			return &ValidationError{Name: "uid", err: fmt.Errorf(`ent: validator failed for field "Task.uid": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Status(); ok {
		if err := task.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "Task.status": %w`, err)}
		}
	}
	if _u.mutation.CalendarCleared() && len(_u.mutation.CalendarIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "Task.calendar"`)
	}
	return nil
}

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

func (_u *TaskUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(task.Table, task.Columns, sqlgraph.NewFieldSpec(task.FieldID, field.TypeString))
	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.UID(); ok {
		_spec.SetField(task.FieldUID, field.TypeString, value)
	}
	if value, ok := _u.mutation.RemoteID(); ok {
		_spec.SetField(task.FieldRemoteID, field.TypeString, value)
	}
	if _u.mutation.RemoteIDCleared() {
		_spec.ClearField(task.FieldRemoteID, field.TypeString)
	}
	if value, ok := _u.mutation.Etag(); ok {
		_spec.SetField(task.FieldEtag, field.TypeString, value)
	}
	if _u.mutation.EtagCleared() {
		_spec.ClearField(task.FieldEtag, field.TypeString)
	}
	if value, ok := _u.mutation.Summary(); ok {
		_spec.SetField(task.FieldSummary, field.TypeString, value)
	}
	if value, ok := _u.mutation.Description(); ok {
		_spec.SetField(task.FieldDescription, field.TypeString, value)
	}
	if _u.mutation.DescriptionCleared() {
		_spec.ClearField(task.FieldDescription, field.TypeString)
	}
	if value, ok := _u.mutation.Location(); ok {
		_spec.SetField(task.FieldLocation, field.TypeString, value)
	}
	if _u.mutation.LocationCleared() {
		_spec.ClearField(task.FieldLocation, field.TypeString)
	}
	if value, ok := _u.mutation.Status(); ok {
		_spec.SetField(task.FieldStatus, field.TypeEnum, value)
	}
	if value, ok := _u.mutation.Priority(); ok {
		_spec.SetField(task.FieldPriority, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedPriority(); ok {
		_spec.AddField(task.FieldPriority, field.TypeInt, value)
	}
	if value, ok := _u.mutation.PercentComplete(); ok {
		_spec.SetField(task.FieldPercentComplete, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedPercentComplete(); ok {
		_spec.AddField(task.FieldPercentComplete, field.TypeInt, value)
	}
	if value, ok := _u.mutation.Due(); ok {
		_spec.SetField(task.FieldDue, field.TypeTime, value)
	}
	if _u.mutation.DueCleared() {
		_spec.ClearField(task.FieldDue, field.TypeTime)
	}
	if value, ok := _u.mutation.Start(); ok {
		_spec.SetField(task.FieldStart, field.TypeTime, value)
	}
	if _u.mutation.StartCleared() {
		_spec.ClearField(task.FieldStart, field.TypeTime)
	}
	if value, ok := _u.mutation.Completed(); ok {
		_spec.SetField(task.FieldCompleted, field.TypeTime, value)
	}
	if _u.mutation.CompletedCleared() {
		_spec.ClearField(task.FieldCompleted, field.TypeTime)
	}
	if value, ok := _u.mutation.AllDay(); ok {
		_spec.SetField(task.FieldAllDay, field.TypeBool, value)
	}
	if value, ok := _u.mutation.DueTz(); ok {
		_spec.SetField(task.FieldDueTz, field.TypeString, value)
	}
	if _u.mutation.DueTzCleared() {
		_spec.ClearField(task.FieldDueTz, field.TypeString)
	}
	if value, ok := _u.mutation.StartTz(); ok {
		_spec.SetField(task.FieldStartTz, field.TypeString, value)
	}
	if _u.mutation.StartTzCleared() {
		_spec.ClearField(task.FieldStartTz, field.TypeString)
	}
	if value, ok := _u.mutation.ParentUID(); ok {
		_spec.SetField(task.FieldParentUID, field.TypeString, value)
	}
	if _u.mutation.ParentUIDCleared() {
		_spec.ClearField(task.FieldParentUID, field.TypeString)
	}
	if value, ok := _u.mutation.Recurrence(); ok {
		_spec.SetField(task.FieldRecurrence, field.TypeJSON, value)
	}
	if _u.mutation.RecurrenceCleared() {
		_spec.ClearField(task.FieldRecurrence, field.TypeJSON)
	}
	if value, ok := _u.mutation.Reminders(); ok {
		_spec.SetField(task.FieldReminders, field.TypeJSON, value)
	}
	if value, ok := _u.mutation.AppendedReminders(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, task.FieldReminders, value)
		})
	}
	if _u.mutation.RemindersCleared() {
		_spec.ClearField(task.FieldReminders, field.TypeJSON)
	}
	if value, ok := _u.mutation.Categories(); ok {
		_spec.SetField(task.FieldCategories, field.TypeJSON, value)
	}
	if value, ok := _u.mutation.AppendedCategories(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, task.FieldCategories, value)
		})
	}
	if _u.mutation.CategoriesCleared() {
		_spec.ClearField(task.FieldCategories, field.TypeJSON)
	}
	if value, ok := _u.mutation.RawIcs(); ok {
		_spec.SetField(task.FieldRawIcs, field.TypeString, value)
	}
	if _u.mutation.RawIcsCleared() {
		_spec.ClearField(task.FieldRawIcs, field.TypeString)
	}
	if value, ok := _u.mutation.Created(); ok {
		_spec.SetField(task.FieldCreated, field.TypeTime, value)
	}
	if value, ok := _u.mutation.Updated(); ok {
		_spec.SetField(task.FieldUpdated, field.TypeTime, value)
	}
	if _u.mutation.CalendarCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   task.CalendarTable,
			Columns: []string{task.CalendarColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(calendar.FieldID, field.TypeString),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.CalendarIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   task.CalendarTable,
			Columns: []string{task.CalendarColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(calendar.FieldID, field.TypeString),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_spec.AddModifiers(_u.modifiers...)
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{task.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

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

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

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

// SetRemoteID sets the "remote_id" field.
func (_u *TaskUpdateOne) SetRemoteID(v string) *TaskUpdateOne {
	_u.mutation.SetRemoteID(v)
	return _u
}

// SetNillableRemoteID sets the "remote_id" field if the given value is not nil.
func (_u *TaskUpdateOne) SetNillableRemoteID(v *string) *TaskUpdateOne {
	if v != nil {
		_u.SetRemoteID(*v)
	}
	return _u
}

// ClearRemoteID clears the value of the "remote_id" field.
func (_u *TaskUpdateOne) ClearRemoteID() *TaskUpdateOne {
	_u.mutation.ClearRemoteID()
	return _u
}

// SetEtag sets the "etag" field.
func (_u *TaskUpdateOne) SetEtag(v string) *TaskUpdateOne {
	_u.mutation.SetEtag(v)
	return _u
}

// SetNillableEtag sets the "etag" field if the given value is not nil.
func (_u *TaskUpdateOne) SetNillableEtag(v *string) *TaskUpdateOne {
	if v != nil {
		_u.SetEtag(*v)
	}
	return _u
}

// ClearEtag clears the value of the "etag" field.
func (_u *TaskUpdateOne) ClearEtag() *TaskUpdateOne {
	_u.mutation.ClearEtag()
	return _u
}

// SetSummary sets the "summary" field.
func (_u *TaskUpdateOne) SetSummary(v string) *TaskUpdateOne {
	_u.mutation.SetSummary(v)
	return _u
}

// SetNillableSummary sets the "summary" field if the given value is not nil.
func (_u *TaskUpdateOne) SetNillableSummary(v *string) *TaskUpdateOne {
	if v != nil {
		_u.SetSummary(*v)
	}
	return _u
}

// SetDescription sets the "description" field.
func (_u *TaskUpdateOne) SetDescription(v string) *TaskUpdateOne {
	_u.mutation.SetDescription(v)
	return _u
}

// SetNillableDescription sets the "description" field if the given value is not nil.
func (_u *TaskUpdateOne) SetNillableDescription(v *string) *TaskUpdateOne {
	if v != nil {
		_u.SetDescription(*v)
	}
	return _u
}

// ClearDescription clears the value of the "description" field.
func (_u *TaskUpdateOne) ClearDescription() *TaskUpdateOne {
	_u.mutation.ClearDescription()
	return _u
}

// SetLocation sets the "location" field.
func (_u *TaskUpdateOne) SetLocation(v string) *TaskUpdateOne {
	_u.mutation.SetLocation(v)
	return _u
}

// SetNillableLocation sets the "location" field if the given value is not nil.
func (_u *TaskUpdateOne) SetNillableLocation(v *string) *TaskUpdateOne {
	if v != nil {
		_u.SetLocation(*v)
	}
	return _u
}

// ClearLocation clears the value of the "location" field.
func (_u *TaskUpdateOne) ClearLocation() *TaskUpdateOne {
	_u.mutation.ClearLocation()
	return _u
}

// SetStatus sets the "status" field.
func (_u *TaskUpdateOne) SetStatus(v task.Status) *TaskUpdateOne {
	_u.mutation.SetStatus(v)
	return _u
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (_u *TaskUpdateOne) SetNillableStatus(v *task.Status) *TaskUpdateOne {
	if v != nil {
		_u.SetStatus(*v)
	}
	return _u
}

// SetPriority sets the "priority" field.
func (_u *TaskUpdateOne) SetPriority(v int) *TaskUpdateOne {
	_u.mutation.ResetPriority()
	_u.mutation.SetPriority(v)
	return _u
}

// SetNillablePriority sets the "priority" field if the given value is not nil.
func (_u *TaskUpdateOne) SetNillablePriority(v *int) *TaskUpdateOne {
	if v != nil {
		_u.SetPriority(*v)
	}
	return _u
}

// AddPriority adds value to the "priority" field.
func (_u *TaskUpdateOne) AddPriority(v int) *TaskUpdateOne {
	_u.mutation.AddPriority(v)
	return _u
}

// SetPercentComplete sets the "percent_complete" field.
func (_u *TaskUpdateOne) SetPercentComplete(v int) *TaskUpdateOne {
	_u.mutation.ResetPercentComplete()
	_u.mutation.SetPercentComplete(v)
	return _u
}

// SetNillablePercentComplete sets the "percent_complete" field if the given value is not nil.
func (_u *TaskUpdateOne) SetNillablePercentComplete(v *int) *TaskUpdateOne {
	if v != nil {
		_u.SetPercentComplete(*v)
	}
	return _u
}

// AddPercentComplete adds value to the "percent_complete" field.
func (_u *TaskUpdateOne) AddPercentComplete(v int) *TaskUpdateOne {
	_u.mutation.AddPercentComplete(v)
	return _u
}

// SetDue sets the "due" field.
func (_u *TaskUpdateOne) SetDue(v time.Time) *TaskUpdateOne {
	_u.mutation.SetDue(v)
	return _u
}

// SetNillableDue sets the "due" field if the given value is not nil.
func (_u *TaskUpdateOne) SetNillableDue(v *time.Time) *TaskUpdateOne {
	if v != nil {
		_u.SetDue(*v)
	}
	return _u
}

// ClearDue clears the value of the "due" field.
func (_u *TaskUpdateOne) ClearDue() *TaskUpdateOne {
	_u.mutation.ClearDue()
	return _u
}

// SetStart sets the "start" field.
func (_u *TaskUpdateOne) SetStart(v time.Time) *TaskUpdateOne {
	_u.mutation.SetStart(v)
	return _u
}

// SetNillableStart sets the "start" field if the given value is not nil.
func (_u *TaskUpdateOne) SetNillableStart(v *time.Time) *TaskUpdateOne {
	if v != nil {
		_u.SetStart(*v)
	}
	return _u
}

// ClearStart clears the value of the "start" field.
func (_u *TaskUpdateOne) ClearStart() *TaskUpdateOne {
	_u.mutation.ClearStart()
	return _u
}

// SetCompleted sets the "completed" field.
func (_u *TaskUpdateOne) SetCompleted(v time.Time) *TaskUpdateOne {
	_u.mutation.SetCompleted(v)
	return _u
}

// SetNillableCompleted sets the "completed" field if the given value is not nil.
func (_u *TaskUpdateOne) SetNillableCompleted(v *time.Time) *TaskUpdateOne {
	if v != nil {
		_u.SetCompleted(*v)
	}
	return _u
}

// ClearCompleted clears the value of the "completed" field.
func (_u *TaskUpdateOne) ClearCompleted() *TaskUpdateOne {
	_u.mutation.ClearCompleted()
	return _u
}

// SetAllDay sets the "all_day" field.
func (_u *TaskUpdateOne) SetAllDay(v bool) *TaskUpdateOne {
	_u.mutation.SetAllDay(v)
	return _u
}

// SetNillableAllDay sets the "all_day" field if the given value is not nil.
func (_u *TaskUpdateOne) SetNillableAllDay(v *bool) *TaskUpdateOne {
	if v != nil {
		_u.SetAllDay(*v)
	}
	return _u
}

// SetDueTz sets the "due_tz" field.
func (_u *TaskUpdateOne) SetDueTz(v string) *TaskUpdateOne {
	_u.mutation.SetDueTz(v)
	return _u
}

// SetNillableDueTz sets the "due_tz" field if the given value is not nil.
func (_u *TaskUpdateOne) SetNillableDueTz(v *string) *TaskUpdateOne {
	if v != nil {
		_u.SetDueTz(*v)
	}
	return _u
}

// ClearDueTz clears the value of the "due_tz" field.
func (_u *TaskUpdateOne) ClearDueTz() *TaskUpdateOne {
	_u.mutation.ClearDueTz()
	return _u
}

// SetStartTz sets the "start_tz" field.
func (_u *TaskUpdateOne) SetStartTz(v string) *TaskUpdateOne {
	_u.mutation.SetStartTz(v)
	return _u
}

// SetNillableStartTz sets the "start_tz" field if the given value is not nil.
func (_u *TaskUpdateOne) SetNillableStartTz(v *string) *TaskUpdateOne {
	if v != nil {
		_u.SetStartTz(*v)
	}
	return _u
}

// ClearStartTz clears the value of the "start_tz" field.
func (_u *TaskUpdateOne) ClearStartTz() *TaskUpdateOne {
	_u.mutation.ClearStartTz()
	return _u
}

// SetParentUID sets the "parent_uid" field.
func (_u *TaskUpdateOne) SetParentUID(v string) *TaskUpdateOne {
	_u.mutation.SetParentUID(v)
	return _u
}

// SetNillableParentUID sets the "parent_uid" field if the given value is not nil.
func (_u *TaskUpdateOne) SetNillableParentUID(v *string) *TaskUpdateOne {
	if v != nil {
		_u.SetParentUID(*v)
	}
	return _u
}

// ClearParentUID clears the value of the "parent_uid" field.
func (_u *TaskUpdateOne) ClearParentUID() *TaskUpdateOne {
	_u.mutation.ClearParentUID()
	return _u
}

// SetRecurrence sets the "recurrence" field.
func (_u *TaskUpdateOne) SetRecurrence(v map[string]interface{}) *TaskUpdateOne {
	_u.mutation.SetRecurrence(v)
	return _u
}

// ClearRecurrence clears the value of the "recurrence" field.
func (_u *TaskUpdateOne) ClearRecurrence() *TaskUpdateOne {
	_u.mutation.ClearRecurrence()
	return _u
}

// SetReminders sets the "reminders" field.
func (_u *TaskUpdateOne) SetReminders(v []map[string]interface{}) *TaskUpdateOne {
	_u.mutation.SetReminders(v)
	return _u
}

// AppendReminders appends value to the "reminders" field.
func (_u *TaskUpdateOne) AppendReminders(v []map[string]interface{}) *TaskUpdateOne {
	_u.mutation.AppendReminders(v)
	return _u
}

// ClearReminders clears the value of the "reminders" field.
func (_u *TaskUpdateOne) ClearReminders() *TaskUpdateOne {
	_u.mutation.ClearReminders()
	return _u
}

// SetCategories sets the "categories" field.
func (_u *TaskUpdateOne) SetCategories(v []string) *TaskUpdateOne {
	_u.mutation.SetCategories(v)
	return _u
}

// AppendCategories appends value to the "categories" field.
func (_u *TaskUpdateOne) AppendCategories(v []string) *TaskUpdateOne {
	_u.mutation.AppendCategories(v)
	return _u
}

// ClearCategories clears the value of the "categories" field.
func (_u *TaskUpdateOne) ClearCategories() *TaskUpdateOne {
	_u.mutation.ClearCategories()
	return _u
}

// SetRawIcs sets the "raw_ics" field.
func (_u *TaskUpdateOne) SetRawIcs(v string) *TaskUpdateOne {
	_u.mutation.SetRawIcs(v)
	return _u
}

// SetNillableRawIcs sets the "raw_ics" field if the given value is not nil.
func (_u *TaskUpdateOne) SetNillableRawIcs(v *string) *TaskUpdateOne {
	if v != nil {
		_u.SetRawIcs(*v)
	}
	return _u
}

// ClearRawIcs clears the value of the "raw_ics" field.
func (_u *TaskUpdateOne) ClearRawIcs() *TaskUpdateOne {
	_u.mutation.ClearRawIcs()
	return _u
}

// SetCreated sets the "created" field.
func (_u *TaskUpdateOne) SetCreated(v time.Time) *TaskUpdateOne {
	_u.mutation.SetCreated(v)
	return _u
}

// SetNillableCreated sets the "created" field if the given value is not nil.
func (_u *TaskUpdateOne) SetNillableCreated(v *time.Time) *TaskUpdateOne {
	if v != nil {
		_u.SetCreated(*v)
	}
	return _u
}

// SetUpdated sets the "updated" field.
func (_u *TaskUpdateOne) SetUpdated(v time.Time) *TaskUpdateOne {
	_u.mutation.SetUpdated(v)
	return _u
}

// SetCalendarID sets the "calendar" edge to the Calendar entity by ID.
func (_u *TaskUpdateOne) SetCalendarID(id string) *TaskUpdateOne {
	_u.mutation.SetCalendarID(id)
	return _u
}

// SetCalendar sets the "calendar" edge to the Calendar entity.
func (_u *TaskUpdateOne) SetCalendar(v *Calendar) *TaskUpdateOne {
	return _u.SetCalendarID(v.ID)
}

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

// ClearCalendar clears the "calendar" edge to the Calendar entity.
func (_u *TaskUpdateOne) ClearCalendar() *TaskUpdateOne {
	_u.mutation.ClearCalendar()
	return _u
}

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

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *TaskUpdateOne) ExecX(ctx context.Context) {
	if err := _u.Exec(ctx); err != nil {
		panic(err)
	}
}

// defaults sets the default values of the builder before save.
func (_u *TaskUpdateOne) defaults() {
	if _, ok := _u.mutation.Updated(); !ok {
		v := task.UpdateDefaultUpdated()
		_u.mutation.SetUpdated(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_u *TaskUpdateOne) check() error {
	if v, ok := _u.mutation.UID(); ok {
		if err := task.UIDValidator(v); err != nil {
			return &ValidationError{Name: "uid", err: fmt.Errorf(`ent: validator failed for field "Task.uid": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Status(); ok {
		if err := task.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "Task.status": %w`, err)}
		}
	}
	if _u.mutation.CalendarCleared() && len(_u.mutation.CalendarIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "Task.calendar"`)
	}
	return nil
}

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

func (_u *TaskUpdateOne) sqlSave(ctx context.Context) (_node *Task, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(task.Table, task.Columns, sqlgraph.NewFieldSpec(task.FieldID, field.TypeString))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Task.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, task.FieldID)
		for _, f := range fields {
			if !task.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != task.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.UID(); ok {
		_spec.SetField(task.FieldUID, field.TypeString, value)
	}
	if value, ok := _u.mutation.RemoteID(); ok {
		_spec.SetField(task.FieldRemoteID, field.TypeString, value)
	}
	if _u.mutation.RemoteIDCleared() {
		_spec.ClearField(task.FieldRemoteID, field.TypeString)
	}
	if value, ok := _u.mutation.Etag(); ok {
		_spec.SetField(task.FieldEtag, field.TypeString, value)
	}
	if _u.mutation.EtagCleared() {
		_spec.ClearField(task.FieldEtag, field.TypeString)
	}
	if value, ok := _u.mutation.Summary(); ok {
		_spec.SetField(task.FieldSummary, field.TypeString, value)
	}
	if value, ok := _u.mutation.Description(); ok {
		_spec.SetField(task.FieldDescription, field.TypeString, value)
	}
	if _u.mutation.DescriptionCleared() {
		_spec.ClearField(task.FieldDescription, field.TypeString)
	}
	if value, ok := _u.mutation.Location(); ok {
		_spec.SetField(task.FieldLocation, field.TypeString, value)
	}
	if _u.mutation.LocationCleared() {
		_spec.ClearField(task.FieldLocation, field.TypeString)
	}
	if value, ok := _u.mutation.Status(); ok {
		_spec.SetField(task.FieldStatus, field.TypeEnum, value)
	}
	if value, ok := _u.mutation.Priority(); ok {
		_spec.SetField(task.FieldPriority, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedPriority(); ok {
		_spec.AddField(task.FieldPriority, field.TypeInt, value)
	}
	if value, ok := _u.mutation.PercentComplete(); ok {
		_spec.SetField(task.FieldPercentComplete, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedPercentComplete(); ok {
		_spec.AddField(task.FieldPercentComplete, field.TypeInt, value)
	}
	if value, ok := _u.mutation.Due(); ok {
		_spec.SetField(task.FieldDue, field.TypeTime, value)
	}
	if _u.mutation.DueCleared() {
		_spec.ClearField(task.FieldDue, field.TypeTime)
	}
	if value, ok := _u.mutation.Start(); ok {
		_spec.SetField(task.FieldStart, field.TypeTime, value)
	}
	if _u.mutation.StartCleared() {
		_spec.ClearField(task.FieldStart, field.TypeTime)
	}
	if value, ok := _u.mutation.Completed(); ok {
		_spec.SetField(task.FieldCompleted, field.TypeTime, value)
	}
	if _u.mutation.CompletedCleared() {
		_spec.ClearField(task.FieldCompleted, field.TypeTime)
	}
	if value, ok := _u.mutation.AllDay(); ok {
		_spec.SetField(task.FieldAllDay, field.TypeBool, value)
	}
	if value, ok := _u.mutation.DueTz(); ok {
		_spec.SetField(task.FieldDueTz, field.TypeString, value)
	}
	if _u.mutation.DueTzCleared() {
		_spec.ClearField(task.FieldDueTz, field.TypeString)
	}
	if value, ok := _u.mutation.StartTz(); ok {
		_spec.SetField(task.FieldStartTz, field.TypeString, value)
	}
	if _u.mutation.StartTzCleared() {
		_spec.ClearField(task.FieldStartTz, field.TypeString)
	}
	if value, ok := _u.mutation.ParentUID(); ok {
		_spec.SetField(task.FieldParentUID, field.TypeString, value)
	}
	if _u.mutation.ParentUIDCleared() {
		_spec.ClearField(task.FieldParentUID, field.TypeString)
	}
	if value, ok := _u.mutation.Recurrence(); ok {
		_spec.SetField(task.FieldRecurrence, field.TypeJSON, value)
	}
	if _u.mutation.RecurrenceCleared() {
		_spec.ClearField(task.FieldRecurrence, field.TypeJSON)
	}
	if value, ok := _u.mutation.Reminders(); ok {
		_spec.SetField(task.FieldReminders, field.TypeJSON, value)
	}
	if value, ok := _u.mutation.AppendedReminders(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, task.FieldReminders, value)
		})
	}
	if _u.mutation.RemindersCleared() {
		_spec.ClearField(task.FieldReminders, field.TypeJSON)
	}
	if value, ok := _u.mutation.Categories(); ok {
		_spec.SetField(task.FieldCategories, field.TypeJSON, value)
	}
	if value, ok := _u.mutation.AppendedCategories(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, task.FieldCategories, value)
		})
	}
	if _u.mutation.CategoriesCleared() {
		_spec.ClearField(task.FieldCategories, field.TypeJSON)
	}
	if value, ok := _u.mutation.RawIcs(); ok {
		_spec.SetField(task.FieldRawIcs, field.TypeString, value)
	}
	if _u.mutation.RawIcsCleared() {
		_spec.ClearField(task.FieldRawIcs, field.TypeString)
	}
	if value, ok := _u.mutation.Created(); ok {
		_spec.SetField(task.FieldCreated, field.TypeTime, value)
	}
	if value, ok := _u.mutation.Updated(); ok {
		_spec.SetField(task.FieldUpdated, field.TypeTime, value)
	}
	if _u.mutation.CalendarCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   task.CalendarTable,
			Columns: []string{task.CalendarColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(calendar.FieldID, field.TypeString),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.CalendarIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   task.CalendarTable,
			Columns: []string{task.CalendarColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(calendar.FieldID, field.TypeString),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_spec.AddModifiers(_u.modifiers...)
	_node = &Task{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{task.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
