// 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/Wei-Shaw/sub2api/ent/channelmonitor"
	"github.com/Wei-Shaw/sub2api/ent/channelmonitordailyrollup"
	"github.com/Wei-Shaw/sub2api/ent/channelmonitorhistory"
	"github.com/Wei-Shaw/sub2api/ent/predicate"
)

// ChannelMonitorUpdate is the builder for updating ChannelMonitor entities.
type ChannelMonitorUpdate struct {
	config
	hooks    []Hook
	mutation *ChannelMonitorMutation
}

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

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

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

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

// SetProvider sets the "provider" field.
func (_u *ChannelMonitorUpdate) SetProvider(v channelmonitor.Provider) *ChannelMonitorUpdate {
	_u.mutation.SetProvider(v)
	return _u
}

// SetNillableProvider sets the "provider" field if the given value is not nil.
func (_u *ChannelMonitorUpdate) SetNillableProvider(v *channelmonitor.Provider) *ChannelMonitorUpdate {
	if v != nil {
		_u.SetProvider(*v)
	}
	return _u
}

// SetEndpoint sets the "endpoint" field.
func (_u *ChannelMonitorUpdate) SetEndpoint(v string) *ChannelMonitorUpdate {
	_u.mutation.SetEndpoint(v)
	return _u
}

// SetNillableEndpoint sets the "endpoint" field if the given value is not nil.
func (_u *ChannelMonitorUpdate) SetNillableEndpoint(v *string) *ChannelMonitorUpdate {
	if v != nil {
		_u.SetEndpoint(*v)
	}
	return _u
}

// SetAPIKeyEncrypted sets the "api_key_encrypted" field.
func (_u *ChannelMonitorUpdate) SetAPIKeyEncrypted(v string) *ChannelMonitorUpdate {
	_u.mutation.SetAPIKeyEncrypted(v)
	return _u
}

// SetNillableAPIKeyEncrypted sets the "api_key_encrypted" field if the given value is not nil.
func (_u *ChannelMonitorUpdate) SetNillableAPIKeyEncrypted(v *string) *ChannelMonitorUpdate {
	if v != nil {
		_u.SetAPIKeyEncrypted(*v)
	}
	return _u
}

// SetPrimaryModel sets the "primary_model" field.
func (_u *ChannelMonitorUpdate) SetPrimaryModel(v string) *ChannelMonitorUpdate {
	_u.mutation.SetPrimaryModel(v)
	return _u
}

// SetNillablePrimaryModel sets the "primary_model" field if the given value is not nil.
func (_u *ChannelMonitorUpdate) SetNillablePrimaryModel(v *string) *ChannelMonitorUpdate {
	if v != nil {
		_u.SetPrimaryModel(*v)
	}
	return _u
}

// SetExtraModels sets the "extra_models" field.
func (_u *ChannelMonitorUpdate) SetExtraModels(v []string) *ChannelMonitorUpdate {
	_u.mutation.SetExtraModels(v)
	return _u
}

// AppendExtraModels appends value to the "extra_models" field.
func (_u *ChannelMonitorUpdate) AppendExtraModels(v []string) *ChannelMonitorUpdate {
	_u.mutation.AppendExtraModels(v)
	return _u
}

// SetGroupName sets the "group_name" field.
func (_u *ChannelMonitorUpdate) SetGroupName(v string) *ChannelMonitorUpdate {
	_u.mutation.SetGroupName(v)
	return _u
}

// SetNillableGroupName sets the "group_name" field if the given value is not nil.
func (_u *ChannelMonitorUpdate) SetNillableGroupName(v *string) *ChannelMonitorUpdate {
	if v != nil {
		_u.SetGroupName(*v)
	}
	return _u
}

// ClearGroupName clears the value of the "group_name" field.
func (_u *ChannelMonitorUpdate) ClearGroupName() *ChannelMonitorUpdate {
	_u.mutation.ClearGroupName()
	return _u
}

