// 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/google/uuid"
	"github.com/hay-kot/homebox/backend/internal/data/ent/group"
	"github.com/hay-kot/homebox/backend/internal/data/ent/notifier"
	"github.com/hay-kot/homebox/backend/internal/data/ent/predicate"
	"github.com/hay-kot/homebox/backend/internal/data/ent/user"
)

// NotifierUpdate is the builder for updating Notifier entities.
type NotifierUpdate struct {
	config
	hooks    []Hook
	mutation *NotifierMutation
}

// Where appends a list predicates to the NotifierUpdate builder.
func (nu *NotifierUpdate) Where(ps ...predicate.Notifier) *NotifierUpdate {
	nu.mutation.Where(ps...)
	return nu
}

// SetUpdatedAt sets the "updated_at" field.
func (nu *NotifierUpdate) SetUpdatedAt(t time.Time) *NotifierUpdate {
	nu.mutation.SetUpdatedAt(t)
	return nu
}

// SetGroupID sets the "group_id" field.
func (nu *NotifierUpdate) SetGroupID(u uuid.UUID) *NotifierUpdate {
	nu.mutation.SetGroupID(u)
	return nu
}

// SetNillableGroupID sets the "group_id" field if the given value is not nil.
func (nu *NotifierUpdate) SetNillableGroupID(u *uuid.UUID) *NotifierUpdate {
	if u != nil {
		nu.SetGroupID(*u)
	}
	return nu
}

// SetUserID sets the "user_id" field.
func (nu *NotifierUpdate) SetUserID(u uuid.UUID) *NotifierUpdate {
	nu.mutation.SetUserID(u)
	return nu
}

// SetNillableUserID sets the "user_id" field if the given value is not nil.
func (nu *NotifierUpdate) SetNillableUserID(u *uuid.UUID) *NotifierUpdate {
	if u != nil {
		nu.SetUserID(*u)
	}
	return nu
}

// SetName sets the "name" field.
func (nu *NotifierUpdate) SetName(s string) *NotifierUpdate {
	nu.mutation.SetName(s)
	return nu
}

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

// SetURL sets the "url" field.
func (nu *NotifierUpdate) SetURL(s string) *NotifierUpdate {
	nu.mutation.SetURL(s)
	return nu
}

// SetNillableURL sets the "url" field if the given value is not nil.
func (nu *NotifierUpdate) SetNillableURL(s *string) *NotifierUpdate {
	if s != nil {
		nu.SetURL(*s)
	}
	return nu
}

// SetIsActive sets the "is_active" field.
func (nu *NotifierUpdate) SetIsActive(b bool) *NotifierUpdate {
	nu.mutation.SetIsActive(b)
	return nu
}

// SetNillableIsActive sets the "is_active" field if the given value is not nil.
func (nu *NotifierUpdate) SetNillableIsActive(b *bool) *NotifierUpdate {
	if b != nil {
		nu.SetIsActive(*b)
	}
	return nu
}

// SetGroup sets the "group" edge to the Group entity.
func (nu *NotifierUpdate) SetGroup(g *Group) *NotifierUpdate {
	return nu.SetGroupID(g.ID)
}

// SetUser sets the "user" edge to the User entity.
func (nu *NotifierUpdate) SetUser(u *User) *NotifierUpdate {
	return nu.SetUserID(u.ID)
}

// Mutation returns the NotifierMutation object of the builder.
func (nu *NotifierUpdate) Mutation() *NotifierMutation {
	return nu.mutation
}

// ClearGroup clears the "group" edge to the Group entity.
func (nu *NotifierUpdate) ClearGroup() *NotifierUpdate {
	nu.mutation.ClearGroup()
	return nu
}

