// 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/account"
	"github.com/AvengeMedia/dankcalendar/core/ent/calendar"
	"github.com/AvengeMedia/dankcalendar/core/ent/event"
	"github.com/AvengeMedia/dankcalendar/core/ent/predicate"
	"github.com/AvengeMedia/dankcalendar/core/ent/task"
	"github.com/AvengeMedia/dankcalendar/core/internal/settings"
)

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

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

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

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

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

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

// SetNameOverride sets the "name_override" field.
func (_u *CalendarUpdate) SetNameOverride(v string) *CalendarUpdate {
	_u.mutation.SetNameOverride(v)
	return _u
}

// SetNillableNameOverride sets the "name_override" field if the given value is not nil.
func (_u *CalendarUpdate) SetNillableNameOverride(v *string) *CalendarUpdate {
	if v != nil {
		_u.SetNameOverride(*v)
	}
	return _u
}

// ClearNameOverride clears the value of the "name_override" field.
func (_u *CalendarUpdate) ClearNameOverride() *CalendarUpdate {
	_u.mutation.ClearNameOverride()
	return _u
}

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

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

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

// SetColor sets the "color" field.
func (_u *CalendarUpdate) SetColor(v string) *CalendarUpdate {
	_u.mutation.SetColor(v)
	return _u
}

// SetNillableColor sets the "color" field if the given value is not nil.
func (_u *CalendarUpdate) SetNillableColor(v *string) *CalendarUpdate {
	if v != nil {
		_u.SetColor(*v)
	}
	return _u
}

// ClearColor clears the value of the "color" field.
func (_u *CalendarUpdate) ClearColor() *CalendarUpdate {
	_u.mutation.ClearColor()
	return _u
}

// SetTimeZone sets the "time_zone" field.
func (_u *CalendarUpdate) SetTimeZone(v string) *CalendarUpdate {
	_u.mutation.SetTimeZone(v)
	return _u
}

// SetNillableTimeZone sets the "time_zone" field if the given value is not nil.
func (_u *CalendarUpdate) SetNillableTimeZone(v *string) *CalendarUpdate {
	if v != nil {
		_u.SetTimeZone(*v)
	}
	return _u
}

// ClearTimeZone clears the value of the "time_zone" field.
func (_u *CalendarUpdate) ClearTimeZone() *CalendarUpdate {
	_u.mutation.ClearTimeZone()
	return _u
}

// SetReadOnly sets the "read_only" field.
func (_u *CalendarUpdate) SetReadOnly(v bool) *CalendarUpdate {
	_u.mutation.SetReadOnly(v)
	return _u
}

// SetNillableReadOnly sets the "read_only" field if the given value is not nil.
func (_u *CalendarUpdate) SetNillableReadOnly(v *bool) *CalendarUpdate {
	if v != nil {
		_u.SetReadOnly(*v)
	}
	return _u
}

// SetHidden sets the "hidden" field.
func (_u *CalendarUpdate) SetHidden(v bool) *CalendarUpdate {
	_u.mutation.SetHidden(v)
	return _u
}

// SetNillableHidden sets the "hidden" field if the given value is not nil.
func (_u *CalendarUpdate) SetNillableHidden(v *bool) *CalendarUpdate {
	if v != nil {
		_u.SetHidden(*v)
	}
	return _u
}

// SetReminderOverrides sets the "reminder_overrides" field.
func (_u *CalendarUpdate) SetReminderOverrides(v *settings.ReminderOverride) *CalendarUpdate {
	_u.mutation.SetReminderOverrides(v)
	return _u
}

// ClearReminderOverrides clears the value of the "reminder_overrides" field.
func (_u *CalendarUpdate) ClearReminderOverrides() *CalendarUpdate {
	_u.mutation.ClearReminderOverrides()
	return _u
}

// SetSyncToken sets the "sync_token" field.
func (_u *CalendarUpdate) SetSyncToken(v string) *CalendarUpdate {
	_u.mutation.SetSyncToken(v)
	return _u
}

// SetNillableSyncToken sets the "sync_token" field if the given value is not nil.
func (_u *CalendarUpdate) SetNillableSyncToken(v *string) *CalendarUpdate {
	if v != nil {
		_u.SetSyncToken(*v)
	}
	return _u
}

