// 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/Wei-Shaw/sub2api/ent/authidentity"
	"github.com/Wei-Shaw/sub2api/ent/identityadoptiondecision"
	"github.com/Wei-Shaw/sub2api/ent/pendingauthsession"
	"github.com/Wei-Shaw/sub2api/ent/predicate"
)

// IdentityAdoptionDecisionUpdate is the builder for updating IdentityAdoptionDecision entities.
type IdentityAdoptionDecisionUpdate struct {
	config
	hooks    []Hook
	mutation *IdentityAdoptionDecisionMutation
}

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

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

// SetPendingAuthSessionID sets the "pending_auth_session_id" field.
func (_u *IdentityAdoptionDecisionUpdate) SetPendingAuthSessionID(v int64) *IdentityAdoptionDecisionUpdate {
	_u.mutation.SetPendingAuthSessionID(v)
	return _u
}

// SetNillablePendingAuthSessionID sets the "pending_auth_session_id" field if the given value is not nil.
func (_u *IdentityAdoptionDecisionUpdate) SetNillablePendingAuthSessionID(v *int64) *IdentityAdoptionDecisionUpdate {
	if v != nil {
		_u.SetPendingAuthSessionID(*v)
	}
	return _u
}

// SetIdentityID sets the "identity_id" field.
func (_u *IdentityAdoptionDecisionUpdate) SetIdentityID(v int64) *IdentityAdoptionDecisionUpdate {
	_u.mutation.SetIdentityID(v)
	return _u
}

// SetNillableIdentityID sets the "identity_id" field if the given value is not nil.
func (_u *IdentityAdoptionDecisionUpdate) SetNillableIdentityID(v *int64) *IdentityAdoptionDecisionUpdate {
	if v != nil {
		_u.SetIdentityID(*v)
	}
	return _u
}

// ClearIdentityID clears the value of the "identity_id" field.
func (_u *IdentityAdoptionDecisionUpdate) ClearIdentityID() *IdentityAdoptionDecisionUpdate {
	_u.mutation.ClearIdentityID()
	return _u
}

// SetAdoptDisplayName sets the "adopt_display_name" field.
func (_u *IdentityAdoptionDecisionUpdate) SetAdoptDisplayName(v bool) *IdentityAdoptionDecisionUpdate {
	_u.mutation.SetAdoptDisplayName(v)
	return _u
}

// SetNillableAdoptDisplayName sets the "adopt_display_name" field if the given value is not nil.
func (_u *IdentityAdoptionDecisionUpdate) SetNillableAdoptDisplayName(v *bool) *IdentityAdoptionDecisionUpdate {
	if v != nil {
		_u.SetAdoptDisplayName(*v)
	}
	return _u
}

// SetAdoptAvatar sets the "adopt_avatar" field.
func (_u *IdentityAdoptionDecisionUpdate) SetAdoptAvatar(v bool) *IdentityAdoptionDecisionUpdate {
	_u.mutation.SetAdoptAvatar(v)
	return _u
}

// SetNillableAdoptAvatar sets the "adopt_avatar" field if the given value is not nil.
func (_u *IdentityAdoptionDecisionUpdate) SetNillableAdoptAvatar(v *bool) *IdentityAdoptionDecisionUpdate {
	if v != nil {
		_u.SetAdoptAvatar(*v)
	}
	return _u
}

// SetPendingAuthSession sets the "pending_auth_session" edge to the PendingAuthSession entity.
func (_u *IdentityAdoptionDecisionUpdate) SetPendingAuthSession(v *PendingAuthSession) *IdentityAdoptionDecisionUpdate {
	return _u.SetPendingAuthSessionID(v.ID)
}

// SetIdentity sets the "identity" edge to the AuthIdentity entity.
func (_u *IdentityAdoptionDecisionUpdate) SetIdentity(v *AuthIdentity) *IdentityAdoptionDecisionUpdate {
	return _u.SetIdentityID(v.ID)
}

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

// ClearPendingAuthSession clears the "pending_auth_session" edge to the PendingAuthSession entity.
func (_u *IdentityAdoptionDecisionUpdate) ClearPendingAuthSession() *IdentityAdoptionDecisionUpdate {
	_u.mutation.ClearPendingAuthSession()
	return _u
}

