// Code generated by entc, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"

	"code.icod.de/postfix/manager/ent/account"
	"code.icod.de/postfix/manager/ent/domain"
	"code.icod.de/postfix/manager/ent/logentry"
	"code.icod.de/postfix/manager/ent/predicate"
	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
)

// LogentryUpdate is the builder for updating Logentry entities.
type LogentryUpdate struct {
	config
	hooks    []Hook
	mutation *LogentryMutation
}

// Where appends a list predicates to the LogentryUpdate builder.
func (lu *LogentryUpdate) Where(ps ...predicate.Logentry) *LogentryUpdate {
	lu.mutation.Where(ps...)
	return lu
}

// SetAction sets the "action" field.
func (lu *LogentryUpdate) SetAction(s string) *LogentryUpdate {
	lu.mutation.SetAction(s)
	return lu
}

// SetData sets the "data" field.
func (lu *LogentryUpdate) SetData(s string) *LogentryUpdate {
	lu.mutation.SetData(s)
	return lu
}

// SetNillableData sets the "data" field if the given value is not nil.
func (lu *LogentryUpdate) SetNillableData(s *string) *LogentryUpdate {
	if s != nil {
		lu.SetData(*s)
	}
	return lu
}

// ClearData clears the value of the "data" field.
func (lu *LogentryUpdate) ClearData() *LogentryUpdate {
	lu.mutation.ClearData()
	return lu
}

// SetAccountID sets the "account_id" field.
func (lu *LogentryUpdate) SetAccountID(i int64) *LogentryUpdate {
	lu.mutation.SetAccountID(i)
	return lu
}

// SetNillableAccountID sets the "account_id" field if the given value is not nil.
func (lu *LogentryUpdate) SetNillableAccountID(i *int64) *LogentryUpdate {
	if i != nil {
		lu.SetAccountID(*i)
	}
	return lu
}

// ClearAccountID clears the value of the "account_id" field.
func (lu *LogentryUpdate) ClearAccountID() *LogentryUpdate {
	lu.mutation.ClearAccountID()
	return lu
}

// SetDomainID sets the "domain_id" field.
func (lu *LogentryUpdate) SetDomainID(i int64) *LogentryUpdate {
	lu.mutation.SetDomainID(i)
	return lu
}

// SetNillableDomainID sets the "domain_id" field if the given value is not nil.
func (lu *LogentryUpdate) SetNillableDomainID(i *int64) *LogentryUpdate {
	if i != nil {
		lu.SetDomainID(*i)
	}
	return lu
}

// ClearDomainID clears the value of the "domain_id" field.
func (lu *LogentryUpdate) ClearDomainID() *LogentryUpdate {
	lu.mutation.ClearDomainID()
	return lu
}

// SetAccount sets the "account" edge to the Account entity.
func (lu *LogentryUpdate) SetAccount(a *Account) *LogentryUpdate {
	return lu.SetAccountID(a.ID)
}

// SetDomain sets the "domain" edge to the Domain entity.
func (lu *LogentryUpdate) SetDomain(d *Domain) *LogentryUpdate {
	return lu.SetDomainID(d.ID)
}

// Mutation returns the LogentryMutation object of the builder.
func (lu *LogentryUpdate) Mutation() *LogentryMutation {
	return lu.mutation
}

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

// ClearDomain clears the "domain" edge to the Domain entity.
func (lu *LogentryUpdate) ClearDomain() *LogentryUpdate {
	lu.mutation.ClearDomain()
	return lu
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (lu *LogentryUpdate) Save(ctx context.Context) (int, error) {
	var (
		err      error
		affected int
	)
	if len(lu.hooks) == 0 {
		affected, err = lu.sqlSave(ctx)
	} else {
		var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
			mutation, ok := m.(*LogentryMutation)
			if !ok {
				return nil, fmt.Errorf("unexpected mutation type %T", m)
			}
			lu.mutation = mutation
			affected, err = lu.sqlSave(ctx)
			mutation.done = true
			return affected, err
		})
		for i := len(lu.hooks) - 1; i >= 0; i-- {
			if lu.hooks[i] == nil {
				return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
			}
			mut = lu.hooks[i](mut)
		}
		if _, err := mut.Mutate(ctx, lu.mutation); err != nil {
			return 0, err
		}
	}
	return affected, err
}

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

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

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

