// Code generated by ent, DO NOT EDIT.

package models

import (
	"context"
	"errors"
	"fmt"
	"juwan-backend/app/player/rpc/internal/models/players"
	"juwan-backend/pkg/types"
	"time"

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

// PlayersCreate is the builder for creating a Players entity.
type PlayersCreate struct {
	config
	mutation *PlayersMutation
	hooks    []Hook
}

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

// SetStatus sets the "status" field.
func (_c *PlayersCreate) SetStatus(v string) *PlayersCreate {
	_c.mutation.SetStatus(v)
	return _c
}

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

// SetGender sets the "gender" field.
func (_c *PlayersCreate) SetGender(v bool) *PlayersCreate {
	_c.mutation.SetGender(v)
	return _c
}

// SetNillableGender sets the "gender" field if the given value is not nil.
func (_c *PlayersCreate) SetNillableGender(v *bool) *PlayersCreate {
	if v != nil {
		_c.SetGender(*v)
	}
	return _c
}

// SetRating sets the "rating" field.
func (_c *PlayersCreate) SetRating(v decimal.Decimal) *PlayersCreate {
	_c.mutation.SetRating(v)
	return _c
}

// SetNillableRating sets the "rating" field if the given value is not nil.
func (_c *PlayersCreate) SetNillableRating(v *decimal.Decimal) *PlayersCreate {
	if v != nil {
		_c.SetRating(*v)
	}
	return _c
}

// SetTotalOrders sets the "total_orders" field.
func (_c *PlayersCreate) SetTotalOrders(v int) *PlayersCreate {
	_c.mutation.SetTotalOrders(v)
	return _c
}

// SetNillableTotalOrders sets the "total_orders" field if the given value is not nil.
func (_c *PlayersCreate) SetNillableTotalOrders(v *int) *PlayersCreate {
	if v != nil {
		_c.SetTotalOrders(*v)
	}
	return _c
}

// SetCompletedOrders sets the "completed_orders" field.
func (_c *PlayersCreate) SetCompletedOrders(v int) *PlayersCreate {
	_c.mutation.SetCompletedOrders(v)
	return _c
}

// SetNillableCompletedOrders sets the "completed_orders" field if the given value is not nil.
func (_c *PlayersCreate) SetNillableCompletedOrders(v *int) *PlayersCreate {
	if v != nil {
		_c.SetCompletedOrders(*v)
	}
	return _c
}

// SetShopID sets the "shop_id" field.
func (_c *PlayersCreate) SetShopID(v int64) *PlayersCreate {
	_c.mutation.SetShopID(v)
	return _c
}

// SetNillableShopID sets the "shop_id" field if the given value is not nil.
func (_c *PlayersCreate) SetNillableShopID(v *int64) *PlayersCreate {
	if v != nil {
		_c.SetShopID(*v)
	}
	return _c
}

// SetTags sets the "tags" field.
func (_c *PlayersCreate) SetTags(v []string) *PlayersCreate {
	_c.mutation.SetTags(v)
	return _c
}

// SetGames sets the "games" field.
func (_c *PlayersCreate) SetGames(v pq.Int64Array) *PlayersCreate {
	_c.mutation.SetGames(v)
	return _c
}

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

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

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

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

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

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

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_c *PlayersCreate) 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 *PlayersCreate) defaults() {
	if _, ok := _c.mutation.Status(); !ok {
		v := players.DefaultStatus
		_c.mutation.SetStatus(v)
	}
	if _, ok := _c.mutation.Gender(); !ok {
		v := players.DefaultGender
		_c.mutation.SetGender(v)
	}
	if _, ok := _c.mutation.Rating(); !ok {
		v := players.DefaultRating
		_c.mutation.SetRating(v)
	}
	if _, ok := _c.mutation.TotalOrders(); !ok {
		v := players.DefaultTotalOrders
		_c.mutation.SetTotalOrders(v)
	}
	if _, ok := _c.mutation.CompletedOrders(); !ok {
		v := players.DefaultCompletedOrders
		_c.mutation.SetCompletedOrders(v)
	}
	if _, ok := _c.mutation.Tags(); !ok {
		v := players.DefaultTags
		_c.mutation.SetTags(v)
	}
	if _, ok := _c.mutation.Games(); !ok {
		v := players.DefaultGames
		_c.mutation.SetGames(v)
	}
	if _, ok := _c.mutation.CreatedAt(); !ok {
		v := players.DefaultCreatedAt()
		_c.mutation.SetCreatedAt(v)
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		v := players.DefaultUpdatedAt()
		_c.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_c *PlayersCreate) check() error {
	if _, ok := _c.mutation.UserID(); !ok {
		return &ValidationError{Name: "user_id", err: errors.New(`models: missing required field "Players.user_id"`)}
	}
	if _, ok := _c.mutation.Status(); !ok {
		return &ValidationError{Name: "status", err: errors.New(`models: missing required field "Players.status"`)}
	}
	if v, ok := _c.mutation.Status(); ok {
		if err := players.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`models: validator failed for field "Players.status": %w`, err)}
		}
	}
	if _, ok := _c.mutation.Gender(); !ok {
		return &ValidationError{Name: "gender", err: errors.New(`models: missing required field "Players.gender"`)}
	}
	if _, ok := _c.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`models: missing required field "Players.created_at"`)}
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`models: missing required field "Players.updated_at"`)}
	}
	return nil
}

