// Code generated by entc, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"

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

// AliasUpdate is the builder for updating Alias entities.
type AliasUpdate struct {
	config
	hooks    []Hook
	mutation *AliasMutation
}

// Where appends a list predicates to the AliasUpdate builder.
func (au *AliasUpdate) Where(ps ...predicate.Alias) *AliasUpdate {
	au.mutation.Where(ps...)
	return au
}

// SetModified sets the "modified" field.
func (au *AliasUpdate) SetModified(t time.Time) *AliasUpdate {
	au.mutation.SetModified(t)
	return au
}

// ClearModified clears the value of the "modified" field.
func (au *AliasUpdate) ClearModified() *AliasUpdate {
	au.mutation.ClearModified()
	return au
}

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

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

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

// SetGoto sets the "goto" field.
func (au *AliasUpdate) SetGoto(s string) *AliasUpdate {
	au.mutation.SetGoto(s)
	return au
}

// SetActive sets the "active" field.
func (au *AliasUpdate) SetActive(b bool) *AliasUpdate {
	au.mutation.SetActive(b)
	return au
}

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

// Mutation returns the AliasMutation object of the builder.
func (au *AliasUpdate) Mutation() *AliasMutation {
	return au.mutation
}

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

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

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

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

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

// defaults sets the default values of the builder before save.
func (au *AliasUpdate) defaults() {
	if _, ok := au.mutation.Modified(); !ok && !au.mutation.ModifiedCleared() {
		v := alias.UpdateDefaultModified()
		au.mutation.SetModified(v)
	}
}

func (au *AliasUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := &sqlgraph.UpdateSpec{
		Node: &sqlgraph.NodeSpec{
			Table:   alias.Table,
			Columns: alias.Columns,
			ID: &sqlgraph.FieldSpec{
				Type:   field.TypeInt64,
				Column: alias.FieldID,
			},
		},
	}
	if ps := au.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := au.mutation.Modified(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeTime,
			Value:  value,
			Column: alias.FieldModified,
		})
	}
	if au.mutation.ModifiedCleared() {
		_spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{
			Type:   field.TypeTime,
			Column: alias.FieldModified,
		})
	}
	if value, ok := au.mutation.Goto(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeString,
			Value:  value,
			Column: alias.FieldGoto,
		})
	}
	if value, ok := au.mutation.Active(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeBool,
			Value:  value,
			Column: alias.FieldActive,
		})
	}
	if au.mutation.DomainCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   alias.DomainTable,
			Columns: []string{alias.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 := au.mutation.DomainIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   alias.DomainTable,
			Columns: []string{alias.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, au.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{alias.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{err.Error(), err}
		}
		return 0, err
	}
	return n, nil
}

// AliasUpdateOne is the builder for updating a single Alias entity.
type AliasUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *AliasMutation
}

// SetModified sets the "modified" field.
func (auo *AliasUpdateOne) SetModified(t time.Time) *AliasUpdateOne {
	auo.mutation.SetModified(t)
	return auo
}

// ClearModified clears the value of the "modified" field.
func (auo *AliasUpdateOne) ClearModified() *AliasUpdateOne {
	auo.mutation.ClearModified()
	return auo
}

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

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

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

// SetGoto sets the "goto" field.
func (auo *AliasUpdateOne) SetGoto(s string) *AliasUpdateOne {
	auo.mutation.SetGoto(s)
	return auo
}

// SetActive sets the "active" field.
func (auo *AliasUpdateOne) SetActive(b bool) *AliasUpdateOne {
	auo.mutation.SetActive(b)
	return auo
}

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

// Mutation returns the AliasMutation object of the builder.
func (auo *AliasUpdateOne) Mutation() *AliasMutation {
	return auo.mutation
}

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

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

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

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

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

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

// defaults sets the default values of the builder before save.
func (auo *AliasUpdateOne) defaults() {
	if _, ok := auo.mutation.Modified(); !ok && !auo.mutation.ModifiedCleared() {
		v := alias.UpdateDefaultModified()
		auo.mutation.SetModified(v)
	}
}

func (auo *AliasUpdateOne) sqlSave(ctx context.Context) (_node *Alias, err error) {
	_spec := &sqlgraph.UpdateSpec{
		Node: &sqlgraph.NodeSpec{
			Table:   alias.Table,
			Columns: alias.Columns,
			ID: &sqlgraph.FieldSpec{
				Type:   field.TypeInt64,
				Column: alias.FieldID,
			},
		},
	}
	id, ok := auo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Alias.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := auo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, alias.FieldID)
		for _, f := range fields {
			if !alias.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != alias.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := auo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := auo.mutation.Modified(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeTime,
			Value:  value,
			Column: alias.FieldModified,
		})
	}
	if auo.mutation.ModifiedCleared() {
		_spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{
			Type:   field.TypeTime,
			Column: alias.FieldModified,
		})
	}
	if value, ok := auo.mutation.Goto(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeString,
			Value:  value,
			Column: alias.FieldGoto,
		})
	}
	if value, ok := auo.mutation.Active(); ok {
		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
			Type:   field.TypeBool,
			Value:  value,
			Column: alias.FieldActive,
		})
	}
	if auo.mutation.DomainCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   alias.DomainTable,
			Columns: []string{alias.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 := auo.mutation.DomainIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   alias.DomainTable,
			Columns: []string{alias.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 = &Alias{config: auo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, auo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{alias.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{err.Error(), err}
		}
		return nil, err
	}
	return _node, nil
}