// ClearUser clears the "user" edge to the User entity.
func (nu *NotifierUpdate) ClearUser() *NotifierUpdate {
	nu.mutation.ClearUser()
	return nu
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (nu *NotifierUpdate) defaults() {
	if _, ok := nu.mutation.UpdatedAt(); !ok {
		v := notifier.UpdateDefaultUpdatedAt()
		nu.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (nu *NotifierUpdate) check() error {
	if v, ok := nu.mutation.Name(); ok {
		if err := notifier.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Notifier.name": %w`, err)}
		}
	}
	if v, ok := nu.mutation.URL(); ok {
		if err := notifier.URLValidator(v); err != nil {
			return &ValidationError{Name: "url", err: fmt.Errorf(`ent: validator failed for field "Notifier.url": %w`, err)}
		}
	}
	if _, ok := nu.mutation.GroupID(); nu.mutation.GroupCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "Notifier.group"`)
	}
	if _, ok := nu.mutation.UserID(); nu.mutation.UserCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "Notifier.user"`)
	}
	return nil
}

func (nu *NotifierUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := nu.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(notifier.Table, notifier.Columns, sqlgraph.NewFieldSpec(notifier.FieldID, field.TypeUUID))
	if ps := nu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := nu.mutation.UpdatedAt(); ok {
		_spec.SetField(notifier.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := nu.mutation.Name(); ok {
		_spec.SetField(notifier.FieldName, field.TypeString, value)
	}
	if value, ok := nu.mutation.URL(); ok {
		_spec.SetField(notifier.FieldURL, field.TypeString, value)
	}
	if value, ok := nu.mutation.IsActive(); ok {
		_spec.SetField(notifier.FieldIsActive, field.TypeBool, value)
	}
	if nu.mutation.GroupCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   notifier.GroupTable,
			Columns: []string{notifier.GroupColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := nu.mutation.GroupIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   notifier.GroupTable,
			Columns: []string{notifier.GroupColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if nu.mutation.UserCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   notifier.UserTable,
			Columns: []string{notifier.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := nu.mutation.UserIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   notifier.UserTable,
			Columns: []string{notifier.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, nu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{notifier.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	nu.mutation.done = true
	return n, nil
}

// NotifierUpdateOne is the builder for updating a single Notifier entity.
type NotifierUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *NotifierMutation
}

// SetUpdatedAt sets the "updated_at" field.
func (nuo *NotifierUpdateOne) SetUpdatedAt(t time.Time) *NotifierUpdateOne {
	nuo.mutation.SetUpdatedAt(t)
	return nuo
}

// SetGroupID sets the "group_id" field.
func (nuo *NotifierUpdateOne) SetGroupID(u uuid.UUID) *NotifierUpdateOne {
	nuo.mutation.SetGroupID(u)
	return nuo
}

// SetNillableGroupID sets the "group_id" field if the given value is not nil.
func (nuo *NotifierUpdateOne) SetNillableGroupID(u *uuid.UUID) *NotifierUpdateOne {
	if u != nil {
		nuo.SetGroupID(*u)
	}
	return nuo
}

// SetUserID sets the "user_id" field.
func (nuo *NotifierUpdateOne) SetUserID(u uuid.UUID) *NotifierUpdateOne {
	nuo.mutation.SetUserID(u)
	return nuo
}

// SetNillableUserID sets the "user_id" field if the given value is not nil.
func (nuo *NotifierUpdateOne) SetNillableUserID(u *uuid.UUID) *NotifierUpdateOne {
	if u != nil {
		nuo.SetUserID(*u)
	}
	return nuo
}

// SetName sets the "name" field.
func (nuo *NotifierUpdateOne) SetName(s string) *NotifierUpdateOne {
	nuo.mutation.SetName(s)
	return nuo
}

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

// SetURL sets the "url" field.
func (nuo *NotifierUpdateOne) SetURL(s string) *NotifierUpdateOne {
	nuo.mutation.SetURL(s)
	return nuo
}

// SetNillableURL sets the "url" field if the given value is not nil.
func (nuo *NotifierUpdateOne) SetNillableURL(s *string) *NotifierUpdateOne {
	if s != nil {
		nuo.SetURL(*s)
	}
	return nuo
}

// SetIsActive sets the "is_active" field.
func (nuo *NotifierUpdateOne) SetIsActive(b bool) *NotifierUpdateOne {
	nuo.mutation.SetIsActive(b)
	return nuo
}

// SetNillableIsActive sets the "is_active" field if the given value is not nil.
func (nuo *NotifierUpdateOne) SetNillableIsActive(b *bool) *NotifierUpdateOne {
	if b != nil {
		nuo.SetIsActive(*b)
	}
	return nuo
}

// SetGroup sets the "group" edge to the Group entity.
func (nuo *NotifierUpdateOne) SetGroup(g *Group) *NotifierUpdateOne {
	return nuo.SetGroupID(g.ID)
}

// SetUser sets the "user" edge to the User entity.
func (nuo *NotifierUpdateOne) SetUser(u *User) *NotifierUpdateOne {
	return nuo.SetUserID(u.ID)
}

// Mutation returns the NotifierMutation object of the builder.
func (nuo *NotifierUpdateOne) Mutation() *NotifierMutation {
	return nuo.mutation
}

// ClearGroup clears the "group" edge to the Group entity.
func (nuo *NotifierUpdateOne) ClearGroup() *NotifierUpdateOne {
	nuo.mutation.ClearGroup()
	return nuo
}

// ClearUser clears the "user" edge to the User entity.
func (nuo *NotifierUpdateOne) ClearUser() *NotifierUpdateOne {
	nuo.mutation.ClearUser()
	return nuo
}

// Where appends a list predicates to the NotifierUpdate builder.
func (nuo *NotifierUpdateOne) Where(ps ...predicate.Notifier) *NotifierUpdateOne {
	nuo.mutation.Where(ps...)
	return nuo
}

// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (nuo *NotifierUpdateOne) Select(field string, fields ...string) *NotifierUpdateOne {
	nuo.fields = append([]string{field}, fields...)
	return nuo
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (nuo *NotifierUpdateOne) defaults() {
	if _, ok := nuo.mutation.UpdatedAt(); !ok {
		v := notifier.UpdateDefaultUpdatedAt()
		nuo.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (nuo *NotifierUpdateOne) check() error {
	if v, ok := nuo.mutation.Name(); ok {
		if err := notifier.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Notifier.name": %w`, err)}
		}
	}
	if v, ok := nuo.mutation.URL(); ok {
		if err := notifier.URLValidator(v); err != nil {
			return &ValidationError{Name: "url", err: fmt.Errorf(`ent: validator failed for field "Notifier.url": %w`, err)}
		}
	}
	if _, ok := nuo.mutation.GroupID(); nuo.mutation.GroupCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "Notifier.group"`)
	}
	if _, ok := nuo.mutation.UserID(); nuo.mutation.UserCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "Notifier.user"`)
	}
	return nil
}

func (nuo *NotifierUpdateOne) sqlSave(ctx context.Context) (_node *Notifier, err error) {
	if err := nuo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(notifier.Table, notifier.Columns, sqlgraph.NewFieldSpec(notifier.FieldID, field.TypeUUID))
	id, ok := nuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Notifier.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := nuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, notifier.FieldID)
		for _, f := range fields {
			if !notifier.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != notifier.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := nuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := nuo.mutation.UpdatedAt(); ok {
		_spec.SetField(notifier.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := nuo.mutation.Name(); ok {
		_spec.SetField(notifier.FieldName, field.TypeString, value)
	}
	if value, ok := nuo.mutation.URL(); ok {
		_spec.SetField(notifier.FieldURL, field.TypeString, value)
	}
	if value, ok := nuo.mutation.IsActive(); ok {
		_spec.SetField(notifier.FieldIsActive, field.TypeBool, value)
	}
	if nuo.mutation.GroupCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   notifier.GroupTable,
			Columns: []string{notifier.GroupColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := nuo.mutation.GroupIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   notifier.GroupTable,
			Columns: []string{notifier.GroupColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if nuo.mutation.UserCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   notifier.UserTable,
			Columns: []string{notifier.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := nuo.mutation.UserIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   notifier.UserTable,
			Columns: []string{notifier.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &Notifier{config: nuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, nuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{notifier.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	nuo.mutation.done = true
	return _node, nil
}
