// 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/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/group"
	"github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/groupmembership"
	"github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/predicate"
	"github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/user"
	"github.com/google/uuid"
)

// GroupMembershipUpdate is the builder for updating GroupMembership entities.
type GroupMembershipUpdate struct {
	config
	hooks     []Hook
	mutation  *GroupMembershipMutation
	modifiers []func(*sql.UpdateBuilder)
}

// Where appends a list predicates to the GroupMembershipUpdate builder.
func (gmu *GroupMembershipUpdate) Where(ps ...predicate.GroupMembership) *GroupMembershipUpdate {
	gmu.mutation.Where(ps...)
	return gmu
}

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

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

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

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

// SetMaintainer sets the "maintainer" field.
func (gmu *GroupMembershipUpdate) SetMaintainer(b bool) *GroupMembershipUpdate {
	gmu.mutation.SetMaintainer(b)
	return gmu
}

// SetNillableMaintainer sets the "maintainer" field if the given value is not nil.
func (gmu *GroupMembershipUpdate) SetNillableMaintainer(b *bool) *GroupMembershipUpdate {
	if b != nil {
		gmu.SetMaintainer(*b)
	}
	return gmu
}

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

// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (gmu *GroupMembershipUpdate) SetNillableUpdatedAt(t *time.Time) *GroupMembershipUpdate {
	if t != nil {
		gmu.SetUpdatedAt(*t)
	}
	return gmu
}

// SetDeletedAt sets the "deleted_at" field.
func (gmu *GroupMembershipUpdate) SetDeletedAt(t time.Time) *GroupMembershipUpdate {
	gmu.mutation.SetDeletedAt(t)
	return gmu
}

// SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
func (gmu *GroupMembershipUpdate) SetNillableDeletedAt(t *time.Time) *GroupMembershipUpdate {
	if t != nil {
		gmu.SetDeletedAt(*t)
	}
	return gmu
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (gmu *GroupMembershipUpdate) ClearDeletedAt() *GroupMembershipUpdate {
	gmu.mutation.ClearDeletedAt()
	return gmu
}

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

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

// Mutation returns the GroupMembershipMutation object of the builder.
func (gmu *GroupMembershipUpdate) Mutation() *GroupMembershipMutation {
	return gmu.mutation
}

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

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (gmu *GroupMembershipUpdate) check() error {
	if gmu.mutation.GroupCleared() && len(gmu.mutation.GroupIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "GroupMembership.group"`)
	}
	if gmu.mutation.UserCleared() && len(gmu.mutation.UserIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "GroupMembership.user"`)
	}
	return nil
}

// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (gmu *GroupMembershipUpdate) Modify(modifiers ...func(u *sql.UpdateBuilder)) *GroupMembershipUpdate {
	gmu.modifiers = append(gmu.modifiers, modifiers...)
	return gmu
}

