// Code generated by ent, DO NOT EDIT.

package models

import (
	"context"
	"errors"
	"fmt"
	"juwan-backend/app/wallet/rpc/internal/models/wallettransactions"
	"time"

	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/shopspring/decimal"
)

// WalletTransactionsCreate is the builder for creating a WalletTransactions entity.
type WalletTransactionsCreate struct {
	config
	mutation *WalletTransactionsMutation
	hooks    []Hook
}

// SetUserID sets the "user_id" field.
func (_c *WalletTransactionsCreate) SetUserID(v int64) *WalletTransactionsCreate {
	_c.mutation.SetUserID(v)
	return _c
}

// SetType sets the "type" field.
func (_c *WalletTransactionsCreate) SetType(v string) *WalletTransactionsCreate {
	_c.mutation.SetType(v)
	return _c
}

// SetAmount sets the "amount" field.
func (_c *WalletTransactionsCreate) SetAmount(v decimal.Decimal) *WalletTransactionsCreate {
	_c.mutation.SetAmount(v)
	return _c
}

// SetBalanceAfter sets the "balance_after" field.
func (_c *WalletTransactionsCreate) SetBalanceAfter(v decimal.Decimal) *WalletTransactionsCreate {
	_c.mutation.SetBalanceAfter(v)
	return _c
}

// SetDescription sets the "description" field.
func (_c *WalletTransactionsCreate) SetDescription(v []string) *WalletTransactionsCreate {
	_c.mutation.SetDescription(v)
	return _c
}

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

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

// SetSearchText sets the "search_text" field.
func (_c *WalletTransactionsCreate) SetSearchText(v string) *WalletTransactionsCreate {
	_c.mutation.SetSearchText(v)
	return _c
}

// SetNillableSearchText sets the "search_text" field if the given value is not nil.
func (_c *WalletTransactionsCreate) SetNillableSearchText(v *string) *WalletTransactionsCreate {
	if v != nil {
		_c.SetSearchText(*v)
	}
	return _c
}

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

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (_c *WalletTransactionsCreate) check() error {
	if _, ok := _c.mutation.UserID(); !ok {
		return &ValidationError{Name: "user_id", err: errors.New(`models: missing required field "WalletTransactions.user_id"`)}
	}
	if _, ok := _c.mutation.GetType(); !ok {
		return &ValidationError{Name: "type", err: errors.New(`models: missing required field "WalletTransactions.type"`)}
	}
	if _, ok := _c.mutation.Amount(); !ok {
		return &ValidationError{Name: "amount", err: errors.New(`models: missing required field "WalletTransactions.amount"`)}
	}
	if _, ok := _c.mutation.BalanceAfter(); !ok {
		return &ValidationError{Name: "balance_after", err: errors.New(`models: missing required field "WalletTransactions.balance_after"`)}
	}
	if _, ok := _c.mutation.Description(); !ok {
		return &ValidationError{Name: "description", err: errors.New(`models: missing required field "WalletTransactions.description"`)}
	}
	if _, ok := _c.mutation.OrderID(); !ok {
		return &ValidationError{Name: "order_id", err: errors.New(`models: missing required field "WalletTransactions.order_id"`)}
	}
	if _, ok := _c.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`models: missing required field "WalletTransactions.created_at"`)}
	}
	return nil
}

func (_c *WalletTransactionsCreate) sqlSave(ctx context.Context) (*WalletTransactions, 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 != nil {
		if id, ok := _spec.ID.Value.(string); ok {
			_node.ID = id
		} else {
			return nil, fmt.Errorf("unexpected WalletTransactions.ID type: %T", _spec.ID.Value)
		}
	}
	_c.mutation.id = &_node.ID
	_c.mutation.done = true
	return _node, nil
}

func (_c *WalletTransactionsCreate) createSpec() (*WalletTransactions, *sqlgraph.CreateSpec) {
	var (
		_node = &WalletTransactions{config: _c.config}
		_spec = sqlgraph.NewCreateSpec(wallettransactions.Table, sqlgraph.NewFieldSpec(wallettransactions.FieldID, field.TypeString))
	)
	if id, ok := _c.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := _c.mutation.UserID(); ok {
		_spec.SetField(wallettransactions.FieldUserID, field.TypeInt64, value)
		_node.UserID = value
	}
	if value, ok := _c.mutation.GetType(); ok {
		_spec.SetField(wallettransactions.FieldType, field.TypeString, value)
		_node.Type = value
	}
	if value, ok := _c.mutation.Amount(); ok {
		_spec.SetField(wallettransactions.FieldAmount, field.TypeOther, value)
		_node.Amount = value
	}
	if value, ok := _c.mutation.BalanceAfter(); ok {
		_spec.SetField(wallettransactions.FieldBalanceAfter, field.TypeOther, value)
		_node.BalanceAfter = value
	}
	if value, ok := _c.mutation.Description(); ok {
		_spec.SetField(wallettransactions.FieldDescription, field.TypeJSON, value)
		_node.Description = value
	}
	if value, ok := _c.mutation.OrderID(); ok {
		_spec.SetField(wallettransactions.FieldOrderID, field.TypeInt64, value)
		_node.OrderID = value
	}
	if value, ok := _c.mutation.CreatedAt(); ok {
		_spec.SetField(wallettransactions.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := _c.mutation.SearchText(); ok {
		_spec.SetField(wallettransactions.FieldSearchText, field.TypeString, value)
		_node.SearchText = value
	}
	return _node, _spec
}

// WalletTransactionsCreateBulk is the builder for creating many WalletTransactions entities in bulk.
type WalletTransactionsCreateBulk struct {
	config
	err      error
	builders []*WalletTransactionsCreate
}

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

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

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