// 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/bazelinvocation"
	"github.com/buildbarn/bb-portal/ent/gen/ent/sourcecontrol"
)

// SourceControlCreate is the builder for creating a SourceControl entity.
type SourceControlCreate struct {
	config
	mutation *SourceControlMutation
	hooks    []Hook
}

// SetRepoURL sets the "repo_url" field.
func (scc *SourceControlCreate) SetRepoURL(s string) *SourceControlCreate {
	scc.mutation.SetRepoURL(s)
	return scc
}

// SetNillableRepoURL sets the "repo_url" field if the given value is not nil.
func (scc *SourceControlCreate) SetNillableRepoURL(s *string) *SourceControlCreate {
	if s != nil {
		scc.SetRepoURL(*s)
	}
	return scc
}

// SetBranch sets the "branch" field.
func (scc *SourceControlCreate) SetBranch(s string) *SourceControlCreate {
	scc.mutation.SetBranch(s)
	return scc
}

// SetNillableBranch sets the "branch" field if the given value is not nil.
func (scc *SourceControlCreate) SetNillableBranch(s *string) *SourceControlCreate {
	if s != nil {
		scc.SetBranch(*s)
	}
	return scc
}

// SetCommitSha sets the "commit_sha" field.
func (scc *SourceControlCreate) SetCommitSha(s string) *SourceControlCreate {
	scc.mutation.SetCommitSha(s)
	return scc
}

// SetNillableCommitSha sets the "commit_sha" field if the given value is not nil.
func (scc *SourceControlCreate) SetNillableCommitSha(s *string) *SourceControlCreate {
	if s != nil {
		scc.SetCommitSha(*s)
	}
	return scc
}

// SetActor sets the "actor" field.
func (scc *SourceControlCreate) SetActor(s string) *SourceControlCreate {
	scc.mutation.SetActor(s)
	return scc
}

// SetNillableActor sets the "actor" field if the given value is not nil.
func (scc *SourceControlCreate) SetNillableActor(s *string) *SourceControlCreate {
	if s != nil {
		scc.SetActor(*s)
	}
	return scc
}

// SetRefs sets the "refs" field.
func (scc *SourceControlCreate) SetRefs(s string) *SourceControlCreate {
	scc.mutation.SetRefs(s)
	return scc
}

// SetNillableRefs sets the "refs" field if the given value is not nil.
func (scc *SourceControlCreate) SetNillableRefs(s *string) *SourceControlCreate {
	if s != nil {
		scc.SetRefs(*s)
	}
	return scc
}

// SetRunID sets the "run_id" field.
func (scc *SourceControlCreate) SetRunID(s string) *SourceControlCreate {
	scc.mutation.SetRunID(s)
	return scc
}

// SetNillableRunID sets the "run_id" field if the given value is not nil.
func (scc *SourceControlCreate) SetNillableRunID(s *string) *SourceControlCreate {
	if s != nil {
		scc.SetRunID(*s)
	}
	return scc
}

// SetWorkflow sets the "workflow" field.
func (scc *SourceControlCreate) SetWorkflow(s string) *SourceControlCreate {
	scc.mutation.SetWorkflow(s)
	return scc
}

// SetNillableWorkflow sets the "workflow" field if the given value is not nil.
func (scc *SourceControlCreate) SetNillableWorkflow(s *string) *SourceControlCreate {
	if s != nil {
		scc.SetWorkflow(*s)
	}
	return scc
}

// SetAction sets the "action" field.
func (scc *SourceControlCreate) SetAction(s string) *SourceControlCreate {
	scc.mutation.SetAction(s)
	return scc
}

// SetNillableAction sets the "action" field if the given value is not nil.
func (scc *SourceControlCreate) SetNillableAction(s *string) *SourceControlCreate {
	if s != nil {
		scc.SetAction(*s)
	}
	return scc
}

// SetWorkspace sets the "workspace" field.
func (scc *SourceControlCreate) SetWorkspace(s string) *SourceControlCreate {
	scc.mutation.SetWorkspace(s)
	return scc
}

// SetNillableWorkspace sets the "workspace" field if the given value is not nil.
func (scc *SourceControlCreate) SetNillableWorkspace(s *string) *SourceControlCreate {
	if s != nil {
		scc.SetWorkspace(*s)
	}
	return scc
}

// SetEventName sets the "event_name" field.
func (scc *SourceControlCreate) SetEventName(s string) *SourceControlCreate {
	scc.mutation.SetEventName(s)
	return scc
}

// SetNillableEventName sets the "event_name" field if the given value is not nil.
func (scc *SourceControlCreate) SetNillableEventName(s *string) *SourceControlCreate {
	if s != nil {
		scc.SetEventName(*s)
	}
	return scc
}

// SetJob sets the "job" field.
func (scc *SourceControlCreate) SetJob(s string) *SourceControlCreate {
	scc.mutation.SetJob(s)
	return scc
}

// SetNillableJob sets the "job" field if the given value is not nil.
func (scc *SourceControlCreate) SetNillableJob(s *string) *SourceControlCreate {
	if s != nil {
		scc.SetJob(*s)
	}
	return scc
}

// SetRunnerName sets the "runner_name" field.
func (scc *SourceControlCreate) SetRunnerName(s string) *SourceControlCreate {
	scc.mutation.SetRunnerName(s)
	return scc
}

// SetNillableRunnerName sets the "runner_name" field if the given value is not nil.
func (scc *SourceControlCreate) SetNillableRunnerName(s *string) *SourceControlCreate {
	if s != nil {
		scc.SetRunnerName(*s)
	}
	return scc
}

