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

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

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

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

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

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

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

// SetEventStart sets the "event_start" field.
func (_u *InvitationStateUpdate) SetEventStart(v time.Time) *InvitationStateUpdate {
	_u.mutation.SetEventStart(v)
	return _u
}

// SetNillableEventStart sets the "event_start" field if the given value is not nil.
func (_u *InvitationStateUpdate) SetNillableEventStart(v *time.Time) *InvitationStateUpdate {
	if v != nil {
		_u.SetEventStart(*v)
	}
	return _u
}

// SetNotifiedAt sets the "notified_at" field.
func (_u *InvitationStateUpdate) SetNotifiedAt(v time.Time) *InvitationStateUpdate {
	_u.mutation.SetNotifiedAt(v)
	return _u
}

// SetNillableNotifiedAt sets the "notified_at" field if the given value is not nil.
func (_u *InvitationStateUpdate) SetNillableNotifiedAt(v *time.Time) *InvitationStateUpdate {
	if v != nil {
		_u.SetNotifiedAt(*v)
	}
	return _u
}

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

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

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

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

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

func (_u *InvitationStateUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(invitationstate.Table, invitationstate.Columns, sqlgraph.NewFieldSpec(invitationstate.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(invitationstate.FieldCalendarID, field.TypeString, value)
	}
	if value, ok := _u.mutation.UID(); ok {
		_spec.SetField(invitationstate.FieldUID, field.TypeString, value)
	}
	if value, ok := _u.mutation.EventStart(); ok {
		_spec.SetField(invitationstate.FieldEventStart, field.TypeTime, value)
	}
	if value, ok := _u.mutation.NotifiedAt(); ok {
		_spec.SetField(invitationstate.FieldNotifiedAt, field.TypeTime, value)
	}
	_spec.AddModifiers(_u.modifiers...)
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{invitationstate.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

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

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

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

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

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

// SetEventStart sets the "event_start" field.
func (_u *InvitationStateUpdateOne) SetEventStart(v time.Time) *InvitationStateUpdateOne {
	_u.mutation.SetEventStart(v)
	return _u
}

// SetNillableEventStart sets the "event_start" field if the given value is not nil.
func (_u *InvitationStateUpdateOne) SetNillableEventStart(v *time.Time) *InvitationStateUpdateOne {
	if v != nil {
		_u.SetEventStart(*v)
	}
	return _u
}

// SetNotifiedAt sets the "notified_at" field.
func (_u *InvitationStateUpdateOne) SetNotifiedAt(v time.Time) *InvitationStateUpdateOne {
	_u.mutation.SetNotifiedAt(v)
	return _u
}

// SetNillableNotifiedAt sets the "notified_at" field if the given value is not nil.
func (_u *InvitationStateUpdateOne) SetNillableNotifiedAt(v *time.Time) *InvitationStateUpdateOne {
	if v != nil {
		_u.SetNotifiedAt(*v)
	}
	return _u
}

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

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

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

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

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

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

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

func (_u *InvitationStateUpdateOne) sqlSave(ctx context.Context) (_node *InvitationState, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(invitationstate.Table, invitationstate.Columns, sqlgraph.NewFieldSpec(invitationstate.FieldID, field.TypeInt))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "InvitationState.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, invitationstate.FieldID)
		for _, f := range fields {
			if !invitationstate.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != invitationstate.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(invitationstate.FieldCalendarID, field.TypeString, value)
	}
	if value, ok := _u.mutation.UID(); ok {
		_spec.SetField(invitationstate.FieldUID, field.TypeString, value)
	}
	if value, ok := _u.mutation.EventStart(); ok {
		_spec.SetField(invitationstate.FieldEventStart, field.TypeTime, value)
	}
	if value, ok := _u.mutation.NotifiedAt(); ok {
		_spec.SetField(invitationstate.FieldNotifiedAt, field.TypeTime, value)
	}
	_spec.AddModifiers(_u.modifiers...)
	_node = &InvitationState{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{invitationstate.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