func (_c *PlayersCreate) sqlSave(ctx context.Context) (*Players, 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 *PlayersCreate) createSpec() (*Players, *sqlgraph.CreateSpec) {
	var (
		_node = &Players{config: _c.config}
		_spec = sqlgraph.NewCreateSpec(players.Table, sqlgraph.NewFieldSpec(players.FieldID, field.TypeInt64))
	)
	if id, ok := _c.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := _c.mutation.UserID(); ok {
		_spec.SetField(players.FieldUserID, field.TypeInt64, value)
		_node.UserID = value
	}
	if value, ok := _c.mutation.Status(); ok {
		_spec.SetField(players.FieldStatus, field.TypeString, value)
		_node.Status = value
	}
	if value, ok := _c.mutation.Gender(); ok {
		_spec.SetField(players.FieldGender, field.TypeBool, value)
		_node.Gender = value
	}
	if value, ok := _c.mutation.Rating(); ok {
		_spec.SetField(players.FieldRating, field.TypeOther, value)
		_node.Rating = value
	}
	if value, ok := _c.mutation.TotalOrders(); ok {
		_spec.SetField(players.FieldTotalOrders, field.TypeInt, value)
		_node.TotalOrders = value
	}
	if value, ok := _c.mutation.CompletedOrders(); ok {
		_spec.SetField(players.FieldCompletedOrders, field.TypeInt, value)
		_node.CompletedOrders = value
	}
	if value, ok := _c.mutation.ShopID(); ok {
		_spec.SetField(players.FieldShopID, field.TypeInt64, value)
		_node.ShopID = &value
	}
	if value, ok := _c.mutation.Tags(); ok {
		tags := types.TextArray{Elements: append([]string{}, value...), Valid: true}
		_spec.SetField(players.FieldTags, field.TypeOther, tags)
		_node.Tags = value
	}
	if value, ok := _c.mutation.Games(); ok {
		_spec.SetField(players.FieldGames, field.TypeOther, value)
		_node.Games = value
	}
	if value, ok := _c.mutation.CreatedAt(); ok {
		_spec.SetField(players.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := _c.mutation.UpdatedAt(); ok {
		_spec.SetField(players.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	return _node, _spec
}

// PlayersCreateBulk is the builder for creating many Players entities in bulk.
type PlayersCreateBulk struct {
	config
	err      error
	builders []*PlayersCreate
}

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

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

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