// ClearIdentity clears the "identity" edge to the AuthIdentity entity.
func (_u *IdentityAdoptionDecisionUpdate) ClearIdentity() *IdentityAdoptionDecisionUpdate {
	_u.mutation.ClearIdentity()
	return _u
}

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

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

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

// check runs all checks and user-defined validators on the builder.
func (_u *IdentityAdoptionDecisionUpdate) check() error {
	if _u.mutation.PendingAuthSessionCleared() && len(_u.mutation.PendingAuthSessionIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "IdentityAdoptionDecision.pending_auth_session"`)
	}
	return nil
}

func (_u *IdentityAdoptionDecisionUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(identityadoptiondecision.Table, identityadoptiondecision.Columns, sqlgraph.NewFieldSpec(identityadoptiondecision.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(identityadoptiondecision.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.AdoptDisplayName(); ok {
		_spec.SetField(identityadoptiondecision.FieldAdoptDisplayName, field.TypeBool, value)
	}
	if value, ok := _u.mutation.AdoptAvatar(); ok {
		_spec.SetField(identityadoptiondecision.FieldAdoptAvatar, field.TypeBool, value)
	}
	if _u.mutation.PendingAuthSessionCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   identityadoptiondecision.PendingAuthSessionTable,
			Columns: []string{identityadoptiondecision.PendingAuthSessionColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(pendingauthsession.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.PendingAuthSessionIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   identityadoptiondecision.PendingAuthSessionTable,
			Columns: []string{identityadoptiondecision.PendingAuthSessionColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(pendingauthsession.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.IdentityCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   identityadoptiondecision.IdentityTable,
			Columns: []string{identityadoptiondecision.IdentityColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(authidentity.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.IdentityIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   identityadoptiondecision.IdentityTable,
			Columns: []string{identityadoptiondecision.IdentityColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(authidentity.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{identityadoptiondecision.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// IdentityAdoptionDecisionUpdateOne is the builder for updating a single IdentityAdoptionDecision entity.
type IdentityAdoptionDecisionUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *IdentityAdoptionDecisionMutation
}

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

// SetPendingAuthSessionID sets the "pending_auth_session_id" field.
func (_u *IdentityAdoptionDecisionUpdateOne) SetPendingAuthSessionID(v int64) *IdentityAdoptionDecisionUpdateOne {
	_u.mutation.SetPendingAuthSessionID(v)
	return _u
}

// SetNillablePendingAuthSessionID sets the "pending_auth_session_id" field if the given value is not nil.
func (_u *IdentityAdoptionDecisionUpdateOne) SetNillablePendingAuthSessionID(v *int64) *IdentityAdoptionDecisionUpdateOne {
	if v != nil {
		_u.SetPendingAuthSessionID(*v)
	}
	return _u
}

// SetIdentityID sets the "identity_id" field.
func (_u *IdentityAdoptionDecisionUpdateOne) SetIdentityID(v int64) *IdentityAdoptionDecisionUpdateOne {
	_u.mutation.SetIdentityID(v)
	return _u
}

// SetNillableIdentityID sets the "identity_id" field if the given value is not nil.
func (_u *IdentityAdoptionDecisionUpdateOne) SetNillableIdentityID(v *int64) *IdentityAdoptionDecisionUpdateOne {
	if v != nil {
		_u.SetIdentityID(*v)
	}
	return _u
}

// ClearIdentityID clears the value of the "identity_id" field.
func (_u *IdentityAdoptionDecisionUpdateOne) ClearIdentityID() *IdentityAdoptionDecisionUpdateOne {
	_u.mutation.ClearIdentityID()
	return _u
}

// SetAdoptDisplayName sets the "adopt_display_name" field.
func (_u *IdentityAdoptionDecisionUpdateOne) SetAdoptDisplayName(v bool) *IdentityAdoptionDecisionUpdateOne {
	_u.mutation.SetAdoptDisplayName(v)
	return _u
}

// SetNillableAdoptDisplayName sets the "adopt_display_name" field if the given value is not nil.
func (_u *IdentityAdoptionDecisionUpdateOne) SetNillableAdoptDisplayName(v *bool) *IdentityAdoptionDecisionUpdateOne {
	if v != nil {
		_u.SetAdoptDisplayName(*v)
	}
	return _u
}

// SetAdoptAvatar sets the "adopt_avatar" field.
func (_u *IdentityAdoptionDecisionUpdateOne) SetAdoptAvatar(v bool) *IdentityAdoptionDecisionUpdateOne {
	_u.mutation.SetAdoptAvatar(v)
	return _u
}

// SetNillableAdoptAvatar sets the "adopt_avatar" field if the given value is not nil.
func (_u *IdentityAdoptionDecisionUpdateOne) SetNillableAdoptAvatar(v *bool) *IdentityAdoptionDecisionUpdateOne {
	if v != nil {
		_u.SetAdoptAvatar(*v)
	}
	return _u
}

// SetPendingAuthSession sets the "pending_auth_session" edge to the PendingAuthSession entity.
func (_u *IdentityAdoptionDecisionUpdateOne) SetPendingAuthSession(v *PendingAuthSession) *IdentityAdoptionDecisionUpdateOne {
	return _u.SetPendingAuthSessionID(v.ID)
}

// SetIdentity sets the "identity" edge to the AuthIdentity entity.
func (_u *IdentityAdoptionDecisionUpdateOne) SetIdentity(v *AuthIdentity) *IdentityAdoptionDecisionUpdateOne {
	return _u.SetIdentityID(v.ID)
}

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

// ClearPendingAuthSession clears the "pending_auth_session" edge to the PendingAuthSession entity.
func (_u *IdentityAdoptionDecisionUpdateOne) ClearPendingAuthSession() *IdentityAdoptionDecisionUpdateOne {
	_u.mutation.ClearPendingAuthSession()
	return _u
}

// ClearIdentity clears the "identity" edge to the AuthIdentity entity.
func (_u *IdentityAdoptionDecisionUpdateOne) ClearIdentity() *IdentityAdoptionDecisionUpdateOne {
	_u.mutation.ClearIdentity()
	return _u
}

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (_u *IdentityAdoptionDecisionUpdateOne) check() error {
	if _u.mutation.PendingAuthSessionCleared() && len(_u.mutation.PendingAuthSessionIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "IdentityAdoptionDecision.pending_auth_session"`)
	}
	return nil
}

