// Code generated by ent, DO NOT EDIT.

package models

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

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

// WalletCreate is the builder for creating a Wallet entity.
type WalletCreate struct {
	config
	mutation *WalletMutation
	hooks    []Hook
}

// SetBalance sets the "balance" field.
func (_c *WalletCreate) SetBalance(v decimal.Decimal) *WalletCreate {
	_c.mutation.SetBalance(v)
	return _c
}

// SetNillableBalance sets the "balance" field if the given value is not nil.
func (_c *WalletCreate) SetNillableBalance(v *decimal.Decimal) *WalletCreate {
	if v != nil {
		_c.SetBalance(*v)
	}
	return _c
}

// SetFrozenBalance sets the "frozen_balance" field.
func (_c *WalletCreate) SetFrozenBalance(v decimal.Decimal) *WalletCreate {
	_c.mutation.SetFrozenBalance(v)
	return _c
}

// SetNillableFrozenBalance sets the "frozen_balance" field if the given value is not nil.
func (_c *WalletCreate) SetNillableFrozenBalance(v *decimal.Decimal) *WalletCreate {
	if v != nil {
		_c.SetFrozenBalance(*v)
	}
	return _c
}

// SetVersion sets the "version" field.
func (_c *WalletCreate) SetVersion(v int) *WalletCreate {
	_c.mutation.SetVersion(v)
	return _c
}

// SetNillableVersion sets the "version" field if the given value is not nil.
func (_c *WalletCreate) SetNillableVersion(v *int) *WalletCreate {
	if v != nil {
		_c.SetVersion(*v)
	}
	return _c
}

// SetUpdatedAt sets the "updated_at" field.
func (_c *WalletCreate) SetUpdatedAt(v time.Time) *WalletCreate {
	_c.mutation.SetUpdatedAt(v)
	return _c
}

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

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

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

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_c *WalletCreate) 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 *WalletCreate) defaults() {
	if _, ok := _c.mutation.Balance(); !ok {
		v := wallet.DefaultBalance
		_c.mutation.SetBalance(v)
	}
	if _, ok := _c.mutation.FrozenBalance(); !ok {
		v := wallet.DefaultFrozenBalance
		_c.mutation.SetFrozenBalance(v)
	}
	if _, ok := _c.mutation.Version(); !ok {
		v := wallet.DefaultVersion
		_c.mutation.SetVersion(v)
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		v := wallet.DefaultUpdatedAt()
		_c.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_c *WalletCreate) check() error {
	if _, ok := _c.mutation.Balance(); !ok {
		return &ValidationError{Name: "balance", err: errors.New(`models: missing required field "Wallet.balance"`)}
	}
	if _, ok := _c.mutation.FrozenBalance(); !ok {
		return &ValidationError{Name: "frozen_balance", err: errors.New(`models: missing required field "Wallet.frozen_balance"`)}
	}
	if _, ok := _c.mutation.Version(); !ok {
		return &ValidationError{Name: "version", err: errors.New(`models: missing required field "Wallet.version"`)}
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`models: missing required field "Wallet.updated_at"`)}
	}
	return nil
}

func (_c *WalletCreate) sqlSave(ctx context.Context) (*Wallet, 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 *WalletCreate) createSpec() (*Wallet, *sqlgraph.CreateSpec) {
	var (
		_node = &Wallet{config: _c.config}
		_spec = sqlgraph.NewCreateSpec(wallet.Table, sqlgraph.NewFieldSpec(wallet.FieldID, field.TypeInt64))
	)
	if id, ok := _c.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := _c.mutation.Balance(); ok {
		_spec.SetField(wallet.FieldBalance, field.TypeOther, value)
		_node.Balance = value
	}
	if value, ok := _c.mutation.FrozenBalance(); ok {
		_spec.SetField(wallet.FieldFrozenBalance, field.TypeOther, value)
		_node.FrozenBalance = value
	}
	if value, ok := _c.mutation.Version(); ok {
		_spec.SetField(wallet.FieldVersion, field.TypeInt, value)
		_node.Version = value
	}
	if value, ok := _c.mutation.UpdatedAt(); ok {
		_spec.SetField(wallet.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	return _node, _spec
}

// WalletCreateBulk is the builder for creating many Wallet entities in bulk.
type WalletCreateBulk struct {
	config
	err      error
	builders []*WalletCreate
}

// Save creates the Wallet entities in the database.
func (_c *WalletCreateBulk) Save(ctx context.Context) ([]*Wallet, error) {
	if _c.err != nil {
		return nil, _c.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
	nodes := make([]*Wallet, 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.(*WalletMutation)
				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 *WalletCreateBulk) SaveX(ctx context.Context) []*Wallet {
	v, err := _c.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

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

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