func (gmu *GroupMembershipUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := gmu.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(groupmembership.Table, groupmembership.Columns, sqlgraph.NewFieldSpec(groupmembership.FieldID, field.TypeUUID))
	if ps := gmu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := gmu.mutation.Maintainer(); ok {
		_spec.SetField(groupmembership.FieldMaintainer, field.TypeBool, value)
	}
	if value, ok := gmu.mutation.UpdatedAt(); ok {
		_spec.SetField(groupmembership.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := gmu.mutation.DeletedAt(); ok {
		_spec.SetField(groupmembership.FieldDeletedAt, field.TypeTime, value)
	}
	if gmu.mutation.DeletedAtCleared() {
		_spec.ClearField(groupmembership.FieldDeletedAt, field.TypeTime)
	}
	if gmu.mutation.GroupCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   groupmembership.GroupTable,
			Columns: []string{groupmembership.GroupColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := gmu.mutation.GroupIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   groupmembership.GroupTable,
			Columns: []string{groupmembership.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 gmu.mutation.UserCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   groupmembership.UserTable,
			Columns: []string{groupmembership.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := gmu.mutation.UserIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   groupmembership.UserTable,
			Columns: []string{groupmembership.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)
	}
	_spec.AddModifiers(gmu.modifiers...)
	if n, err = sqlgraph.UpdateNodes(ctx, gmu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{groupmembership.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	gmu.mutation.done = true
	return n, nil
}

// GroupMembershipUpdateOne is the builder for updating a single GroupMembership entity.
type GroupMembershipUpdateOne struct {
	config
	fields    []string
	hooks     []Hook
	mutation  *GroupMembershipMutation
	modifiers []func(*sql.UpdateBuilder)
}

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

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

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

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

// SetMaintainer sets the "maintainer" field.
func (gmuo *GroupMembershipUpdateOne) SetMaintainer(b bool) *GroupMembershipUpdateOne {
	gmuo.mutation.SetMaintainer(b)
	return gmuo
}

// SetNillableMaintainer sets the "maintainer" field if the given value is not nil.
func (gmuo *GroupMembershipUpdateOne) SetNillableMaintainer(b *bool) *GroupMembershipUpdateOne {
	if b != nil {
		gmuo.SetMaintainer(*b)
	}
	return gmuo
}

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

// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (gmuo *GroupMembershipUpdateOne) SetNillableUpdatedAt(t *time.Time) *GroupMembershipUpdateOne {
	if t != nil {
		gmuo.SetUpdatedAt(*t)
	}
	return gmuo
}

// SetDeletedAt sets the "deleted_at" field.
func (gmuo *GroupMembershipUpdateOne) SetDeletedAt(t time.Time) *GroupMembershipUpdateOne {
	gmuo.mutation.SetDeletedAt(t)
	return gmuo
}

// SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
func (gmuo *GroupMembershipUpdateOne) SetNillableDeletedAt(t *time.Time) *GroupMembershipUpdateOne {
	if t != nil {
		gmuo.SetDeletedAt(*t)
	}
	return gmuo
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (gmuo *GroupMembershipUpdateOne) ClearDeletedAt() *GroupMembershipUpdateOne {
	gmuo.mutation.ClearDeletedAt()
	return gmuo
}

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

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

// Mutation returns the GroupMembershipMutation object of the builder.
func (gmuo *GroupMembershipUpdateOne) Mutation() *GroupMembershipMutation {
	return gmuo.mutation
}

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

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

// Where appends a list predicates to the GroupMembershipUpdate builder.
func (gmuo *GroupMembershipUpdateOne) Where(ps ...predicate.GroupMembership) *GroupMembershipUpdateOne {
	gmuo.mutation.Where(ps...)
	return gmuo
}

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

// Save executes the query and returns the updated GroupMembership entity.
func (gmuo *GroupMembershipUpdateOne) Save(ctx context.Context) (*GroupMembership, error) {
	return withHooks(ctx, gmuo.sqlSave, gmuo.mutation, gmuo.hooks)
}

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

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

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

// check runs all checks and user-defined validators on the builder.
func (gmuo *GroupMembershipUpdateOne) check() error {
	if gmuo.mutation.GroupCleared() && len(gmuo.mutation.GroupIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "GroupMembership.group"`)
	}
	if gmuo.mutation.UserCleared() && len(gmuo.mutation.UserIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "GroupMembership.user"`)
	}
	return nil
}

// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (gmuo *GroupMembershipUpdateOne) Modify(modifiers ...func(u *sql.UpdateBuilder)) *GroupMembershipUpdateOne {
	gmuo.modifiers = append(gmuo.modifiers, modifiers...)
	return gmuo
}

func (gmuo *GroupMembershipUpdateOne) sqlSave(ctx context.Context) (_node *GroupMembership, err error) {
	if err := gmuo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(groupmembership.Table, groupmembership.Columns, sqlgraph.NewFieldSpec(groupmembership.FieldID, field.TypeUUID))
	id, ok := gmuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "GroupMembership.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := gmuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, groupmembership.FieldID)
		for _, f := range fields {
			if !groupmembership.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != groupmembership.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := gmuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := gmuo.mutation.Maintainer(); ok {
		_spec.SetField(groupmembership.FieldMaintainer, field.TypeBool, value)
	}
	if value, ok := gmuo.mutation.UpdatedAt(); ok {
		_spec.SetField(groupmembership.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := gmuo.mutation.DeletedAt(); ok {
		_spec.SetField(groupmembership.FieldDeletedAt, field.TypeTime, value)
	}
	if gmuo.mutation.DeletedAtCleared() {
		_spec.ClearField(groupmembership.FieldDeletedAt, field.TypeTime)
	}
	if gmuo.mutation.GroupCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   groupmembership.GroupTable,
			Columns: []string{groupmembership.GroupColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := gmuo.mutation.GroupIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   groupmembership.GroupTable,
			Columns: []string{groupmembership.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 gmuo.mutation.UserCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   groupmembership.UserTable,
			Columns: []string{groupmembership.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := gmuo.mutation.UserIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   groupmembership.UserTable,
			Columns: []string{groupmembership.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)
	}
	_spec.AddModifiers(gmuo.modifiers...)
	_node = &GroupMembership{config: gmuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, gmuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{groupmembership.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	gmuo.mutation.done = true
	return _node, nil
}