// SetEnabled sets the "enabled" field.
func (_u *ChannelMonitorUpdate) SetEnabled(v bool) *ChannelMonitorUpdate {
	_u.mutation.SetEnabled(v)
	return _u
}

// SetNillableEnabled sets the "enabled" field if the given value is not nil.
func (_u *ChannelMonitorUpdate) SetNillableEnabled(v *bool) *ChannelMonitorUpdate {
	if v != nil {
		_u.SetEnabled(*v)
	}
	return _u
}

// SetIntervalSeconds sets the "interval_seconds" field.
func (_u *ChannelMonitorUpdate) SetIntervalSeconds(v int) *ChannelMonitorUpdate {
	_u.mutation.ResetIntervalSeconds()
	_u.mutation.SetIntervalSeconds(v)
	return _u
}

// SetNillableIntervalSeconds sets the "interval_seconds" field if the given value is not nil.
func (_u *ChannelMonitorUpdate) SetNillableIntervalSeconds(v *int) *ChannelMonitorUpdate {
	if v != nil {
		_u.SetIntervalSeconds(*v)
	}
	return _u
}

// AddIntervalSeconds adds value to the "interval_seconds" field.
func (_u *ChannelMonitorUpdate) AddIntervalSeconds(v int) *ChannelMonitorUpdate {
	_u.mutation.AddIntervalSeconds(v)
	return _u
}

// SetLastCheckedAt sets the "last_checked_at" field.
func (_u *ChannelMonitorUpdate) SetLastCheckedAt(v time.Time) *ChannelMonitorUpdate {
	_u.mutation.SetLastCheckedAt(v)
	return _u
}

// SetNillableLastCheckedAt sets the "last_checked_at" field if the given value is not nil.
func (_u *ChannelMonitorUpdate) SetNillableLastCheckedAt(v *time.Time) *ChannelMonitorUpdate {
	if v != nil {
		_u.SetLastCheckedAt(*v)
	}
	return _u
}

// ClearLastCheckedAt clears the value of the "last_checked_at" field.
func (_u *ChannelMonitorUpdate) ClearLastCheckedAt() *ChannelMonitorUpdate {
	_u.mutation.ClearLastCheckedAt()
	return _u
}

// SetCreatedBy sets the "created_by" field.
func (_u *ChannelMonitorUpdate) SetCreatedBy(v int64) *ChannelMonitorUpdate {
	_u.mutation.ResetCreatedBy()
	_u.mutation.SetCreatedBy(v)
	return _u
}

// SetNillableCreatedBy sets the "created_by" field if the given value is not nil.
func (_u *ChannelMonitorUpdate) SetNillableCreatedBy(v *int64) *ChannelMonitorUpdate {
	if v != nil {
		_u.SetCreatedBy(*v)
	}
	return _u
}

// AddCreatedBy adds value to the "created_by" field.
func (_u *ChannelMonitorUpdate) AddCreatedBy(v int64) *ChannelMonitorUpdate {
	_u.mutation.AddCreatedBy(v)
	return _u
}

// AddHistoryIDs adds the "history" edge to the ChannelMonitorHistory entity by IDs.
func (_u *ChannelMonitorUpdate) AddHistoryIDs(ids ...int64) *ChannelMonitorUpdate {
	_u.mutation.AddHistoryIDs(ids...)
	return _u
}

// AddHistory adds the "history" edges to the ChannelMonitorHistory entity.
func (_u *ChannelMonitorUpdate) AddHistory(v ...*ChannelMonitorHistory) *ChannelMonitorUpdate {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddHistoryIDs(ids...)
}

// AddDailyRollupIDs adds the "daily_rollups" edge to the ChannelMonitorDailyRollup entity by IDs.
func (_u *ChannelMonitorUpdate) AddDailyRollupIDs(ids ...int64) *ChannelMonitorUpdate {
	_u.mutation.AddDailyRollupIDs(ids...)
	return _u
}