// ClearSyncToken clears the value of the "sync_token" field.
func (_u *CalendarUpdate) ClearSyncToken() *CalendarUpdate {
	_u.mutation.ClearSyncToken()
	return _u
}

// SetSupportedComponents sets the "supported_components" field.
func (_u *CalendarUpdate) SetSupportedComponents(v []string) *CalendarUpdate {
	_u.mutation.SetSupportedComponents(v)
	return _u
}

// AppendSupportedComponents appends value to the "supported_components" field.
func (_u *CalendarUpdate) AppendSupportedComponents(v []string) *CalendarUpdate {
	_u.mutation.AppendSupportedComponents(v)
	return _u
}

// ClearSupportedComponents clears the value of the "supported_components" field.
func (_u *CalendarUpdate) ClearSupportedComponents() *CalendarUpdate {
	_u.mutation.ClearSupportedComponents()
	return _u
}

// SetUpdatedAt sets the "updated_at" field.
func (_u *CalendarUpdate) SetUpdatedAt(v time.Time) *CalendarUpdate {
	_u.mutation.SetUpdatedAt(v)
	return _u
}

// SetAccountID sets the "account" edge to the Account entity by ID.
func (_u *CalendarUpdate) SetAccountID(id string) *CalendarUpdate {
	_u.mutation.SetAccountID(id)
	return _u
}

// SetAccount sets the "account" edge to the Account entity.
func (_u *CalendarUpdate) SetAccount(v *Account) *CalendarUpdate {
	return _u.SetAccountID(v.ID)
}

// AddEventIDs adds the "events" edge to the Event entity by IDs.
func (_u *CalendarUpdate) AddEventIDs(ids ...string) *CalendarUpdate {
	_u.mutation.AddEventIDs(ids...)
	return _u
}

