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

// ActionDataCreate is the builder for creating a ActionData entity.
type ActionDataCreate struct {
	config
	mutation *ActionDataMutation
	hooks    []Hook
}

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

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

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

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

// SetActionsCreated sets the "actions_created" field.
func (adc *ActionDataCreate) SetActionsCreated(i int64) *ActionDataCreate {
	adc.mutation.SetActionsCreated(i)
	return adc
}

// SetNillableActionsCreated sets the "actions_created" field if the given value is not nil.
func (adc *ActionDataCreate) SetNillableActionsCreated(i *int64) *ActionDataCreate {
	if i != nil {
		adc.SetActionsCreated(*i)
	}
	return adc
}

// SetFirstStartedMs sets the "first_started_ms" field.
func (adc *ActionDataCreate) SetFirstStartedMs(i int64) *ActionDataCreate {
	adc.mutation.SetFirstStartedMs(i)
	return adc
}

// SetNillableFirstStartedMs sets the "first_started_ms" field if the given value is not nil.
func (adc *ActionDataCreate) SetNillableFirstStartedMs(i *int64) *ActionDataCreate {
	if i != nil {
		adc.SetFirstStartedMs(*i)
	}
	return adc
}

// SetLastEndedMs sets the "last_ended_ms" field.
func (adc *ActionDataCreate) SetLastEndedMs(i int64) *ActionDataCreate {
	adc.mutation.SetLastEndedMs(i)
	return adc
}

// SetNillableLastEndedMs sets the "last_ended_ms" field if the given value is not nil.
func (adc *ActionDataCreate) SetNillableLastEndedMs(i *int64) *ActionDataCreate {
	if i != nil {
		adc.SetLastEndedMs(*i)
	}
	return adc
}

// SetSystemTime sets the "system_time" field.
func (adc *ActionDataCreate) SetSystemTime(i int64) *ActionDataCreate {
	adc.mutation.SetSystemTime(i)
	return adc
}

// SetNillableSystemTime sets the "system_time" field if the given value is not nil.
func (adc *ActionDataCreate) SetNillableSystemTime(i *int64) *ActionDataCreate {
	if i != nil {
		adc.SetSystemTime(*i)
	}
	return adc
}

// SetUserTime sets the "user_time" field.
func (adc *ActionDataCreate) SetUserTime(i int64) *ActionDataCreate {
	adc.mutation.SetUserTime(i)
	return adc
}

// SetNillableUserTime sets the "user_time" field if the given value is not nil.
func (adc *ActionDataCreate) SetNillableUserTime(i *int64) *ActionDataCreate {
	if i != nil {
		adc.SetUserTime(*i)
	}
	return adc
}

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

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

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

// Mutation returns the ActionDataMutation object of the builder.
func (adc *ActionDataCreate) Mutation() *ActionDataMutation {
	return adc.mutation
}

// Save creates the ActionData in the database.
func (adc *ActionDataCreate) Save(ctx context.Context) (*ActionData, error) {
	return withHooks(ctx, adc.sqlSave, adc.mutation, adc.hooks)
}

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

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

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

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

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

func (adc *ActionDataCreate) createSpec() (*ActionData, *sqlgraph.CreateSpec) {
	var (
		_node = &ActionData{config: adc.config}
		_spec = sqlgraph.NewCreateSpec(actiondata.Table, sqlgraph.NewFieldSpec(actiondata.FieldID, field.TypeInt))
	)
	if value, ok := adc.mutation.Mnemonic(); ok {
		_spec.SetField(actiondata.FieldMnemonic, field.TypeString, value)
		_node.Mnemonic = value
	}
	if value, ok := adc.mutation.ActionsExecuted(); ok {
		_spec.SetField(actiondata.FieldActionsExecuted, field.TypeInt64, value)
		_node.ActionsExecuted = value
	}
	if value, ok := adc.mutation.ActionsCreated(); ok {
		_spec.SetField(actiondata.FieldActionsCreated, field.TypeInt64, value)
		_node.ActionsCreated = value
	}
	if value, ok := adc.mutation.FirstStartedMs(); ok {
		_spec.SetField(actiondata.FieldFirstStartedMs, field.TypeInt64, value)
		_node.FirstStartedMs = value
	}
	if value, ok := adc.mutation.LastEndedMs(); ok {
		_spec.SetField(actiondata.FieldLastEndedMs, field.TypeInt64, value)
		_node.LastEndedMs = value
	}
	if value, ok := adc.mutation.SystemTime(); ok {
		_spec.SetField(actiondata.FieldSystemTime, field.TypeInt64, value)
		_node.SystemTime = value
	}
	if value, ok := adc.mutation.UserTime(); ok {
		_spec.SetField(actiondata.FieldUserTime, field.TypeInt64, value)
		_node.UserTime = value
	}
	if nodes := adc.mutation.ActionSummaryIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   actiondata.ActionSummaryTable,
			Columns: []string{actiondata.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_action_data = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	return _node, _spec
}

// ActionDataCreateBulk is the builder for creating many ActionData entities in bulk.
type ActionDataCreateBulk struct {
	config
	err      error
	builders []*ActionDataCreate
}

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

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

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

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