// AddDailyRollups adds the "daily_rollups" edges to the ChannelMonitorDailyRollup entity.
func (_u *ChannelMonitorUpdate) AddDailyRollups(v ...*ChannelMonitorDailyRollup) *ChannelMonitorUpdate {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddDailyRollupIDs(ids...)
}

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

// ClearHistory clears all "history" edges to the ChannelMonitorHistory entity.
func (_u *ChannelMonitorUpdate) ClearHistory() *ChannelMonitorUpdate {
	_u.mutation.ClearHistory()
	return _u
}

// RemoveHistoryIDs removes the "history" edge to ChannelMonitorHistory entities by IDs.
func (_u *ChannelMonitorUpdate) RemoveHistoryIDs(ids ...int64) *ChannelMonitorUpdate {
	_u.mutation.RemoveHistoryIDs(ids...)
	return _u
}

// RemoveHistory removes "history" edges to ChannelMonitorHistory entities.
func (_u *ChannelMonitorUpdate) RemoveHistory(v ...*ChannelMonitorHistory) *ChannelMonitorUpdate {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveHistoryIDs(ids...)
}

// ClearDailyRollups clears all "daily_rollups" edges to the ChannelMonitorDailyRollup entity.
func (_u *ChannelMonitorUpdate) ClearDailyRollups() *ChannelMonitorUpdate {
	_u.mutation.ClearDailyRollups()
	return _u
}

// RemoveDailyRollupIDs removes the "daily_rollups" edge to ChannelMonitorDailyRollup entities by IDs.
func (_u *ChannelMonitorUpdate) RemoveDailyRollupIDs(ids ...int64) *ChannelMonitorUpdate {
	_u.mutation.RemoveDailyRollupIDs(ids...)
	return _u
}

// RemoveDailyRollups removes "daily_rollups" edges to ChannelMonitorDailyRollup entities.
func (_u *ChannelMonitorUpdate) RemoveDailyRollups(v ...*ChannelMonitorDailyRollup) *ChannelMonitorUpdate {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveDailyRollupIDs(ids...)
}

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

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

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