func (lu *LogentryUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := &sqlgraph.UpdateSpec{
		Node: &sqlgraph.NodeSpec{
			Table:   logentry.Table,
			Columns: logentry.Columns,
			ID: &sqlgraph.FieldSpec{
				Type:   field.TypeInt64,
				Column: logentry.FieldID,
			},
		},
	}
	if ps := lu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := lu.mutation.Action(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeString,
			Value:  value,
			Column: logentry.FieldAction,
		})
	}
	if value, ok := lu.mutation.Data(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeString,
			Value:  value,
			Column: logentry.FieldData,
		})
	}
	if lu.mutation.DataCleared() {
		_spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{
			Type:   field.TypeString,
			Column: logentry.FieldData,
		})
	}
	if lu.mutation.AccountCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   logentry.AccountTable,
			Columns: []string{logentry.AccountColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: &sqlgraph.FieldSpec{
					Type:   field.TypeInt64,
					Column: account.FieldID,
				},
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := lu.mutation.AccountIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   logentry.AccountTable,
			Columns: []string{logentry.AccountColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: &sqlgraph.FieldSpec{
					Type:   field.TypeInt64,
					Column: account.FieldID,
				},
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if lu.mutation.DomainCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   logentry.DomainTable,
			Columns: []string{logentry.DomainColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: &sqlgraph.FieldSpec{
					Type:   field.TypeInt64,
					Column: domain.FieldID,
				},
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := lu.mutation.DomainIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   logentry.DomainTable,
			Columns: []string{logentry.DomainColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: &sqlgraph.FieldSpec{
					Type:   field.TypeInt64,
					Column: domain.FieldID,
				},
			},
		}
		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, lu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{logentry.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{err.Error(), err}
		}
		return 0, err
	}
	return n, nil
}

// LogentryUpdateOne is the builder for updating a single Logentry entity.
type LogentryUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *LogentryMutation
}

// SetAction sets the "action" field.
func (luo *LogentryUpdateOne) SetAction(s string) *LogentryUpdateOne {
	luo.mutation.SetAction(s)
	return luo
}

// SetData sets the "data" field.
func (luo *LogentryUpdateOne) SetData(s string) *LogentryUpdateOne {
	luo.mutation.SetData(s)
	return luo
}

// SetNillableData sets the "data" field if the given value is not nil.
func (luo *LogentryUpdateOne) SetNillableData(s *string) *LogentryUpdateOne {
	if s != nil {
		luo.SetData(*s)
	}
	return luo
}

// ClearData clears the value of the "data" field.
func (luo *LogentryUpdateOne) ClearData() *LogentryUpdateOne {
	luo.mutation.ClearData()
	return luo
}

// SetAccountID sets the "account_id" field.
func (luo *LogentryUpdateOne) SetAccountID(i int64) *LogentryUpdateOne {
	luo.mutation.SetAccountID(i)
	return luo
}

// SetNillableAccountID sets the "account_id" field if the given value is not nil.
func (luo *LogentryUpdateOne) SetNillableAccountID(i *int64) *LogentryUpdateOne {
	if i != nil {
		luo.SetAccountID(*i)
	}
	return luo
}

// ClearAccountID clears the value of the "account_id" field.
func (luo *LogentryUpdateOne) ClearAccountID() *LogentryUpdateOne {
	luo.mutation.ClearAccountID()
	return luo
}

// SetDomainID sets the "domain_id" field.
func (luo *LogentryUpdateOne) SetDomainID(i int64) *LogentryUpdateOne {
	luo.mutation.SetDomainID(i)
	return luo
}

// SetNillableDomainID sets the "domain_id" field if the given value is not nil.
func (luo *LogentryUpdateOne) SetNillableDomainID(i *int64) *LogentryUpdateOne {
	if i != nil {
		luo.SetDomainID(*i)
	}
	return luo
}

