// Code generated by ent, DO NOT EDIT.

package models

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

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

// ShopPlayersCreate is the builder for creating a ShopPlayers entity.
type ShopPlayersCreate struct {
	config
	mutation *ShopPlayersMutation
	hooks    []Hook
}

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

// SetPlayerID sets the "player_id" field.
func (_c *ShopPlayersCreate) SetPlayerID(v int64) *ShopPlayersCreate {
	_c.mutation.SetPlayerID(v)
	return _c
}

// SetIsPrimary sets the "is_primary" field.
func (_c *ShopPlayersCreate) SetIsPrimary(v bool) *ShopPlayersCreate {
	_c.mutation.SetIsPrimary(v)
	return _c
}

// SetNillableIsPrimary sets the "is_primary" field if the given value is not nil.
func (_c *ShopPlayersCreate) SetNillableIsPrimary(v *bool) *ShopPlayersCreate {
	if v != nil {
		_c.SetIsPrimary(*v)
	}
	return _c
}

// SetJoinedAt sets the "joined_at" field.
func (_c *ShopPlayersCreate) SetJoinedAt(v time.Time) *ShopPlayersCreate {
	_c.mutation.SetJoinedAt(v)
	return _c
}

// SetNillableJoinedAt sets the "joined_at" field if the given value is not nil.
func (_c *ShopPlayersCreate) SetNillableJoinedAt(v *time.Time) *ShopPlayersCreate {
	if v != nil {
		_c.SetJoinedAt(*v)
	}
	return _c
}

// SetLeftAt sets the "left_at" field.
func (_c *ShopPlayersCreate) SetLeftAt(v time.Time) *ShopPlayersCreate {
	_c.mutation.SetLeftAt(v)
	return _c
}

// SetNillableLeftAt sets the "left_at" field if the given value is not nil.
func (_c *ShopPlayersCreate) SetNillableLeftAt(v *time.Time) *ShopPlayersCreate {
	if v != nil {
		_c.SetLeftAt(*v)
	}
	return _c
}

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

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

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_c *ShopPlayersCreate) 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 *ShopPlayersCreate) defaults() {
	if _, ok := _c.mutation.IsPrimary(); !ok {
		v := shopplayers.DefaultIsPrimary
		_c.mutation.SetIsPrimary(v)
	}
	if _, ok := _c.mutation.JoinedAt(); !ok {
		v := shopplayers.DefaultJoinedAt()
		_c.mutation.SetJoinedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_c *ShopPlayersCreate) check() error {
	if _, ok := _c.mutation.ShopID(); !ok {
		return &ValidationError{Name: "shop_id", err: errors.New(`models: missing required field "ShopPlayers.shop_id"`)}
	}
	if _, ok := _c.mutation.PlayerID(); !ok {
		return &ValidationError{Name: "player_id", err: errors.New(`models: missing required field "ShopPlayers.player_id"`)}
	}
	if _, ok := _c.mutation.JoinedAt(); !ok {
		return &ValidationError{Name: "joined_at", err: errors.New(`models: missing required field "ShopPlayers.joined_at"`)}
	}
	return nil
}

func (_c *ShopPlayersCreate) sqlSave(ctx context.Context) (*ShopPlayers, 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 *ShopPlayersCreate) createSpec() (*ShopPlayers, *sqlgraph.CreateSpec) {
	var (
		_node = &ShopPlayers{config: _c.config}
		_spec = sqlgraph.NewCreateSpec(shopplayers.Table, sqlgraph.NewFieldSpec(shopplayers.FieldID, field.TypeInt64))
	)
	if id, ok := _c.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := _c.mutation.ShopID(); ok {
		_spec.SetField(shopplayers.FieldShopID, field.TypeInt64, value)
		_node.ShopID = value
	}
	if value, ok := _c.mutation.PlayerID(); ok {
		_spec.SetField(shopplayers.FieldPlayerID, field.TypeInt64, value)
		_node.PlayerID = value
	}
	if value, ok := _c.mutation.IsPrimary(); ok {
		_spec.SetField(shopplayers.FieldIsPrimary, field.TypeBool, value)
		_node.IsPrimary = value
	}
	if value, ok := _c.mutation.JoinedAt(); ok {
		_spec.SetField(shopplayers.FieldJoinedAt, field.TypeTime, value)
		_node.JoinedAt = value
	}
	if value, ok := _c.mutation.LeftAt(); ok {
		_spec.SetField(shopplayers.FieldLeftAt, field.TypeTime, value)
		_node.LeftAt = &value
	}
	return _node, _spec
}

// ShopPlayersCreateBulk is the builder for creating many ShopPlayers entities in bulk.
type ShopPlayersCreateBulk struct {
	config
	err      error
	builders []*ShopPlayersCreate
}

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

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

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