// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"encoding/json"
	"errors"
	"fmt"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/dialect/sql/sqljson"
	"entgo.io/ent/schema/field"
	"github.com/buildbarn/bb-portal/ent/gen/ent/bazelinvocation"
	"github.com/buildbarn/bb-portal/ent/gen/ent/bazelinvocationproblem"
	"github.com/buildbarn/bb-portal/ent/gen/ent/predicate"
)

// BazelInvocationProblemUpdate is the builder for updating BazelInvocationProblem entities.
type BazelInvocationProblemUpdate struct {
	config
	hooks    []Hook
	mutation *BazelInvocationProblemMutation
}

// Where appends a list predicates to the BazelInvocationProblemUpdate builder.
func (bipu *BazelInvocationProblemUpdate) Where(ps ...predicate.BazelInvocationProblem) *BazelInvocationProblemUpdate {
	bipu.mutation.Where(ps...)
	return bipu
}

// SetProblemType sets the "problem_type" field.
func (bipu *BazelInvocationProblemUpdate) SetProblemType(s string) *BazelInvocationProblemUpdate {
	bipu.mutation.SetProblemType(s)
	return bipu
}

// SetNillableProblemType sets the "problem_type" field if the given value is not nil.
func (bipu *BazelInvocationProblemUpdate) SetNillableProblemType(s *string) *BazelInvocationProblemUpdate {
	if s != nil {
		bipu.SetProblemType(*s)
	}
	return bipu
}

// SetLabel sets the "label" field.
func (bipu *BazelInvocationProblemUpdate) SetLabel(s string) *BazelInvocationProblemUpdate {
	bipu.mutation.SetLabel(s)
	return bipu
}

// SetNillableLabel sets the "label" field if the given value is not nil.
func (bipu *BazelInvocationProblemUpdate) SetNillableLabel(s *string) *BazelInvocationProblemUpdate {
	if s != nil {
		bipu.SetLabel(*s)
	}
	return bipu
}

// SetBepEvents sets the "bep_events" field.
func (bipu *BazelInvocationProblemUpdate) SetBepEvents(jm json.RawMessage) *BazelInvocationProblemUpdate {
	bipu.mutation.SetBepEvents(jm)
	return bipu
}

// AppendBepEvents appends jm to the "bep_events" field.
func (bipu *BazelInvocationProblemUpdate) AppendBepEvents(jm json.RawMessage) *BazelInvocationProblemUpdate {
	bipu.mutation.AppendBepEvents(jm)
	return bipu
}

// SetBazelInvocationID sets the "bazel_invocation" edge to the BazelInvocation entity by ID.
func (bipu *BazelInvocationProblemUpdate) SetBazelInvocationID(id int) *BazelInvocationProblemUpdate {
	bipu.mutation.SetBazelInvocationID(id)
	return bipu
}

// SetNillableBazelInvocationID sets the "bazel_invocation" edge to the BazelInvocation entity by ID if the given value is not nil.
func (bipu *BazelInvocationProblemUpdate) SetNillableBazelInvocationID(id *int) *BazelInvocationProblemUpdate {
	if id != nil {
		bipu = bipu.SetBazelInvocationID(*id)
	}
	return bipu
}

// SetBazelInvocation sets the "bazel_invocation" edge to the BazelInvocation entity.
func (bipu *BazelInvocationProblemUpdate) SetBazelInvocation(b *BazelInvocation) *BazelInvocationProblemUpdate {
	return bipu.SetBazelInvocationID(b.ID)
}

// Mutation returns the BazelInvocationProblemMutation object of the builder.
func (bipu *BazelInvocationProblemUpdate) Mutation() *BazelInvocationProblemMutation {
	return bipu.mutation
}

// ClearBazelInvocation clears the "bazel_invocation" edge to the BazelInvocation entity.
func (bipu *BazelInvocationProblemUpdate) ClearBazelInvocation() *BazelInvocationProblemUpdate {
	bipu.mutation.ClearBazelInvocation()
	return bipu
}

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

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

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

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

func (bipu *BazelInvocationProblemUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(bazelinvocationproblem.Table, bazelinvocationproblem.Columns, sqlgraph.NewFieldSpec(bazelinvocationproblem.FieldID, field.TypeInt))
	if ps := bipu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := bipu.mutation.ProblemType(); ok {
		_spec.SetField(bazelinvocationproblem.FieldProblemType, field.TypeString, value)
	}
	if value, ok := bipu.mutation.Label(); ok {
		_spec.SetField(bazelinvocationproblem.FieldLabel, field.TypeString, value)
	}
	if value, ok := bipu.mutation.BepEvents(); ok {
		_spec.SetField(bazelinvocationproblem.FieldBepEvents, field.TypeJSON, value)
	}
	if value, ok := bipu.mutation.AppendedBepEvents(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, bazelinvocationproblem.FieldBepEvents, value)
		})
	}
	if bipu.mutation.BazelInvocationCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   bazelinvocationproblem.BazelInvocationTable,
			Columns: []string{bazelinvocationproblem.BazelInvocationColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(bazelinvocation.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := bipu.mutation.BazelInvocationIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   bazelinvocationproblem.BazelInvocationTable,
			Columns: []string{bazelinvocationproblem.BazelInvocationColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(bazelinvocation.FieldID, field.TypeInt),
			},
		}
		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, bipu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{bazelinvocationproblem.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	bipu.mutation.done = true
	return n, nil
}

// BazelInvocationProblemUpdateOne is the builder for updating a single BazelInvocationProblem entity.
type BazelInvocationProblemUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *BazelInvocationProblemMutation
}

