// Code generated by ent, DO NOT EDIT.

package models

import (
	"context"
	"errors"
	"fmt"
	"juwan-backend/app/order/rpc/internal/models/orderstatelogs"
	"time"

	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
)

// OrderStateLogsCreate is the builder for creating a OrderStateLogs entity.
type OrderStateLogsCreate struct {
	config
	mutation *OrderStateLogsMutation
	hooks    []Hook
}

// SetOrderID sets the "order_id" field.
func (_c *OrderStateLogsCreate) SetOrderID(v int64) *OrderStateLogsCreate {
	_c.mutation.SetOrderID(v)
	return _c
}

// SetFromStatus sets the "from_status" field.
func (_c *OrderStateLogsCreate) SetFromStatus(v string) *OrderStateLogsCreate {
	_c.mutation.SetFromStatus(v)
	return _c
}

// SetNillableFromStatus sets the "from_status" field if the given value is not nil.
func (_c *OrderStateLogsCreate) SetNillableFromStatus(v *string) *OrderStateLogsCreate {
	if v != nil {
		_c.SetFromStatus(*v)
	}
	return _c
}

// SetToStatus sets the "to_status" field.
func (_c *OrderStateLogsCreate) SetToStatus(v string) *OrderStateLogsCreate {
	_c.mutation.SetToStatus(v)
	return _c
}

// SetAction sets the "action" field.
func (_c *OrderStateLogsCreate) SetAction(v string) *OrderStateLogsCreate {
	_c.mutation.SetAction(v)
	return _c
}

// SetActorID sets the "actor_id" field.
func (_c *OrderStateLogsCreate) SetActorID(v int64) *OrderStateLogsCreate {
	_c.mutation.SetActorID(v)
	return _c
}

// SetActorRole sets the "actor_role" field.
func (_c *OrderStateLogsCreate) SetActorRole(v string) *OrderStateLogsCreate {
	_c.mutation.SetActorRole(v)
	return _c
}

// SetMetadata sets the "metadata" field.
func (_c *OrderStateLogsCreate) SetMetadata(v map[string]interface{}) *OrderStateLogsCreate {
	_c.mutation.SetMetadata(v)
	return _c
}

// SetCreatedAt sets the "created_at" field.
func (_c *OrderStateLogsCreate) SetCreatedAt(v time.Time) *OrderStateLogsCreate {
	_c.mutation.SetCreatedAt(v)
	return _c
}

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

// SetID sets the "id" field.
func (_c *OrderStateLogsCreate) SetID(v int64) *OrderStateLogsCreate {
	_c.mutation.SetID(v)
	return _c
}

// Mutation returns the OrderStateLogsMutation object of the builder.
func (_c *OrderStateLogsCreate) Mutation() *OrderStateLogsMutation {
	return _c.mutation
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (_c *OrderStateLogsCreate) defaults() {
	if _, ok := _c.mutation.CreatedAt(); !ok {
		v := orderstatelogs.DefaultCreatedAt()
		_c.mutation.SetCreatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_c *OrderStateLogsCreate) check() error {
	if _, ok := _c.mutation.OrderID(); !ok {
		return &ValidationError{Name: "order_id", err: errors.New(`models: missing required field "OrderStateLogs.order_id"`)}
	}
	if v, ok := _c.mutation.FromStatus(); ok {
		if err := orderstatelogs.FromStatusValidator(v); err != nil {
			return &ValidationError{Name: "from_status", err: fmt.Errorf(`models: validator failed for field "OrderStateLogs.from_status": %w`, err)}
		}
	}
	if _, ok := _c.mutation.ToStatus(); !ok {
		return &ValidationError{Name: "to_status", err: errors.New(`models: missing required field "OrderStateLogs.to_status"`)}
	}
	if v, ok := _c.mutation.ToStatus(); ok {
		if err := orderstatelogs.ToStatusValidator(v); err != nil {
			return &ValidationError{Name: "to_status", err: fmt.Errorf(`models: validator failed for field "OrderStateLogs.to_status": %w`, err)}
		}
	}
	if _, ok := _c.mutation.Action(); !ok {
		return &ValidationError{Name: "action", err: errors.New(`models: missing required field "OrderStateLogs.action"`)}
	}
	if v, ok := _c.mutation.Action(); ok {
		if err := orderstatelogs.ActionValidator(v); err != nil {
			return &ValidationError{Name: "action", err: fmt.Errorf(`models: validator failed for field "OrderStateLogs.action": %w`, err)}
		}
	}
	if _, ok := _c.mutation.ActorID(); !ok {
		return &ValidationError{Name: "actor_id", err: errors.New(`models: missing required field "OrderStateLogs.actor_id"`)}
	}
	if _, ok := _c.mutation.ActorRole(); !ok {
		return &ValidationError{Name: "actor_role", err: errors.New(`models: missing required field "OrderStateLogs.actor_role"`)}
	}
	if v, ok := _c.mutation.ActorRole(); ok {
		if err := orderstatelogs.ActorRoleValidator(v); err != nil {
			return &ValidationError{Name: "actor_role", err: fmt.Errorf(`models: validator failed for field "OrderStateLogs.actor_role": %w`, err)}
		}
	}
	if _, ok := _c.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`models: missing required field "OrderStateLogs.created_at"`)}
	}
	return nil
}

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

func (_c *OrderStateLogsCreate) createSpec() (*OrderStateLogs, *sqlgraph.CreateSpec) {
	var (
		_node = &OrderStateLogs{config: _c.config}
		_spec = sqlgraph.NewCreateSpec(orderstatelogs.Table, sqlgraph.NewFieldSpec(orderstatelogs.FieldID, field.TypeInt64))
	)
	if id, ok := _c.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := _c.mutation.OrderID(); ok {
		_spec.SetField(orderstatelogs.FieldOrderID, field.TypeInt64, value)
		_node.OrderID = value
	}
	if value, ok := _c.mutation.FromStatus(); ok {
		_spec.SetField(orderstatelogs.FieldFromStatus, field.TypeString, value)
		_node.FromStatus = &value
	}
	if value, ok := _c.mutation.ToStatus(); ok {
		_spec.SetField(orderstatelogs.FieldToStatus, field.TypeString, value)
		_node.ToStatus = value
	}
	if value, ok := _c.mutation.Action(); ok {
		_spec.SetField(orderstatelogs.FieldAction, field.TypeString, value)
		_node.Action = value
	}
	if value, ok := _c.mutation.ActorID(); ok {
		_spec.SetField(orderstatelogs.FieldActorID, field.TypeInt64, value)
		_node.ActorID = value
	}
	if value, ok := _c.mutation.ActorRole(); ok {
		_spec.SetField(orderstatelogs.FieldActorRole, field.TypeString, value)
		_node.ActorRole = value
	}
	if value, ok := _c.mutation.Metadata(); ok {
		_spec.SetField(orderstatelogs.FieldMetadata, field.TypeJSON, value)
		_node.Metadata = value
	}
	if value, ok := _c.mutation.CreatedAt(); ok {
		_spec.SetField(orderstatelogs.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	return _node, _spec
}

// OrderStateLogsCreateBulk is the builder for creating many OrderStateLogs entities in bulk.
type OrderStateLogsCreateBulk struct {
	config
	err      error
	builders []*OrderStateLogsCreate
}

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

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

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

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