// ClearDomainID clears the value of the "domain_id" field.
func (luo *LogentryUpdateOne) ClearDomainID() *LogentryUpdateOne {
	luo.mutation.ClearDomainID()
	return luo
}

// SetAccount sets the "account" edge to the Account entity.
func (luo *LogentryUpdateOne) SetAccount(a *Account) *LogentryUpdateOne {
	return luo.SetAccountID(a.ID)
}

// SetDomain sets the "domain" edge to the Domain entity.
func (luo *LogentryUpdateOne) SetDomain(d *Domain) *LogentryUpdateOne {
	return luo.SetDomainID(d.ID)
}

// Mutation returns the LogentryMutation object of the builder.
func (luo *LogentryUpdateOne) Mutation() *LogentryMutation {
	return luo.mutation
}

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

// ClearDomain clears the "domain" edge to the Domain entity.
func (luo *LogentryUpdateOne) ClearDomain() *LogentryUpdateOne {
	luo.mutation.ClearDomain()
	return luo
}

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

// Save executes the query and returns the updated Logentry entity.
func (luo *LogentryUpdateOne) Save(ctx context.Context) (*Logentry, error) {
	var (
		err  error
		node *Logentry
	)
	if len(luo.hooks) == 0 {
		node, err = luo.sqlSave(ctx)
	} else {
		var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
			mutation, ok := m.(*LogentryMutation)
			if !ok {
				return nil, fmt.Errorf("unexpected mutation type %T", m)
			}
			luo.mutation = mutation
			node, err = luo.sqlSave(ctx)
			mutation.done = true
			return node, err
		})
		for i := len(luo.hooks) - 1; i >= 0; i-- {
			if luo.hooks[i] == nil {
				return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
			}
			mut = luo.hooks[i](mut)
		}
		if _, err := mut.Mutate(ctx, luo.mutation); err != nil {
			return nil, err
		}
	}
	return node, err
}

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

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

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

func (luo *LogentryUpdateOne) sqlSave(ctx context.Context) (_node *Logentry, err error) {
	_spec := &sqlgraph.UpdateSpec{
		Node: &sqlgraph.NodeSpec{
			Table:   logentry.Table,
			Columns: logentry.Columns,
			ID: &sqlgraph.FieldSpec{
				Type:   field.TypeInt64,
				Column: logentry.FieldID,
			},
		},
	}
	id, ok := luo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Logentry.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := luo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, logentry.FieldID)
		for _, f := range fields {
			if !logentry.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != logentry.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := luo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := luo.mutation.Action(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeString,
			Value:  value,
			Column: logentry.FieldAction,
		})
	}
	if value, ok := luo.mutation.Data(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeString,
			Value:  value,
			Column: logentry.FieldData,
		})
	}
	if luo.mutation.DataCleared() {
		_spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{
			Type:   field.TypeString,
			Column: logentry.FieldData,
		})
	}
	if luo.mutation.AccountCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   logentry.AccountTable,
			Columns: []string{logentry.AccountColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: &sqlgraph.FieldSpec{
					Type:   field.TypeInt64,
					Column: account.FieldID,
				},
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := luo.mutation.AccountIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   logentry.AccountTable,
			Columns: []string{logentry.AccountColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: &sqlgraph.FieldSpec{
					Type:   field.TypeInt64,
					Column: account.FieldID,
				},
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if luo.mutation.DomainCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   logentry.DomainTable,
			Columns: []string{logentry.DomainColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: &sqlgraph.FieldSpec{
					Type:   field.TypeInt64,
					Column: domain.FieldID,
				},
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := luo.mutation.DomainIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   logentry.DomainTable,
			Columns: []string{logentry.DomainColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: &sqlgraph.FieldSpec{
					Type:   field.TypeInt64,
					Column: domain.FieldID,
				},
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &Logentry{config: luo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, luo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{logentry.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{err.Error(), err}
		}
		return nil, err
	}
	return _node, nil
}