// SetRunnerArch sets the "runner_arch" field.
func (scc *SourceControlCreate) SetRunnerArch(s string) *SourceControlCreate {
	scc.mutation.SetRunnerArch(s)
	return scc
}

// SetNillableRunnerArch sets the "runner_arch" field if the given value is not nil.
func (scc *SourceControlCreate) SetNillableRunnerArch(s *string) *SourceControlCreate {
	if s != nil {
		scc.SetRunnerArch(*s)
	}
	return scc
}

// SetRunnerOs sets the "runner_os" field.
func (scc *SourceControlCreate) SetRunnerOs(s string) *SourceControlCreate {
	scc.mutation.SetRunnerOs(s)
	return scc
}

// SetNillableRunnerOs sets the "runner_os" field if the given value is not nil.
func (scc *SourceControlCreate) SetNillableRunnerOs(s *string) *SourceControlCreate {
	if s != nil {
		scc.SetRunnerOs(*s)
	}
	return scc
}

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

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

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

// Mutation returns the SourceControlMutation object of the builder.
func (scc *SourceControlCreate) Mutation() *SourceControlMutation {
	return scc.mutation
}

// Save creates the SourceControl in the database.
func (scc *SourceControlCreate) Save(ctx context.Context) (*SourceControl, error) {
	return withHooks(ctx, scc.sqlSave, scc.mutation, scc.hooks)
}

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

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

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

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

func (scc *SourceControlCreate) sqlSave(ctx context.Context) (*SourceControl, error) {
	if err := scc.check(); err != nil {
		return nil, err
	}
	_node, _spec := scc.createSpec()
	if err := sqlgraph.CreateNode(ctx, scc.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)
	scc.mutation.id = &_node.ID
	scc.mutation.done = true
	return _node, nil
}

func (scc *SourceControlCreate) createSpec() (*SourceControl, *sqlgraph.CreateSpec) {
	var (
		_node = &SourceControl{config: scc.config}
		_spec = sqlgraph.NewCreateSpec(sourcecontrol.Table, sqlgraph.NewFieldSpec(sourcecontrol.FieldID, field.TypeInt))
	)
	if value, ok := scc.mutation.RepoURL(); ok {
		_spec.SetField(sourcecontrol.FieldRepoURL, field.TypeString, value)
		_node.RepoURL = value
	}
	if value, ok := scc.mutation.Branch(); ok {
		_spec.SetField(sourcecontrol.FieldBranch, field.TypeString, value)
		_node.Branch = value
	}
	if value, ok := scc.mutation.CommitSha(); ok {
		_spec.SetField(sourcecontrol.FieldCommitSha, field.TypeString, value)
		_node.CommitSha = value
	}
	if value, ok := scc.mutation.Actor(); ok {
		_spec.SetField(sourcecontrol.FieldActor, field.TypeString, value)
		_node.Actor = value
	}
	if value, ok := scc.mutation.Refs(); ok {
		_spec.SetField(sourcecontrol.FieldRefs, field.TypeString, value)
		_node.Refs = value
	}
	if value, ok := scc.mutation.RunID(); ok {
		_spec.SetField(sourcecontrol.FieldRunID, field.TypeString, value)
		_node.RunID = value
	}
	if value, ok := scc.mutation.Workflow(); ok {
		_spec.SetField(sourcecontrol.FieldWorkflow, field.TypeString, value)
		_node.Workflow = value
	}
	if value, ok := scc.mutation.Action(); ok {
		_spec.SetField(sourcecontrol.FieldAction, field.TypeString, value)
		_node.Action = value
	}
	if value, ok := scc.mutation.Workspace(); ok {
		_spec.SetField(sourcecontrol.FieldWorkspace, field.TypeString, value)
		_node.Workspace = value
	}
	if value, ok := scc.mutation.EventName(); ok {
		_spec.SetField(sourcecontrol.FieldEventName, field.TypeString, value)
		_node.EventName = value
	}
	if value, ok := scc.mutation.Job(); ok {
		_spec.SetField(sourcecontrol.FieldJob, field.TypeString, value)
		_node.Job = value
	}
	if value, ok := scc.mutation.RunnerName(); ok {
		_spec.SetField(sourcecontrol.FieldRunnerName, field.TypeString, value)
		_node.RunnerName = value
	}
	if value, ok := scc.mutation.RunnerArch(); ok {
		_spec.SetField(sourcecontrol.FieldRunnerArch, field.TypeString, value)
		_node.RunnerArch = value
	}
	if value, ok := scc.mutation.RunnerOs(); ok {
		_spec.SetField(sourcecontrol.FieldRunnerOs, field.TypeString, value)
		_node.RunnerOs = value
	}
	if nodes := scc.mutation.BazelInvocationIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   sourcecontrol.BazelInvocationTable,
			Columns: []string{sourcecontrol.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)
		}
		_node.bazel_invocation_source_control = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	return _node, _spec
}

// SourceControlCreateBulk is the builder for creating many SourceControl entities in bulk.
type SourceControlCreateBulk struct {
	config
	err      error
	builders []*SourceControlCreate
}

// Save creates the SourceControl entities in the database.
func (sccb *SourceControlCreateBulk) Save(ctx context.Context) ([]*SourceControl, error) {
	if sccb.err != nil {
		return nil, sccb.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(sccb.builders))
	nodes := make([]*SourceControl, len(sccb.builders))
	mutators := make([]Mutator, len(sccb.builders))
	for i := range sccb.builders {
		func(i int, root context.Context) {
			builder := sccb.builders[i]
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*SourceControlMutation)
				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, sccb.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, sccb.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, sccb.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

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

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

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