// check runs all checks and user-defined validators on the builder.
func (_u *ChannelMonitorUpdate) check() error {
	if v, ok := _u.mutation.Name(); ok {
		if err := channelmonitor.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "ChannelMonitor.name": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Provider(); ok {
		if err := channelmonitor.ProviderValidator(v); err != nil {
			return &ValidationError{Name: "provider", err: fmt.Errorf(`ent: validator failed for field "ChannelMonitor.provider": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Endpoint(); ok {
		if err := channelmonitor.EndpointValidator(v); err != nil {
			return &ValidationError{Name: "endpoint", err: fmt.Errorf(`ent: validator failed for field "ChannelMonitor.endpoint": %w`, err)}
		}
	}
	if v, ok := _u.mutation.APIKeyEncrypted(); ok {
		if err := channelmonitor.APIKeyEncryptedValidator(v); err != nil {
			return &ValidationError{Name: "api_key_encrypted", err: fmt.Errorf(`ent: validator failed for field "ChannelMonitor.api_key_encrypted": %w`, err)}
		}
	}
	if v, ok := _u.mutation.PrimaryModel(); ok {
		if err := channelmonitor.PrimaryModelValidator(v); err != nil {
			return &ValidationError{Name: "primary_model", err: fmt.Errorf(`ent: validator failed for field "ChannelMonitor.primary_model": %w`, err)}
		}
	}
	if v, ok := _u.mutation.GroupName(); ok {
		if err := channelmonitor.GroupNameValidator(v); err != nil {
			return &ValidationError{Name: "group_name", err: fmt.Errorf(`ent: validator failed for field "ChannelMonitor.group_name": %w`, err)}
		}
	}
	if v, ok := _u.mutation.IntervalSeconds(); ok {
		if err := channelmonitor.IntervalSecondsValidator(v); err != nil {
			return &ValidationError{Name: "interval_seconds", err: fmt.Errorf(`ent: validator failed for field "ChannelMonitor.interval_seconds": %w`, err)}
		}
	}
	return nil
}

func (_u *ChannelMonitorUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(channelmonitor.Table, channelmonitor.Columns, sqlgraph.NewFieldSpec(channelmonitor.FieldID, field.TypeInt64))
	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.UpdatedAt(); ok {
		_spec.SetField(channelmonitor.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.Name(); ok {
		_spec.SetField(channelmonitor.FieldName, field.TypeString, value)
	}
	if value, ok := _u.mutation.Provider(); ok {
		_spec.SetField(channelmonitor.FieldProvider, field.TypeEnum, value)
	}
	if value, ok := _u.mutation.Endpoint(); ok {
		_spec.SetField(channelmonitor.FieldEndpoint, field.TypeString, value)
	}
	if value, ok := _u.mutation.APIKeyEncrypted(); ok {
		_spec.SetField(channelmonitor.FieldAPIKeyEncrypted, field.TypeString, value)
	}
	if value, ok := _u.mutation.PrimaryModel(); ok {
		_spec.SetField(channelmonitor.FieldPrimaryModel, field.TypeString, value)
	}
	if value, ok := _u.mutation.ExtraModels(); ok {
		_spec.SetField(channelmonitor.FieldExtraModels, field.TypeJSON, value)
	}
	if value, ok := _u.mutation.AppendedExtraModels(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, channelmonitor.FieldExtraModels, value)
		})
	}
	if value, ok := _u.mutation.GroupName(); ok {
		_spec.SetField(channelmonitor.FieldGroupName, field.TypeString, value)
	}
	if _u.mutation.GroupNameCleared() {
		_spec.ClearField(channelmonitor.FieldGroupName, field.TypeString)
	}
	if value, ok := _u.mutation.Enabled(); ok {
		_spec.SetField(channelmonitor.FieldEnabled, field.TypeBool, value)
	}
	if value, ok := _u.mutation.IntervalSeconds(); ok {
		_spec.SetField(channelmonitor.FieldIntervalSeconds, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedIntervalSeconds(); ok {
		_spec.AddField(channelmonitor.FieldIntervalSeconds, field.TypeInt, value)
	}
	if value, ok := _u.mutation.LastCheckedAt(); ok {
		_spec.SetField(channelmonitor.FieldLastCheckedAt, field.TypeTime, value)
	}
	if _u.mutation.LastCheckedAtCleared() {
		_spec.ClearField(channelmonitor.FieldLastCheckedAt, field.TypeTime)
	}
	if value, ok := _u.mutation.CreatedBy(); ok {
		_spec.SetField(channelmonitor.FieldCreatedBy, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedCreatedBy(); ok {
		_spec.AddField(channelmonitor.FieldCreatedBy, field.TypeInt64, value)
	}
	if _u.mutation.HistoryCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   channelmonitor.HistoryTable,
			Columns: []string{channelmonitor.HistoryColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(channelmonitorhistory.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedHistoryIDs(); len(nodes) > 0 && !_u.mutation.HistoryCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   channelmonitor.HistoryTable,
			Columns: []string{channelmonitor.HistoryColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(channelmonitorhistory.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.HistoryIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   channelmonitor.HistoryTable,
			Columns: []string{channelmonitor.HistoryColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(channelmonitorhistory.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.DailyRollupsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   channelmonitor.DailyRollupsTable,
			Columns: []string{channelmonitor.DailyRollupsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(channelmonitordailyrollup.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedDailyRollupsIDs(); len(nodes) > 0 && !_u.mutation.DailyRollupsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   channelmonitor.DailyRollupsTable,
			Columns: []string{channelmonitor.DailyRollupsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(channelmonitordailyrollup.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.DailyRollupsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   channelmonitor.DailyRollupsTable,
			Columns: []string{channelmonitor.DailyRollupsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(channelmonitordailyrollup.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{channelmonitor.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// ChannelMonitorUpdateOne is the builder for updating a single ChannelMonitor entity.
type ChannelMonitorUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *ChannelMonitorMutation
}

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

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

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

// SetProvider sets the "provider" field.
func (_u *ChannelMonitorUpdateOne) SetProvider(v channelmonitor.Provider) *ChannelMonitorUpdateOne {
	_u.mutation.SetProvider(v)
	return _u
}

// SetNillableProvider sets the "provider" field if the given value is not nil.
func (_u *ChannelMonitorUpdateOne) SetNillableProvider(v *channelmonitor.Provider) *ChannelMonitorUpdateOne {
	if v != nil {
		_u.SetProvider(*v)
	}
	return _u
}

// SetEndpoint sets the "endpoint" field.
func (_u *ChannelMonitorUpdateOne) SetEndpoint(v string) *ChannelMonitorUpdateOne {
	_u.mutation.SetEndpoint(v)
	return _u
}

// SetNillableEndpoint sets the "endpoint" field if the given value is not nil.
func (_u *ChannelMonitorUpdateOne) SetNillableEndpoint(v *string) *ChannelMonitorUpdateOne {
	if v != nil {
		_u.SetEndpoint(*v)
	}
	return _u
}

// SetAPIKeyEncrypted sets the "api_key_encrypted" field.
func (_u *ChannelMonitorUpdateOne) SetAPIKeyEncrypted(v string) *ChannelMonitorUpdateOne {
	_u.mutation.SetAPIKeyEncrypted(v)
	return _u
}

// SetNillableAPIKeyEncrypted sets the "api_key_encrypted" field if the given value is not nil.
func (_u *ChannelMonitorUpdateOne) SetNillableAPIKeyEncrypted(v *string) *ChannelMonitorUpdateOne {
	if v != nil {
		_u.SetAPIKeyEncrypted(*v)
	}
	return _u
}

// SetPrimaryModel sets the "primary_model" field.
func (_u *ChannelMonitorUpdateOne) SetPrimaryModel(v string) *ChannelMonitorUpdateOne {
	_u.mutation.SetPrimaryModel(v)
	return _u
}

// SetNillablePrimaryModel sets the "primary_model" field if the given value is not nil.
func (_u *ChannelMonitorUpdateOne) SetNillablePrimaryModel(v *string) *ChannelMonitorUpdateOne {
	if v != nil {
		_u.SetPrimaryModel(*v)
	}
	return _u
}

// SetExtraModels sets the "extra_models" field.
func (_u *ChannelMonitorUpdateOne) SetExtraModels(v []string) *ChannelMonitorUpdateOne {
	_u.mutation.SetExtraModels(v)
	return _u
}

// AppendExtraModels appends value to the "extra_models" field.
func (_u *ChannelMonitorUpdateOne) AppendExtraModels(v []string) *ChannelMonitorUpdateOne {
	_u.mutation.AppendExtraModels(v)
	return _u
}

// SetGroupName sets the "group_name" field.
func (_u *ChannelMonitorUpdateOne) SetGroupName(v string) *ChannelMonitorUpdateOne {
	_u.mutation.SetGroupName(v)
	return _u
}

// SetNillableGroupName sets the "group_name" field if the given value is not nil.
func (_u *ChannelMonitorUpdateOne) SetNillableGroupName(v *string) *ChannelMonitorUpdateOne {
	if v != nil {
		_u.SetGroupName(*v)
	}
	return _u
}

// ClearGroupName clears the value of the "group_name" field.
func (_u *ChannelMonitorUpdateOne) ClearGroupName() *ChannelMonitorUpdateOne {
	_u.mutation.ClearGroupName()
	return _u
}

// SetEnabled sets the "enabled" field.
func (_u *ChannelMonitorUpdateOne) SetEnabled(v bool) *ChannelMonitorUpdateOne {
	_u.mutation.SetEnabled(v)
	return _u
}

// SetNillableEnabled sets the "enabled" field if the given value is not nil.
func (_u *ChannelMonitorUpdateOne) SetNillableEnabled(v *bool) *ChannelMonitorUpdateOne {
	if v != nil {
		_u.SetEnabled(*v)
	}
	return _u
}

// SetIntervalSeconds sets the "interval_seconds" field.
func (_u *ChannelMonitorUpdateOne) SetIntervalSeconds(v int) *ChannelMonitorUpdateOne {
	_u.mutation.ResetIntervalSeconds()
	_u.mutation.SetIntervalSeconds(v)
	return _u
}

// SetNillableIntervalSeconds sets the "interval_seconds" field if the given value is not nil.
func (_u *ChannelMonitorUpdateOne) SetNillableIntervalSeconds(v *int) *ChannelMonitorUpdateOne {
	if v != nil {
		_u.SetIntervalSeconds(*v)
	}
	return _u
}

// AddIntervalSeconds adds value to the "interval_seconds" field.
func (_u *ChannelMonitorUpdateOne) AddIntervalSeconds(v int) *ChannelMonitorUpdateOne {
	_u.mutation.AddIntervalSeconds(v)
	return _u
}

// SetLastCheckedAt sets the "last_checked_at" field.
func (_u *ChannelMonitorUpdateOne) SetLastCheckedAt(v time.Time) *ChannelMonitorUpdateOne {
	_u.mutation.SetLastCheckedAt(v)
	return _u
}

// SetNillableLastCheckedAt sets the "last_checked_at" field if the given value is not nil.
func (_u *ChannelMonitorUpdateOne) SetNillableLastCheckedAt(v *time.Time) *ChannelMonitorUpdateOne {
	if v != nil {
		_u.SetLastCheckedAt(*v)
	}
	return _u
}

// ClearLastCheckedAt clears the value of the "last_checked_at" field.
func (_u *ChannelMonitorUpdateOne) ClearLastCheckedAt() *ChannelMonitorUpdateOne {
	_u.mutation.ClearLastCheckedAt()
	return _u
}

// SetCreatedBy sets the "created_by" field.
func (_u *ChannelMonitorUpdateOne) SetCreatedBy(v int64) *ChannelMonitorUpdateOne {
	_u.mutation.ResetCreatedBy()
	_u.mutation.SetCreatedBy(v)
	return _u
}

// SetNillableCreatedBy sets the "created_by" field if the given value is not nil.
func (_u *ChannelMonitorUpdateOne) SetNillableCreatedBy(v *int64) *ChannelMonitorUpdateOne {
	if v != nil {
		_u.SetCreatedBy(*v)
	}
	return _u
}

// AddCreatedBy adds value to the "created_by" field.
func (_u *ChannelMonitorUpdateOne) AddCreatedBy(v int64) *ChannelMonitorUpdateOne {
	_u.mutation.AddCreatedBy(v)
	return _u
}

// AddHistoryIDs adds the "history" edge to the ChannelMonitorHistory entity by IDs.
func (_u *ChannelMonitorUpdateOne) AddHistoryIDs(ids ...int64) *ChannelMonitorUpdateOne {
	_u.mutation.AddHistoryIDs(ids...)
	return _u
}

// AddHistory adds the "history" edges to the ChannelMonitorHistory entity.
func (_u *ChannelMonitorUpdateOne) AddHistory(v ...*ChannelMonitorHistory) *ChannelMonitorUpdateOne {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddHistoryIDs(ids...)
}

// AddDailyRollupIDs adds the "daily_rollups" edge to the ChannelMonitorDailyRollup entity by IDs.
func (_u *ChannelMonitorUpdateOne) AddDailyRollupIDs(ids ...int64) *ChannelMonitorUpdateOne {
	_u.mutation.AddDailyRollupIDs(ids...)
	return _u
}

// AddDailyRollups adds the "daily_rollups" edges to the ChannelMonitorDailyRollup entity.
func (_u *ChannelMonitorUpdateOne) AddDailyRollups(v ...*ChannelMonitorDailyRollup) *ChannelMonitorUpdateOne {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddDailyRollupIDs(ids...)
}

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

// ClearHistory clears all "history" edges to the ChannelMonitorHistory entity.
func (_u *ChannelMonitorUpdateOne) ClearHistory() *ChannelMonitorUpdateOne {
	_u.mutation.ClearHistory()
	return _u
}

// RemoveHistoryIDs removes the "history" edge to ChannelMonitorHistory entities by IDs.
func (_u *ChannelMonitorUpdateOne) RemoveHistoryIDs(ids ...int64) *ChannelMonitorUpdateOne {
	_u.mutation.RemoveHistoryIDs(ids...)
	return _u
}

// RemoveHistory removes "history" edges to ChannelMonitorHistory entities.
func (_u *ChannelMonitorUpdateOne) RemoveHistory(v ...*ChannelMonitorHistory) *ChannelMonitorUpdateOne {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveHistoryIDs(ids...)
}

// ClearDailyRollups clears all "daily_rollups" edges to the ChannelMonitorDailyRollup entity.
func (_u *ChannelMonitorUpdateOne) ClearDailyRollups() *ChannelMonitorUpdateOne {
	_u.mutation.ClearDailyRollups()
	return _u
}

// RemoveDailyRollupIDs removes the "daily_rollups" edge to ChannelMonitorDailyRollup entities by IDs.
func (_u *ChannelMonitorUpdateOne) RemoveDailyRollupIDs(ids ...int64) *ChannelMonitorUpdateOne {
	_u.mutation.RemoveDailyRollupIDs(ids...)
	return _u
}

// RemoveDailyRollups removes "daily_rollups" edges to ChannelMonitorDailyRollup entities.
func (_u *ChannelMonitorUpdateOne) RemoveDailyRollups(v ...*ChannelMonitorDailyRollup) *ChannelMonitorUpdateOne {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveDailyRollupIDs(ids...)
}

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (_u *ChannelMonitorUpdateOne) check() error {
	if v, ok := _u.mutation.Name(); ok {
		if err := channelmonitor.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "ChannelMonitor.name": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Provider(); ok {
		if err := channelmonitor.ProviderValidator(v); err != nil {
			return &ValidationError{Name: "provider", err: fmt.Errorf(`ent: validator failed for field "ChannelMonitor.provider": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Endpoint(); ok {
		if err := channelmonitor.EndpointValidator(v); err != nil {
			return &ValidationError{Name: "endpoint", err: fmt.Errorf(`ent: validator failed for field "ChannelMonitor.endpoint": %w`, err)}
		}
	}
	if v, ok := _u.mutation.APIKeyEncrypted(); ok {
		if err := channelmonitor.APIKeyEncryptedValidator(v); err != nil {
			return &ValidationError{Name: "api_key_encrypted", err: fmt.Errorf(`ent: validator failed for field "ChannelMonitor.api_key_encrypted": %w`, err)}
		}
	}
	if v, ok := _u.mutation.PrimaryModel(); ok {
		if err := channelmonitor.PrimaryModelValidator(v); err != nil {
			return &ValidationError{Name: "primary_model", err: fmt.Errorf(`ent: validator failed for field "ChannelMonitor.primary_model": %w`, err)}
		}
	}
	if v, ok := _u.mutation.GroupName(); ok {
		if err := channelmonitor.GroupNameValidator(v); err != nil {
			return &ValidationError{Name: "group_name", err: fmt.Errorf(`ent: validator failed for field "ChannelMonitor.group_name": %w`, err)}
		}
	}
	if v, ok := _u.mutation.IntervalSeconds(); ok {
		if err := channelmonitor.IntervalSecondsValidator(v); err != nil {
			return &ValidationError{Name: "interval_seconds", err: fmt.Errorf(`ent: validator failed for field "ChannelMonitor.interval_seconds": %w`, err)}
		}
	}
	return nil
}

func (_u *ChannelMonitorUpdateOne) sqlSave(ctx context.Context) (_node *ChannelMonitor, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(channelmonitor.Table, channelmonitor.Columns, sqlgraph.NewFieldSpec(channelmonitor.FieldID, field.TypeInt64))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "ChannelMonitor.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, channelmonitor.FieldID)
		for _, f := range fields {
			if !channelmonitor.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != channelmonitor.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.UpdatedAt(); ok {
		_spec.SetField(channelmonitor.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.Name(); ok {
		_spec.SetField(channelmonitor.FieldName, field.TypeString, value)
	}
	if value, ok := _u.mutation.Provider(); ok {
		_spec.SetField(channelmonitor.FieldProvider, field.TypeEnum, value)
	}
	if value, ok := _u.mutation.Endpoint(); ok {
		_spec.SetField(channelmonitor.FieldEndpoint, field.TypeString, value)
	}
	if value, ok := _u.mutation.APIKeyEncrypted(); ok {
		_spec.SetField(channelmonitor.FieldAPIKeyEncrypted, field.TypeString, value)
	}
	if value, ok := _u.mutation.PrimaryModel(); ok {
		_spec.SetField(channelmonitor.FieldPrimaryModel, field.TypeString, value)
	}
	if value, ok := _u.mutation.ExtraModels(); ok {
		_spec.SetField(channelmonitor.FieldExtraModels, field.TypeJSON, value)
	}
	if value, ok := _u.mutation.AppendedExtraModels(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, channelmonitor.FieldExtraModels, value)
		})
	}
	if value, ok := _u.mutation.GroupName(); ok {
		_spec.SetField(channelmonitor.FieldGroupName, field.TypeString, value)
	}
	if _u.mutation.GroupNameCleared() {
		_spec.ClearField(channelmonitor.FieldGroupName, field.TypeString)
	}
	if value, ok := _u.mutation.Enabled(); ok {
		_spec.SetField(channelmonitor.FieldEnabled, field.TypeBool, value)
	}
	if value, ok := _u.mutation.IntervalSeconds(); ok {
		_spec.SetField(channelmonitor.FieldIntervalSeconds, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedIntervalSeconds(); ok {
		_spec.AddField(channelmonitor.FieldIntervalSeconds, field.TypeInt, value)
	}
	if value, ok := _u.mutation.LastCheckedAt(); ok {
		_spec.SetField(channelmonitor.FieldLastCheckedAt, field.TypeTime, value)
	}
	if _u.mutation.LastCheckedAtCleared() {
		_spec.ClearField(channelmonitor.FieldLastCheckedAt, field.TypeTime)
	}
	if value, ok := _u.mutation.CreatedBy(); ok {
		_spec.SetField(channelmonitor.FieldCreatedBy, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedCreatedBy(); ok {
		_spec.AddField(channelmonitor.FieldCreatedBy, field.TypeInt64, value)
	}
	if _u.mutation.HistoryCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   channelmonitor.HistoryTable,
			Columns: []string{channelmonitor.HistoryColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(channelmonitorhistory.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedHistoryIDs(); len(nodes) > 0 && !_u.mutation.HistoryCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   channelmonitor.HistoryTable,
			Columns: []string{channelmonitor.HistoryColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(channelmonitorhistory.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.HistoryIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   channelmonitor.HistoryTable,
			Columns: []string{channelmonitor.HistoryColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(channelmonitorhistory.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.DailyRollupsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   channelmonitor.DailyRollupsTable,
			Columns: []string{channelmonitor.DailyRollupsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(channelmonitordailyrollup.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedDailyRollupsIDs(); len(nodes) > 0 && !_u.mutation.DailyRollupsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   channelmonitor.DailyRollupsTable,
			Columns: []string{channelmonitor.DailyRollupsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(channelmonitordailyrollup.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.DailyRollupsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   channelmonitor.DailyRollupsTable,
			Columns: []string{channelmonitor.DailyRollupsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(channelmonitordailyrollup.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &ChannelMonitor{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{channelmonitor.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
