// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"

	"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/eventfile"
)

// EventFileCreate is the builder for creating a EventFile entity.
type EventFileCreate struct {
	config
	mutation *EventFileMutation
	hooks    []Hook
}

// SetURL sets the "url" field.
func (efc *EventFileCreate) SetURL(s string) *EventFileCreate {
	efc.mutation.SetURL(s)
	return efc
}

// SetModTime sets the "mod_time" field.
func (efc *EventFileCreate) SetModTime(t time.Time) *EventFileCreate {
	efc.mutation.SetModTime(t)
	return efc
}

// SetProtocol sets the "protocol" field.
func (efc *EventFileCreate) SetProtocol(s string) *EventFileCreate {
	efc.mutation.SetProtocol(s)
	return efc
}

// SetMimeType sets the "mime_type" field.
func (efc *EventFileCreate) SetMimeType(s string) *EventFileCreate {
	efc.mutation.SetMimeType(s)
	return efc
}

// SetStatus sets the "status" field.
func (efc *EventFileCreate) SetStatus(s string) *EventFileCreate {
	efc.mutation.SetStatus(s)
	return efc
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (efc *EventFileCreate) SetNillableStatus(s *string) *EventFileCreate {
	if s != nil {
		efc.SetStatus(*s)
	}
	return efc
}

// SetReason sets the "reason" field.
func (efc *EventFileCreate) SetReason(s string) *EventFileCreate {
	efc.mutation.SetReason(s)
	return efc
}

// SetNillableReason sets the "reason" field if the given value is not nil.
func (efc *EventFileCreate) SetNillableReason(s *string) *EventFileCreate {
	if s != nil {
		efc.SetReason(*s)
	}
	return efc
}

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

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

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

// Mutation returns the EventFileMutation object of the builder.
func (efc *EventFileCreate) Mutation() *EventFileMutation {
	return efc.mutation
}

// Save creates the EventFile in the database.
func (efc *EventFileCreate) Save(ctx context.Context) (*EventFile, error) {
	efc.defaults()
	return withHooks(ctx, efc.sqlSave, efc.mutation, efc.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (efc *EventFileCreate) defaults() {
	if _, ok := efc.mutation.Status(); !ok {
		v := eventfile.DefaultStatus
		efc.mutation.SetStatus(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (efc *EventFileCreate) check() error {
	if _, ok := efc.mutation.URL(); !ok {
		return &ValidationError{Name: "url", err: errors.New(`ent: missing required field "EventFile.url"`)}
	}
	if _, ok := efc.mutation.ModTime(); !ok {
		return &ValidationError{Name: "mod_time", err: errors.New(`ent: missing required field "EventFile.mod_time"`)}
	}
	if _, ok := efc.mutation.Protocol(); !ok {
		return &ValidationError{Name: "protocol", err: errors.New(`ent: missing required field "EventFile.protocol"`)}
	}
	if _, ok := efc.mutation.MimeType(); !ok {
		return &ValidationError{Name: "mime_type", err: errors.New(`ent: missing required field "EventFile.mime_type"`)}
	}
	if _, ok := efc.mutation.Status(); !ok {
		return &ValidationError{Name: "status", err: errors.New(`ent: missing required field "EventFile.status"`)}
	}
	return nil
}

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

func (efc *EventFileCreate) createSpec() (*EventFile, *sqlgraph.CreateSpec) {
	var (
		_node = &EventFile{config: efc.config}
		_spec = sqlgraph.NewCreateSpec(eventfile.Table, sqlgraph.NewFieldSpec(eventfile.FieldID, field.TypeInt))
	)
	if value, ok := efc.mutation.URL(); ok {
		_spec.SetField(eventfile.FieldURL, field.TypeString, value)
		_node.URL = value
	}
	if value, ok := efc.mutation.ModTime(); ok {
		_spec.SetField(eventfile.FieldModTime, field.TypeTime, value)
		_node.ModTime = value
	}
	if value, ok := efc.mutation.Protocol(); ok {
		_spec.SetField(eventfile.FieldProtocol, field.TypeString, value)
		_node.Protocol = value
	}
	if value, ok := efc.mutation.MimeType(); ok {
		_spec.SetField(eventfile.FieldMimeType, field.TypeString, value)
		_node.MimeType = value
	}
	if value, ok := efc.mutation.Status(); ok {
		_spec.SetField(eventfile.FieldStatus, field.TypeString, value)
		_node.Status = value
	}
	if value, ok := efc.mutation.Reason(); ok {
		_spec.SetField(eventfile.FieldReason, field.TypeString, value)
		_node.Reason = value
	}
	if nodes := efc.mutation.BazelInvocationIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   eventfile.BazelInvocationTable,
			Columns: []string{eventfile.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 = append(_spec.Edges, edge)
	}
	return _node, _spec
}

// EventFileCreateBulk is the builder for creating many EventFile entities in bulk.
type EventFileCreateBulk struct {
	config
	err      error
	builders []*EventFileCreate
}

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

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

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

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