// AddEvents adds the "events" edges to the Event entity.
func (_u *CalendarUpdate) AddEvents(v ...*Event) *CalendarUpdate {
	ids := make([]string, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddEventIDs(ids...)
}

// AddTaskIDs adds the "tasks" edge to the Task entity by IDs.
func (_u *CalendarUpdate) AddTaskIDs(ids ...string) *CalendarUpdate {
	_u.mutation.AddTaskIDs(ids...)
	return _u
}

// AddTasks adds the "tasks" edges to the Task entity.
func (_u *CalendarUpdate) AddTasks(v ...*Task) *CalendarUpdate {
	ids := make([]string, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddTaskIDs(ids...)
}

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

// ClearAccount clears the "account" edge to the Account entity.
func (_u *CalendarUpdate) ClearAccount() *CalendarUpdate {
	_u.mutation.ClearAccount()
	return _u
}

// ClearEvents clears all "events" edges to the Event entity.
func (_u *CalendarUpdate) ClearEvents() *CalendarUpdate {
	_u.mutation.ClearEvents()
	return _u
}

// RemoveEventIDs removes the "events" edge to Event entities by IDs.
func (_u *CalendarUpdate) RemoveEventIDs(ids ...string) *CalendarUpdate {
	_u.mutation.RemoveEventIDs(ids...)
	return _u
}

// RemoveEvents removes "events" edges to Event entities.
func (_u *CalendarUpdate) RemoveEvents(v ...*Event) *CalendarUpdate {
	ids := make([]string, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveEventIDs(ids...)
}

// ClearTasks clears all "tasks" edges to the Task entity.
func (_u *CalendarUpdate) ClearTasks() *CalendarUpdate {
	_u.mutation.ClearTasks()
	return _u
}

// RemoveTaskIDs removes the "tasks" edge to Task entities by IDs.
func (_u *CalendarUpdate) RemoveTaskIDs(ids ...string) *CalendarUpdate {
	_u.mutation.RemoveTaskIDs(ids...)
	return _u
}

// RemoveTasks removes "tasks" edges to Task entities.
func (_u *CalendarUpdate) RemoveTasks(v ...*Task) *CalendarUpdate {
	ids := make([]string, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveTaskIDs(ids...)
}

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *CalendarUpdate) 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 *CalendarUpdate) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := calendar.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_u *CalendarUpdate) check() error {
	if v, ok := _u.mutation.RemoteID(); ok {
		if err := calendar.RemoteIDValidator(v); err != nil {
			return &ValidationError{Name: "remote_id", err: fmt.Errorf(`ent: validator failed for field "Calendar.remote_id": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Name(); ok {
		if err := calendar.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Calendar.name": %w`, err)}
		}
	}
	if _u.mutation.AccountCleared() && len(_u.mutation.AccountIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "Calendar.account"`)
	}
	return nil
}

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

func (_u *CalendarUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(calendar.Table, calendar.Columns, sqlgraph.NewFieldSpec(calendar.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.RemoteID(); ok {
		_spec.SetField(calendar.FieldRemoteID, field.TypeString, value)
	}
	if value, ok := _u.mutation.Name(); ok {
		_spec.SetField(calendar.FieldName, field.TypeString, value)
	}
	if value, ok := _u.mutation.NameOverride(); ok {
		_spec.SetField(calendar.FieldNameOverride, field.TypeString, value)
	}
	if _u.mutation.NameOverrideCleared() {
		_spec.ClearField(calendar.FieldNameOverride, field.TypeString)
	}
	if value, ok := _u.mutation.Description(); ok {
		_spec.SetField(calendar.FieldDescription, field.TypeString, value)
	}
	if _u.mutation.DescriptionCleared() {
		_spec.ClearField(calendar.FieldDescription, field.TypeString)
	}
	if value, ok := _u.mutation.Color(); ok {
		_spec.SetField(calendar.FieldColor, field.TypeString, value)
	}
	if _u.mutation.ColorCleared() {
		_spec.ClearField(calendar.FieldColor, field.TypeString)
	}
	if value, ok := _u.mutation.TimeZone(); ok {
		_spec.SetField(calendar.FieldTimeZone, field.TypeString, value)
	}
	if _u.mutation.TimeZoneCleared() {
		_spec.ClearField(calendar.FieldTimeZone, field.TypeString)
	}
	if value, ok := _u.mutation.ReadOnly(); ok {
		_spec.SetField(calendar.FieldReadOnly, field.TypeBool, value)
	}
	if value, ok := _u.mutation.Hidden(); ok {
		_spec.SetField(calendar.FieldHidden, field.TypeBool, value)
	}
	if value, ok := _u.mutation.ReminderOverrides(); ok {
		_spec.SetField(calendar.FieldReminderOverrides, field.TypeJSON, value)
	}
	if _u.mutation.ReminderOverridesCleared() {
		_spec.ClearField(calendar.FieldReminderOverrides, field.TypeJSON)
	}
	if value, ok := _u.mutation.SyncToken(); ok {
		_spec.SetField(calendar.FieldSyncToken, field.TypeString, value)
	}
	if _u.mutation.SyncTokenCleared() {
		_spec.ClearField(calendar.FieldSyncToken, field.TypeString)
	}
	if value, ok := _u.mutation.SupportedComponents(); ok {
		_spec.SetField(calendar.FieldSupportedComponents, field.TypeJSON, value)
	}
	if value, ok := _u.mutation.AppendedSupportedComponents(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, calendar.FieldSupportedComponents, value)
		})
	}
	if _u.mutation.SupportedComponentsCleared() {
		_spec.ClearField(calendar.FieldSupportedComponents, field.TypeJSON)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(calendar.FieldUpdatedAt, field.TypeTime, value)
	}
	if _u.mutation.AccountCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   calendar.AccountTable,
			Columns: []string{calendar.AccountColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeString),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.AccountIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   calendar.AccountTable,
			Columns: []string{calendar.AccountColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeString),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.EventsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   calendar.EventsTable,
			Columns: []string{calendar.EventsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(event.FieldID, field.TypeString),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedEventsIDs(); len(nodes) > 0 && !_u.mutation.EventsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   calendar.EventsTable,
			Columns: []string{calendar.EventsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(event.FieldID, field.TypeString),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.EventsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   calendar.EventsTable,
			Columns: []string{calendar.EventsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(event.FieldID, field.TypeString),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.TasksCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   calendar.TasksTable,
			Columns: []string{calendar.TasksColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(task.FieldID, field.TypeString),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedTasksIDs(); len(nodes) > 0 && !_u.mutation.TasksCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   calendar.TasksTable,
			Columns: []string{calendar.TasksColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(task.FieldID, field.TypeString),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.TasksIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   calendar.TasksTable,
			Columns: []string{calendar.TasksColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(task.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{calendar.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

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

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

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

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

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

// SetNameOverride sets the "name_override" field.
func (_u *CalendarUpdateOne) SetNameOverride(v string) *CalendarUpdateOne {
	_u.mutation.SetNameOverride(v)
	return _u
}

// SetNillableNameOverride sets the "name_override" field if the given value is not nil.
func (_u *CalendarUpdateOne) SetNillableNameOverride(v *string) *CalendarUpdateOne {
	if v != nil {
		_u.SetNameOverride(*v)
	}
	return _u
}

// ClearNameOverride clears the value of the "name_override" field.
func (_u *CalendarUpdateOne) ClearNameOverride() *CalendarUpdateOne {
	_u.mutation.ClearNameOverride()
	return _u
}

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

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

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

// SetColor sets the "color" field.
func (_u *CalendarUpdateOne) SetColor(v string) *CalendarUpdateOne {
	_u.mutation.SetColor(v)
	return _u
}

// SetNillableColor sets the "color" field if the given value is not nil.
func (_u *CalendarUpdateOne) SetNillableColor(v *string) *CalendarUpdateOne {
	if v != nil {
		_u.SetColor(*v)
	}
	return _u
}

// ClearColor clears the value of the "color" field.
func (_u *CalendarUpdateOne) ClearColor() *CalendarUpdateOne {
	_u.mutation.ClearColor()
	return _u
}

// SetTimeZone sets the "time_zone" field.
func (_u *CalendarUpdateOne) SetTimeZone(v string) *CalendarUpdateOne {
	_u.mutation.SetTimeZone(v)
	return _u
}

// SetNillableTimeZone sets the "time_zone" field if the given value is not nil.
func (_u *CalendarUpdateOne) SetNillableTimeZone(v *string) *CalendarUpdateOne {
	if v != nil {
		_u.SetTimeZone(*v)
	}
	return _u
}

// ClearTimeZone clears the value of the "time_zone" field.
func (_u *CalendarUpdateOne) ClearTimeZone() *CalendarUpdateOne {
	_u.mutation.ClearTimeZone()
	return _u
}

// SetReadOnly sets the "read_only" field.
func (_u *CalendarUpdateOne) SetReadOnly(v bool) *CalendarUpdateOne {
	_u.mutation.SetReadOnly(v)
	return _u
}

// SetNillableReadOnly sets the "read_only" field if the given value is not nil.
func (_u *CalendarUpdateOne) SetNillableReadOnly(v *bool) *CalendarUpdateOne {
	if v != nil {
		_u.SetReadOnly(*v)
	}
	return _u
}

// SetHidden sets the "hidden" field.
func (_u *CalendarUpdateOne) SetHidden(v bool) *CalendarUpdateOne {
	_u.mutation.SetHidden(v)
	return _u
}

// SetNillableHidden sets the "hidden" field if the given value is not nil.
func (_u *CalendarUpdateOne) SetNillableHidden(v *bool) *CalendarUpdateOne {
	if v != nil {
		_u.SetHidden(*v)
	}
	return _u
}

// SetReminderOverrides sets the "reminder_overrides" field.
func (_u *CalendarUpdateOne) SetReminderOverrides(v *settings.ReminderOverride) *CalendarUpdateOne {
	_u.mutation.SetReminderOverrides(v)
	return _u
}

// ClearReminderOverrides clears the value of the "reminder_overrides" field.
func (_u *CalendarUpdateOne) ClearReminderOverrides() *CalendarUpdateOne {
	_u.mutation.ClearReminderOverrides()
	return _u
}

// SetSyncToken sets the "sync_token" field.
func (_u *CalendarUpdateOne) SetSyncToken(v string) *CalendarUpdateOne {
	_u.mutation.SetSyncToken(v)
	return _u
}

// SetNillableSyncToken sets the "sync_token" field if the given value is not nil.
func (_u *CalendarUpdateOne) SetNillableSyncToken(v *string) *CalendarUpdateOne {
	if v != nil {
		_u.SetSyncToken(*v)
	}
	return _u
}

// ClearSyncToken clears the value of the "sync_token" field.
func (_u *CalendarUpdateOne) ClearSyncToken() *CalendarUpdateOne {
	_u.mutation.ClearSyncToken()
	return _u
}

// SetSupportedComponents sets the "supported_components" field.
func (_u *CalendarUpdateOne) SetSupportedComponents(v []string) *CalendarUpdateOne {
	_u.mutation.SetSupportedComponents(v)
	return _u
}

// AppendSupportedComponents appends value to the "supported_components" field.
func (_u *CalendarUpdateOne) AppendSupportedComponents(v []string) *CalendarUpdateOne {
	_u.mutation.AppendSupportedComponents(v)
	return _u
}

// ClearSupportedComponents clears the value of the "supported_components" field.
func (_u *CalendarUpdateOne) ClearSupportedComponents() *CalendarUpdateOne {
	_u.mutation.ClearSupportedComponents()
	return _u
}

// SetUpdatedAt sets the "updated_at" field.
func (_u *CalendarUpdateOne) SetUpdatedAt(v time.Time) *CalendarUpdateOne {
	_u.mutation.SetUpdatedAt(v)
	return _u
}

// SetAccountID sets the "account" edge to the Account entity by ID.
func (_u *CalendarUpdateOne) SetAccountID(id string) *CalendarUpdateOne {
	_u.mutation.SetAccountID(id)
	return _u
}

// SetAccount sets the "account" edge to the Account entity.
func (_u *CalendarUpdateOne) SetAccount(v *Account) *CalendarUpdateOne {
	return _u.SetAccountID(v.ID)
}

// AddEventIDs adds the "events" edge to the Event entity by IDs.
func (_u *CalendarUpdateOne) AddEventIDs(ids ...string) *CalendarUpdateOne {
	_u.mutation.AddEventIDs(ids...)
	return _u
}

// AddEvents adds the "events" edges to the Event entity.
func (_u *CalendarUpdateOne) AddEvents(v ...*Event) *CalendarUpdateOne {
	ids := make([]string, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddEventIDs(ids...)
}

// AddTaskIDs adds the "tasks" edge to the Task entity by IDs.
func (_u *CalendarUpdateOne) AddTaskIDs(ids ...string) *CalendarUpdateOne {
	_u.mutation.AddTaskIDs(ids...)
	return _u
}

// AddTasks adds the "tasks" edges to the Task entity.
func (_u *CalendarUpdateOne) AddTasks(v ...*Task) *CalendarUpdateOne {
	ids := make([]string, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddTaskIDs(ids...)
}

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

// ClearAccount clears the "account" edge to the Account entity.
func (_u *CalendarUpdateOne) ClearAccount() *CalendarUpdateOne {
	_u.mutation.ClearAccount()
	return _u
}

// ClearEvents clears all "events" edges to the Event entity.
func (_u *CalendarUpdateOne) ClearEvents() *CalendarUpdateOne {
	_u.mutation.ClearEvents()
	return _u
}

// RemoveEventIDs removes the "events" edge to Event entities by IDs.
func (_u *CalendarUpdateOne) RemoveEventIDs(ids ...string) *CalendarUpdateOne {
	_u.mutation.RemoveEventIDs(ids...)
	return _u
}

// RemoveEvents removes "events" edges to Event entities.
func (_u *CalendarUpdateOne) RemoveEvents(v ...*Event) *CalendarUpdateOne {
	ids := make([]string, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveEventIDs(ids...)
}

// ClearTasks clears all "tasks" edges to the Task entity.
func (_u *CalendarUpdateOne) ClearTasks() *CalendarUpdateOne {
	_u.mutation.ClearTasks()
	return _u
}

// RemoveTaskIDs removes the "tasks" edge to Task entities by IDs.
func (_u *CalendarUpdateOne) RemoveTaskIDs(ids ...string) *CalendarUpdateOne {
	_u.mutation.RemoveTaskIDs(ids...)
	return _u
}

// RemoveTasks removes "tasks" edges to Task entities.
func (_u *CalendarUpdateOne) RemoveTasks(v ...*Task) *CalendarUpdateOne {
	ids := make([]string, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveTaskIDs(ids...)
}

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

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *CalendarUpdateOne) 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 *CalendarUpdateOne) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := calendar.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_u *CalendarUpdateOne) check() error {
	if v, ok := _u.mutation.RemoteID(); ok {
		if err := calendar.RemoteIDValidator(v); err != nil {
			return &ValidationError{Name: "remote_id", err: fmt.Errorf(`ent: validator failed for field "Calendar.remote_id": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Name(); ok {
		if err := calendar.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Calendar.name": %w`, err)}
		}
	}
	if _u.mutation.AccountCleared() && len(_u.mutation.AccountIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "Calendar.account"`)
	}
	return nil
}

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

func (_u *CalendarUpdateOne) sqlSave(ctx context.Context) (_node *Calendar, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(calendar.Table, calendar.Columns, sqlgraph.NewFieldSpec(calendar.FieldID, field.TypeString))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Calendar.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, calendar.FieldID)
		for _, f := range fields {
			if !calendar.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != calendar.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.RemoteID(); ok {
		_spec.SetField(calendar.FieldRemoteID, field.TypeString, value)
	}
	if value, ok := _u.mutation.Name(); ok {
		_spec.SetField(calendar.FieldName, field.TypeString, value)
	}
	if value, ok := _u.mutation.NameOverride(); ok {
		_spec.SetField(calendar.FieldNameOverride, field.TypeString, value)
	}
	if _u.mutation.NameOverrideCleared() {
		_spec.ClearField(calendar.FieldNameOverride, field.TypeString)
	}
	if value, ok := _u.mutation.Description(); ok {
		_spec.SetField(calendar.FieldDescription, field.TypeString, value)
	}
	if _u.mutation.DescriptionCleared() {
		_spec.ClearField(calendar.FieldDescription, field.TypeString)
	}
	if value, ok := _u.mutation.Color(); ok {
		_spec.SetField(calendar.FieldColor, field.TypeString, value)
	}
	if _u.mutation.ColorCleared() {
		_spec.ClearField(calendar.FieldColor, field.TypeString)
	}
	if value, ok := _u.mutation.TimeZone(); ok {
		_spec.SetField(calendar.FieldTimeZone, field.TypeString, value)
	}
	if _u.mutation.TimeZoneCleared() {
		_spec.ClearField(calendar.FieldTimeZone, field.TypeString)
	}
	if value, ok := _u.mutation.ReadOnly(); ok {
		_spec.SetField(calendar.FieldReadOnly, field.TypeBool, value)
	}
	if value, ok := _u.mutation.Hidden(); ok {
		_spec.SetField(calendar.FieldHidden, field.TypeBool, value)
	}
	if value, ok := _u.mutation.ReminderOverrides(); ok {
		_spec.SetField(calendar.FieldReminderOverrides, field.TypeJSON, value)
	}
	if _u.mutation.ReminderOverridesCleared() {
		_spec.ClearField(calendar.FieldReminderOverrides, field.TypeJSON)
	}
	if value, ok := _u.mutation.SyncToken(); ok {
		_spec.SetField(calendar.FieldSyncToken, field.TypeString, value)
	}
	if _u.mutation.SyncTokenCleared() {
		_spec.ClearField(calendar.FieldSyncToken, field.TypeString)
	}
	if value, ok := _u.mutation.SupportedComponents(); ok {
		_spec.SetField(calendar.FieldSupportedComponents, field.TypeJSON, value)
	}
	if value, ok := _u.mutation.AppendedSupportedComponents(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, calendar.FieldSupportedComponents, value)
		})
	}
	if _u.mutation.SupportedComponentsCleared() {
		_spec.ClearField(calendar.FieldSupportedComponents, field.TypeJSON)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(calendar.FieldUpdatedAt, field.TypeTime, value)
	}
	if _u.mutation.AccountCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   calendar.AccountTable,
			Columns: []string{calendar.AccountColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeString),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.AccountIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   calendar.AccountTable,
			Columns: []string{calendar.AccountColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeString),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.EventsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   calendar.EventsTable,
			Columns: []string{calendar.EventsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(event.FieldID, field.TypeString),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedEventsIDs(); len(nodes) > 0 && !_u.mutation.EventsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   calendar.EventsTable,
			Columns: []string{calendar.EventsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(event.FieldID, field.TypeString),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.EventsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   calendar.EventsTable,
			Columns: []string{calendar.EventsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(event.FieldID, field.TypeString),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.TasksCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   calendar.TasksTable,
			Columns: []string{calendar.TasksColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(task.FieldID, field.TypeString),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedTasksIDs(); len(nodes) > 0 && !_u.mutation.TasksCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   calendar.TasksTable,
			Columns: []string{calendar.TasksColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(task.FieldID, field.TypeString),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.TasksIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   calendar.TasksTable,
			Columns: []string{calendar.TasksColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(task.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 = &Calendar{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{calendar.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
