// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"fmt"

	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/buildbarn/bb-portal/ent/gen/ent/actionsummary"
	"github.com/buildbarn/bb-portal/ent/gen/ent/runnercount"
)

// RunnerCountCreate is the builder for creating a RunnerCount entity.
type RunnerCountCreate struct {
	config
	mutation *RunnerCountMutation
	hooks    []Hook
}

// SetName sets the "name" field.
func (rcc *RunnerCountCreate) SetName(s string) *RunnerCountCreate {
	rcc.mutation.SetName(s)
	return rcc
}

// SetNillableName sets the "name" field if the given value is not nil.
func (rcc *RunnerCountCreate) SetNillableName(s *string) *RunnerCountCreate {
	if s != nil {
		rcc.SetName(*s)
	}
	return rcc
}

// SetExecKind sets the "exec_kind" field.
func (rcc *RunnerCountCreate) SetExecKind(s string) *RunnerCountCreate {
	rcc.mutation.SetExecKind(s)
	return rcc
}

// SetNillableExecKind sets the "exec_kind" field if the given value is not nil.
func (rcc *RunnerCountCreate) SetNillableExecKind(s *string) *RunnerCountCreate {
	if s != nil {
		rcc.SetExecKind(*s)
	}
	return rcc
}

// SetActionsExecuted sets the "actions_executed" field.
func (rcc *RunnerCountCreate) SetActionsExecuted(i int64) *RunnerCountCreate {
	rcc.mutation.SetActionsExecuted(i)
	return rcc
}

// SetNillableActionsExecuted sets the "actions_executed" field if the given value is not nil.
func (rcc *RunnerCountCreate) SetNillableActionsExecuted(i *int64) *RunnerCountCreate {
	if i != nil {
		rcc.SetActionsExecuted(*i)
	}
	return rcc
}

// SetActionSummaryID sets the "action_summary" edge to the ActionSummary entity by ID.
func (rcc *RunnerCountCreate) SetActionSummaryID(id int) *RunnerCountCreate {
	rcc.mutation.SetActionSummaryID(id)
	return rcc
}

// SetNillableActionSummaryID sets the "action_summary" edge to the ActionSummary entity by ID if the given value is not nil.
func (rcc *RunnerCountCreate) SetNillableActionSummaryID(id *int) *RunnerCountCreate {
	if id != nil {
		rcc = rcc.SetActionSummaryID(*id)
	}
	return rcc
}

// SetActionSummary sets the "action_summary" edge to the ActionSummary entity.
func (rcc *RunnerCountCreate) SetActionSummary(a *ActionSummary) *RunnerCountCreate {
	return rcc.SetActionSummaryID(a.ID)
}

// Mutation returns the RunnerCountMutation object of the builder.
func (rcc *RunnerCountCreate) Mutation() *RunnerCountMutation {
	return rcc.mutation
}

// Save creates the RunnerCount in the database.
func (rcc *RunnerCountCreate) Save(ctx context.Context) (*RunnerCount, error) {
	return withHooks(ctx, rcc.sqlSave, rcc.mutation, rcc.hooks)
}

// SaveX calls Save and panics if Save returns an error.
func (rcc *RunnerCountCreate) SaveX(ctx context.Context) *RunnerCount {
	v, err := rcc.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

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

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

// check runs all checks and user-defined validators on the builder.
func (rcc *RunnerCountCreate) check() error {
	return nil
}

func (rcc *RunnerCountCreate) sqlSave(ctx context.Context) (*RunnerCount, error) {
	if err := rcc.check(); err != nil {
		return nil, err
	}
	_node, _spec := rcc.createSpec()
	if err := sqlgraph.CreateNode(ctx, rcc.driver, _spec); err != nil {
		if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	id := _spec.ID.Value.(int64)
	_node.ID = int(id)
	rcc.mutation.id = &_node.ID
	rcc.mutation.done = true
	return _node, nil
}

func (rcc *RunnerCountCreate) createSpec() (*RunnerCount, *sqlgraph.CreateSpec) {
	var (
		_node = &RunnerCount{config: rcc.config}
		_spec = sqlgraph.NewCreateSpec(runnercount.Table, sqlgraph.NewFieldSpec(runnercount.FieldID, field.TypeInt))
	)
	if value, ok := rcc.mutation.Name(); ok {
		_spec.SetField(runnercount.FieldName, field.TypeString, value)
		_node.Name = value
	}
	if value, ok := rcc.mutation.ExecKind(); ok {
		_spec.SetField(runnercount.FieldExecKind, field.TypeString, value)
		_node.ExecKind = value
	}
	if value, ok := rcc.mutation.ActionsExecuted(); ok {
		_spec.SetField(runnercount.FieldActionsExecuted, field.TypeInt64, value)
		_node.ActionsExecuted = value
	}
	if nodes := rcc.mutation.ActionSummaryIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   runnercount.ActionSummaryTable,
			Columns: []string{runnercount.ActionSummaryColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(actionsummary.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.action_summary_runner_count = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	return _node, _spec
}

// RunnerCountCreateBulk is the builder for creating many RunnerCount entities in bulk.
type RunnerCountCreateBulk struct {
	config
	err      error
	builders []*RunnerCountCreate
}

// Save creates the RunnerCount entities in the database.
func (rccb *RunnerCountCreateBulk) Save(ctx context.Context) ([]*RunnerCount, error) {
	if rccb.err != nil {
		return nil, rccb.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(rccb.builders))
	nodes := make([]*RunnerCount, len(rccb.builders))
	mutators := make([]Mutator, len(rccb.builders))
	for i := range rccb.builders {
		func(i int, root context.Context) {
			builder := rccb.builders[i]
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*RunnerCountMutation)
				if !ok {
					return nil, fmt.Errorf("unexpected mutation type %T", m)
				}
				if err := builder.check(); err != nil {
					return nil, err
				}
				builder.mutation = mutation
				var err error
				nodes[i], specs[i] = builder.createSpec()
				if i < len(mutators)-1 {
					_, err = mutators[i+1].Mutate(root, rccb.builders[i+1].mutation)
				} else {
					spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
					// Invoke the actual operation on the latest mutation in the chain.
					if err = sqlgraph.BatchCreate(ctx, rccb.driver, spec); err != nil {
						if sqlgraph.IsConstraintError(err) {
							err = &ConstraintError{msg: err.Error(), wrap: err}
						}
					}
				}
				if err != nil {
					return nil, err
				}
				mutation.id = &nodes[i].ID
				if specs[i].ID.Value != nil {
					id := specs[i].ID.Value.(int64)
					nodes[i].ID = int(id)
				}
				mutation.done = true
				return nodes[i], nil
			})
			for i := len(builder.hooks) - 1; i >= 0; i-- {
				mut = builder.hooks[i](mut)
			}
			mutators[i] = mut
		}(i, ctx)
	}
	if len(mutators) > 0 {
		if _, err := mutators[0].Mutate(ctx, rccb.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

// SaveX is like Save, but panics if an error occurs.
func (rccb *RunnerCountCreateBulk) SaveX(ctx context.Context) []*RunnerCount {
	v, err := rccb.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

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

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