// SetProblemType sets the "problem_type" field.
func (bipuo *BazelInvocationProblemUpdateOne) SetProblemType(s string) *BazelInvocationProblemUpdateOne {
	bipuo.mutation.SetProblemType(s)
	return bipuo
}

// SetNillableProblemType sets the "problem_type" field if the given value is not nil.
func (bipuo *BazelInvocationProblemUpdateOne) SetNillableProblemType(s *string) *BazelInvocationProblemUpdateOne {
	if s != nil {
		bipuo.SetProblemType(*s)
	}
	return bipuo
}

// SetLabel sets the "label" field.
func (bipuo *BazelInvocationProblemUpdateOne) SetLabel(s string) *BazelInvocationProblemUpdateOne {
	bipuo.mutation.SetLabel(s)
	return bipuo
}

// SetNillableLabel sets the "label" field if the given value is not nil.
func (bipuo *BazelInvocationProblemUpdateOne) SetNillableLabel(s *string) *BazelInvocationProblemUpdateOne {
	if s != nil {
		bipuo.SetLabel(*s)
	}
	return bipuo
}

// SetBepEvents sets the "bep_events" field.
func (bipuo *BazelInvocationProblemUpdateOne) SetBepEvents(jm json.RawMessage) *BazelInvocationProblemUpdateOne {
	bipuo.mutation.SetBepEvents(jm)
	return bipuo
}

// AppendBepEvents appends jm to the "bep_events" field.
func (bipuo *BazelInvocationProblemUpdateOne) AppendBepEvents(jm json.RawMessage) *BazelInvocationProblemUpdateOne {
	bipuo.mutation.AppendBepEvents(jm)
	return bipuo
}

// SetBazelInvocationID sets the "bazel_invocation" edge to the BazelInvocation entity by ID.
func (bipuo *BazelInvocationProblemUpdateOne) SetBazelInvocationID(id int) *BazelInvocationProblemUpdateOne {
	bipuo.mutation.SetBazelInvocationID(id)
	return bipuo
}

// SetNillableBazelInvocationID sets the "bazel_invocation" edge to the BazelInvocation entity by ID if the given value is not nil.
func (bipuo *BazelInvocationProblemUpdateOne) SetNillableBazelInvocationID(id *int) *BazelInvocationProblemUpdateOne {
	if id != nil {
		bipuo = bipuo.SetBazelInvocationID(*id)
	}
	return bipuo
}

// SetBazelInvocation sets the "bazel_invocation" edge to the BazelInvocation entity.
func (bipuo *BazelInvocationProblemUpdateOne) SetBazelInvocation(b *BazelInvocation) *BazelInvocationProblemUpdateOne {
	return bipuo.SetBazelInvocationID(b.ID)
}

// Mutation returns the BazelInvocationProblemMutation object of the builder.
func (bipuo *BazelInvocationProblemUpdateOne) Mutation() *BazelInvocationProblemMutation {
	return bipuo.mutation
}

// ClearBazelInvocation clears the "bazel_invocation" edge to the BazelInvocation entity.
func (bipuo *BazelInvocationProblemUpdateOne) ClearBazelInvocation() *BazelInvocationProblemUpdateOne {
	bipuo.mutation.ClearBazelInvocation()
	return bipuo
}

// Where appends a list predicates to the BazelInvocationProblemUpdate builder.
func (bipuo *BazelInvocationProblemUpdateOne) Where(ps ...predicate.BazelInvocationProblem) *BazelInvocationProblemUpdateOne {
	bipuo.mutation.Where(ps...)
	return bipuo
}

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

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

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

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

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

func (bipuo *BazelInvocationProblemUpdateOne) sqlSave(ctx context.Context) (_node *BazelInvocationProblem, err error) {
	_spec := sqlgraph.NewUpdateSpec(bazelinvocationproblem.Table, bazelinvocationproblem.Columns, sqlgraph.NewFieldSpec(bazelinvocationproblem.FieldID, field.TypeInt))
	id, ok := bipuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "BazelInvocationProblem.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := bipuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, bazelinvocationproblem.FieldID)
		for _, f := range fields {
			if !bazelinvocationproblem.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != bazelinvocationproblem.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := bipuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := bipuo.mutation.ProblemType(); ok {
		_spec.SetField(bazelinvocationproblem.FieldProblemType, field.TypeString, value)
	}
	if value, ok := bipuo.mutation.Label(); ok {
		_spec.SetField(bazelinvocationproblem.FieldLabel, field.TypeString, value)
	}
	if value, ok := bipuo.mutation.BepEvents(); ok {
		_spec.SetField(bazelinvocationproblem.FieldBepEvents, field.TypeJSON, value)
	}
	if value, ok := bipuo.mutation.AppendedBepEvents(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, bazelinvocationproblem.FieldBepEvents, value)
		})
	}
	if bipuo.mutation.BazelInvocationCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   bazelinvocationproblem.BazelInvocationTable,
			Columns: []string{bazelinvocationproblem.BazelInvocationColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(bazelinvocation.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := bipuo.mutation.BazelInvocationIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   bazelinvocationproblem.BazelInvocationTable,
			Columns: []string{bazelinvocationproblem.BazelInvocationColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(bazelinvocation.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &BazelInvocationProblem{config: bipuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, bipuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{bazelinvocationproblem.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	bipuo.mutation.done = true
	return _node, nil
}
