// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"galileo/ent/metaevent"
	"time"

	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/google/uuid"
)

// MetaEventCreate is the builder for creating a MetaEvent entity.
type MetaEventCreate struct {
	config
	mutation *MetaEventMutation
	hooks    []Hook
}

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

// SetUUID sets the "uuid" field.
func (mec *MetaEventCreate) SetUUID(u uuid.UUID) *MetaEventCreate {
	mec.mutation.SetUUID(u)
	return mec
}

// SetNillableUUID sets the "uuid" field if the given value is not nil.
func (mec *MetaEventCreate) SetNillableUUID(u *uuid.UUID) *MetaEventCreate {
	if u != nil {
		mec.SetUUID(*u)
	}
	return mec
}

// SetDisplayName sets the "display_name" field.
func (mec *MetaEventCreate) SetDisplayName(s string) *MetaEventCreate {
	mec.mutation.SetDisplayName(s)
	return mec
}

// SetEventDesc sets the "event_desc" field.
func (mec *MetaEventCreate) SetEventDesc(s string) *MetaEventCreate {
	mec.mutation.SetEventDesc(s)
	return mec
}

// SetNillableEventDesc sets the "event_desc" field if the given value is not nil.
func (mec *MetaEventCreate) SetNillableEventDesc(s *string) *MetaEventCreate {
	if s != nil {
		mec.SetEventDesc(*s)
	}
	return mec
}

// SetRemark sets the "remark" field.
func (mec *MetaEventCreate) SetRemark(s string) *MetaEventCreate {
	mec.mutation.SetRemark(s)
	return mec
}

// SetNillableRemark sets the "remark" field if the given value is not nil.
func (mec *MetaEventCreate) SetNillableRemark(s *string) *MetaEventCreate {
	if s != nil {
		mec.SetRemark(*s)
	}
	return mec
}

// SetCreatedAt sets the "created_at" field.
func (mec *MetaEventCreate) SetCreatedAt(t time.Time) *MetaEventCreate {
	mec.mutation.SetCreatedAt(t)
	return mec
}

// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (mec *MetaEventCreate) SetNillableCreatedAt(t *time.Time) *MetaEventCreate {
	if t != nil {
		mec.SetCreatedAt(*t)
	}
	return mec
}

// SetUpdatedAt sets the "updated_at" field.
func (mec *MetaEventCreate) SetUpdatedAt(t time.Time) *MetaEventCreate {
	mec.mutation.SetUpdatedAt(t)
	return mec
}

// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (mec *MetaEventCreate) SetNillableUpdatedAt(t *time.Time) *MetaEventCreate {
	if t != nil {
		mec.SetUpdatedAt(*t)
	}
	return mec
}

// SetID sets the "id" field.
func (mec *MetaEventCreate) SetID(i int64) *MetaEventCreate {
	mec.mutation.SetID(i)
	return mec
}

// Mutation returns the MetaEventMutation object of the builder.
func (mec *MetaEventCreate) Mutation() *MetaEventMutation {
	return mec.mutation
}

// Save creates the MetaEvent in the database.
func (mec *MetaEventCreate) Save(ctx context.Context) (*MetaEvent, error) {
	mec.defaults()
	return withHooks[*MetaEvent, MetaEventMutation](ctx, mec.sqlSave, mec.mutation, mec.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (mec *MetaEventCreate) defaults() {
	if _, ok := mec.mutation.UUID(); !ok {
		v := metaevent.DefaultUUID()
		mec.mutation.SetUUID(v)
	}
	if _, ok := mec.mutation.CreatedAt(); !ok {
		v := metaevent.DefaultCreatedAt()
		mec.mutation.SetCreatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (mec *MetaEventCreate) check() error {
	if _, ok := mec.mutation.EventName(); !ok {
		return &ValidationError{Name: "event_name", err: errors.New(`ent: missing required field "MetaEvent.event_name"`)}
	}
	if v, ok := mec.mutation.EventName(); ok {
		if err := metaevent.EventNameValidator(v); err != nil {
			return &ValidationError{Name: "event_name", err: fmt.Errorf(`ent: validator failed for field "MetaEvent.event_name": %w`, err)}
		}
	}
	if _, ok := mec.mutation.UUID(); !ok {
		return &ValidationError{Name: "uuid", err: errors.New(`ent: missing required field "MetaEvent.uuid"`)}
	}
	if _, ok := mec.mutation.DisplayName(); !ok {
		return &ValidationError{Name: "display_name", err: errors.New(`ent: missing required field "MetaEvent.display_name"`)}
	}
	if v, ok := mec.mutation.DisplayName(); ok {
		if err := metaevent.DisplayNameValidator(v); err != nil {
			return &ValidationError{Name: "display_name", err: fmt.Errorf(`ent: validator failed for field "MetaEvent.display_name": %w`, err)}
		}
	}
	if _, ok := mec.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "MetaEvent.created_at"`)}
	}
	return nil
}

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

func (mec *MetaEventCreate) createSpec() (*MetaEvent, *sqlgraph.CreateSpec) {
	var (
		_node = &MetaEvent{config: mec.config}
		_spec = sqlgraph.NewCreateSpec(metaevent.Table, sqlgraph.NewFieldSpec(metaevent.FieldID, field.TypeInt64))
	)
	if id, ok := mec.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := mec.mutation.EventName(); ok {
		_spec.SetField(metaevent.FieldEventName, field.TypeString, value)
		_node.EventName = value
	}
	if value, ok := mec.mutation.UUID(); ok {
		_spec.SetField(metaevent.FieldUUID, field.TypeUUID, value)
		_node.UUID = value
	}
	if value, ok := mec.mutation.DisplayName(); ok {
		_spec.SetField(metaevent.FieldDisplayName, field.TypeString, value)
		_node.DisplayName = value
	}
	if value, ok := mec.mutation.EventDesc(); ok {
		_spec.SetField(metaevent.FieldEventDesc, field.TypeString, value)
		_node.EventDesc = value
	}
	if value, ok := mec.mutation.Remark(); ok {
		_spec.SetField(metaevent.FieldRemark, field.TypeString, value)
		_node.Remark = value
	}
	if value, ok := mec.mutation.CreatedAt(); ok {
		_spec.SetField(metaevent.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := mec.mutation.UpdatedAt(); ok {
		_spec.SetField(metaevent.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	return _node, _spec
}

// MetaEventCreateBulk is the builder for creating many MetaEvent entities in bulk.
type MetaEventCreateBulk struct {
	config
	builders []*MetaEventCreate
}

// Save creates the MetaEvent entities in the database.
func (mecb *MetaEventCreateBulk) Save(ctx context.Context) ([]*MetaEvent, error) {
	specs := make([]*sqlgraph.CreateSpec, len(mecb.builders))
	nodes := make([]*MetaEvent, len(mecb.builders))
	mutators := make([]Mutator, len(mecb.builders))
	for i := range mecb.builders {
		func(i int, root context.Context) {
			builder := mecb.builders[i]
			builder.defaults()
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*MetaEventMutation)
				if !ok {
					return nil, fmt.Errorf("unexpected mutation type %T", m)
				}
				if err := builder.check(); err != nil {
					return nil, err
				}
				builder.mutation = mutation
				nodes[i], specs[i] = builder.createSpec()
				var err error
				if i < len(mutators)-1 {
					_, err = mutators[i+1].Mutate(root, mecb.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, mecb.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 && nodes[i].ID == 0 {
					id := specs[i].ID.Value.(int64)
					nodes[i].ID = int64(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, mecb.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

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

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

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