func (_u *IdentityAdoptionDecisionUpdateOne) sqlSave(ctx context.Context) (_node *IdentityAdoptionDecision, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(identityadoptiondecision.Table, identityadoptiondecision.Columns, sqlgraph.NewFieldSpec(identityadoptiondecision.FieldID, field.TypeInt64))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "IdentityAdoptionDecision.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, identityadoptiondecision.FieldID)
		for _, f := range fields {
			if !identityadoptiondecision.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != identityadoptiondecision.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(identityadoptiondecision.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.AdoptDisplayName(); ok {
		_spec.SetField(identityadoptiondecision.FieldAdoptDisplayName, field.TypeBool, value)
	}
	if value, ok := _u.mutation.AdoptAvatar(); ok {
		_spec.SetField(identityadoptiondecision.FieldAdoptAvatar, field.TypeBool, value)
	}
	if _u.mutation.PendingAuthSessionCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   identityadoptiondecision.PendingAuthSessionTable,
			Columns: []string{identityadoptiondecision.PendingAuthSessionColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(pendingauthsession.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.PendingAuthSessionIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   identityadoptiondecision.PendingAuthSessionTable,
			Columns: []string{identityadoptiondecision.PendingAuthSessionColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(pendingauthsession.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.IdentityCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   identityadoptiondecision.IdentityTable,
			Columns: []string{identityadoptiondecision.IdentityColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(authidentity.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.IdentityIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   identityadoptiondecision.IdentityTable,
			Columns: []string{identityadoptiondecision.IdentityColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(authidentity.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &IdentityAdoptionDecision{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{identityadoptiondecision.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
