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

// RaceStatisticsCreate is the builder for creating a RaceStatistics entity.
type RaceStatisticsCreate struct {
	config
	mutation *RaceStatisticsMutation
	hooks    []Hook
}

// SetMnemonic sets the "mnemonic" field.
func (rsc *RaceStatisticsCreate) SetMnemonic(s string) *RaceStatisticsCreate {
	rsc.mutation.SetMnemonic(s)
	return rsc
}

// SetNillableMnemonic sets the "mnemonic" field if the given value is not nil.
func (rsc *RaceStatisticsCreate) SetNillableMnemonic(s *string) *RaceStatisticsCreate {
	if s != nil {
		rsc.SetMnemonic(*s)
	}
	return rsc
}

// SetLocalRunner sets the "local_runner" field.
func (rsc *RaceStatisticsCreate) SetLocalRunner(s string) *RaceStatisticsCreate {
	rsc.mutation.SetLocalRunner(s)
	return rsc
}

// SetNillableLocalRunner sets the "local_runner" field if the given value is not nil.
func (rsc *RaceStatisticsCreate) SetNillableLocalRunner(s *string) *RaceStatisticsCreate {
	if s != nil {
		rsc.SetLocalRunner(*s)
	}
	return rsc
}

// SetRemoteRunner sets the "remote_runner" field.
func (rsc *RaceStatisticsCreate) SetRemoteRunner(s string) *RaceStatisticsCreate {
	rsc.mutation.SetRemoteRunner(s)
	return rsc
}

// SetNillableRemoteRunner sets the "remote_runner" field if the given value is not nil.
func (rsc *RaceStatisticsCreate) SetNillableRemoteRunner(s *string) *RaceStatisticsCreate {
	if s != nil {
		rsc.SetRemoteRunner(*s)
	}
	return rsc
}

// SetLocalWins sets the "local_wins" field.
func (rsc *RaceStatisticsCreate) SetLocalWins(i int64) *RaceStatisticsCreate {
	rsc.mutation.SetLocalWins(i)
	return rsc
}

// SetNillableLocalWins sets the "local_wins" field if the given value is not nil.
func (rsc *RaceStatisticsCreate) SetNillableLocalWins(i *int64) *RaceStatisticsCreate {
	if i != nil {
		rsc.SetLocalWins(*i)
	}
	return rsc
}

// SetRenoteWins sets the "renote_wins" field.
func (rsc *RaceStatisticsCreate) SetRenoteWins(i int64) *RaceStatisticsCreate {
	rsc.mutation.SetRenoteWins(i)
	return rsc
}

// SetNillableRenoteWins sets the "renote_wins" field if the given value is not nil.
func (rsc *RaceStatisticsCreate) SetNillableRenoteWins(i *int64) *RaceStatisticsCreate {
	if i != nil {
		rsc.SetRenoteWins(*i)
	}
	return rsc
}

// SetDynamicExecutionMetricsID sets the "dynamic_execution_metrics" edge to the DynamicExecutionMetrics entity by ID.
func (rsc *RaceStatisticsCreate) SetDynamicExecutionMetricsID(id int) *RaceStatisticsCreate {
	rsc.mutation.SetDynamicExecutionMetricsID(id)
	return rsc
}

// SetNillableDynamicExecutionMetricsID sets the "dynamic_execution_metrics" edge to the DynamicExecutionMetrics entity by ID if the given value is not nil.
func (rsc *RaceStatisticsCreate) SetNillableDynamicExecutionMetricsID(id *int) *RaceStatisticsCreate {
	if id != nil {
		rsc = rsc.SetDynamicExecutionMetricsID(*id)
	}
	return rsc
}

// SetDynamicExecutionMetrics sets the "dynamic_execution_metrics" edge to the DynamicExecutionMetrics entity.
func (rsc *RaceStatisticsCreate) SetDynamicExecutionMetrics(d *DynamicExecutionMetrics) *RaceStatisticsCreate {
	return rsc.SetDynamicExecutionMetricsID(d.ID)
}

// Mutation returns the RaceStatisticsMutation object of the builder.
func (rsc *RaceStatisticsCreate) Mutation() *RaceStatisticsMutation {
	return rsc.mutation
}

// Save creates the RaceStatistics in the database.
func (rsc *RaceStatisticsCreate) Save(ctx context.Context) (*RaceStatistics, error) {
	return withHooks(ctx, rsc.sqlSave, rsc.mutation, rsc.hooks)
}

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

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

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

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

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

func (rsc *RaceStatisticsCreate) createSpec() (*RaceStatistics, *sqlgraph.CreateSpec) {
	var (
		_node = &RaceStatistics{config: rsc.config}
		_spec = sqlgraph.NewCreateSpec(racestatistics.Table, sqlgraph.NewFieldSpec(racestatistics.FieldID, field.TypeInt))
	)
	if value, ok := rsc.mutation.Mnemonic(); ok {
		_spec.SetField(racestatistics.FieldMnemonic, field.TypeString, value)
		_node.Mnemonic = value
	}
	if value, ok := rsc.mutation.LocalRunner(); ok {
		_spec.SetField(racestatistics.FieldLocalRunner, field.TypeString, value)
		_node.LocalRunner = value
	}
	if value, ok := rsc.mutation.RemoteRunner(); ok {
		_spec.SetField(racestatistics.FieldRemoteRunner, field.TypeString, value)
		_node.RemoteRunner = value
	}
	if value, ok := rsc.mutation.LocalWins(); ok {
		_spec.SetField(racestatistics.FieldLocalWins, field.TypeInt64, value)
		_node.LocalWins = value
	}
	if value, ok := rsc.mutation.RenoteWins(); ok {
		_spec.SetField(racestatistics.FieldRenoteWins, field.TypeInt64, value)
		_node.RenoteWins = value
	}
	if nodes := rsc.mutation.DynamicExecutionMetricsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   racestatistics.DynamicExecutionMetricsTable,
			Columns: []string{racestatistics.DynamicExecutionMetricsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(dynamicexecutionmetrics.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.dynamic_execution_metrics_race_statistics = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	return _node, _spec
}

// RaceStatisticsCreateBulk is the builder for creating many RaceStatistics entities in bulk.
type RaceStatisticsCreateBulk struct {
	config
	err      error
	builders []*RaceStatisticsCreate
}

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

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

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

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