// Code generated by ent, DO NOT EDIT.

package models

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

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

// PlayerServicesCreate is the builder for creating a PlayerServices entity.
type PlayerServicesCreate struct {
	config
	mutation *PlayerServicesMutation
	hooks    []Hook
}

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

// SetGameID sets the "game_id" field.
func (_c *PlayerServicesCreate) SetGameID(v int64) *PlayerServicesCreate {
	_c.mutation.SetGameID(v)
	return _c
}

// SetTitle sets the "title" field.
func (_c *PlayerServicesCreate) SetTitle(v string) *PlayerServicesCreate {
	_c.mutation.SetTitle(v)
	return _c
}

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

// SetNillableDescription sets the "description" field if the given value is not nil.
func (_c *PlayerServicesCreate) SetNillableDescription(v *string) *PlayerServicesCreate {
	if v != nil {
		_c.SetDescription(*v)
	}
	return _c
}

// SetPrice sets the "price" field.
func (_c *PlayerServicesCreate) SetPrice(v decimal.Decimal) *PlayerServicesCreate {
	_c.mutation.SetPrice(v)
	return _c
}

// SetUnit sets the "unit" field.
func (_c *PlayerServicesCreate) SetUnit(v string) *PlayerServicesCreate {
	_c.mutation.SetUnit(v)
	return _c
}

// SetRankRange sets the "rank_range" field.
func (_c *PlayerServicesCreate) SetRankRange(v string) *PlayerServicesCreate {
	_c.mutation.SetRankRange(v)
	return _c
}

// SetNillableRankRange sets the "rank_range" field if the given value is not nil.
func (_c *PlayerServicesCreate) SetNillableRankRange(v *string) *PlayerServicesCreate {
	if v != nil {
		_c.SetRankRange(*v)
	}
	return _c
}

// SetAvailability sets the "availability" field.
func (_c *PlayerServicesCreate) SetAvailability(v []string) *PlayerServicesCreate {
	_c.mutation.SetAvailability(v)
	return _c
}

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

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

// SetIsActive sets the "is_active" field.
func (_c *PlayerServicesCreate) SetIsActive(v bool) *PlayerServicesCreate {
	_c.mutation.SetIsActive(v)
	return _c
}

// SetNillableIsActive sets the "is_active" field if the given value is not nil.
func (_c *PlayerServicesCreate) SetNillableIsActive(v *bool) *PlayerServicesCreate {
	if v != nil {
		_c.SetIsActive(*v)
	}
	return _c
}

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

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

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

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

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

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

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_c *PlayerServicesCreate) 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 *PlayerServicesCreate) defaults() {
	if _, ok := _c.mutation.Availability(); !ok {
		v := playerservices.DefaultAvailability
		_c.mutation.SetAvailability(v)
	}
	if _, ok := _c.mutation.Rating(); !ok {
		v := playerservices.DefaultRating
		_c.mutation.SetRating(v)
	}
	if _, ok := _c.mutation.IsActive(); !ok {
		v := playerservices.DefaultIsActive
		_c.mutation.SetIsActive(v)
	}
	if _, ok := _c.mutation.CreatedAt(); !ok {
		v := playerservices.DefaultCreatedAt()
		_c.mutation.SetCreatedAt(v)
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		v := playerservices.DefaultUpdatedAt()
		_c.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_c *PlayerServicesCreate) check() error {
	if _, ok := _c.mutation.PlayerID(); !ok {
		return &ValidationError{Name: "player_id", err: errors.New(`models: missing required field "PlayerServices.player_id"`)}
	}
	if _, ok := _c.mutation.GameID(); !ok {
		return &ValidationError{Name: "game_id", err: errors.New(`models: missing required field "PlayerServices.game_id"`)}
	}
	if _, ok := _c.mutation.Title(); !ok {
		return &ValidationError{Name: "title", err: errors.New(`models: missing required field "PlayerServices.title"`)}
	}
	if v, ok := _c.mutation.Title(); ok {
		if err := playerservices.TitleValidator(v); err != nil {
			return &ValidationError{Name: "title", err: fmt.Errorf(`models: validator failed for field "PlayerServices.title": %w`, err)}
		}
	}
	if _, ok := _c.mutation.Price(); !ok {
		return &ValidationError{Name: "price", err: errors.New(`models: missing required field "PlayerServices.price"`)}
	}
	if _, ok := _c.mutation.Unit(); !ok {
		return &ValidationError{Name: "unit", err: errors.New(`models: missing required field "PlayerServices.unit"`)}
	}
	if v, ok := _c.mutation.Unit(); ok {
		if err := playerservices.UnitValidator(v); err != nil {
			return &ValidationError{Name: "unit", err: fmt.Errorf(`models: validator failed for field "PlayerServices.unit": %w`, err)}
		}
	}
	if v, ok := _c.mutation.RankRange(); ok {
		if err := playerservices.RankRangeValidator(v); err != nil {
			return &ValidationError{Name: "rank_range", err: fmt.Errorf(`models: validator failed for field "PlayerServices.rank_range": %w`, err)}
		}
	}
	if _, ok := _c.mutation.Rating(); !ok {
		return &ValidationError{Name: "rating", err: errors.New(`models: missing required field "PlayerServices.rating"`)}
	}
	if _, ok := _c.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`models: missing required field "PlayerServices.created_at"`)}
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`models: missing required field "PlayerServices.updated_at"`)}
	}
	return nil
}

func (_c *PlayerServicesCreate) sqlSave(ctx context.Context) (*PlayerServices, 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 *PlayerServicesCreate) createSpec() (*PlayerServices, *sqlgraph.CreateSpec) {
	var (
		_node = &PlayerServices{config: _c.config}
		_spec = sqlgraph.NewCreateSpec(playerservices.Table, sqlgraph.NewFieldSpec(playerservices.FieldID, field.TypeInt64))
	)
	if id, ok := _c.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := _c.mutation.PlayerID(); ok {
		_spec.SetField(playerservices.FieldPlayerID, field.TypeInt64, value)
		_node.PlayerID = value
	}
	if value, ok := _c.mutation.GameID(); ok {
		_spec.SetField(playerservices.FieldGameID, field.TypeInt64, value)
		_node.GameID = value
	}
	if value, ok := _c.mutation.Title(); ok {
		_spec.SetField(playerservices.FieldTitle, field.TypeString, value)
		_node.Title = value
	}
	if value, ok := _c.mutation.Description(); ok {
		_spec.SetField(playerservices.FieldDescription, field.TypeString, value)
		_node.Description = &value
	}
	if value, ok := _c.mutation.Price(); ok {
		_spec.SetField(playerservices.FieldPrice, field.TypeOther, value)
		_node.Price = value
	}
	if value, ok := _c.mutation.Unit(); ok {
		_spec.SetField(playerservices.FieldUnit, field.TypeString, value)
		_node.Unit = value
	}
	if value, ok := _c.mutation.RankRange(); ok {
		_spec.SetField(playerservices.FieldRankRange, field.TypeString, value)
		_node.RankRange = &value
	}
	if value, ok := _c.mutation.Availability(); ok {
		availability := types.TextArray{Elements: append([]string{}, value...), Valid: true}
		_spec.SetField(playerservices.FieldAvailability, field.TypeOther, availability)
		_node.Availability = value
	}
	if value, ok := _c.mutation.Rating(); ok {
		_spec.SetField(playerservices.FieldRating, field.TypeOther, value)
		_node.Rating = value
	}
	if value, ok := _c.mutation.IsActive(); ok {
		_spec.SetField(playerservices.FieldIsActive, field.TypeBool, value)
		_node.IsActive = value
	}
	if value, ok := _c.mutation.CreatedAt(); ok {
		_spec.SetField(playerservices.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := _c.mutation.UpdatedAt(); ok {
		_spec.SetField(playerservices.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	return _node, _spec
}

// PlayerServicesCreateBulk is the builder for creating many PlayerServices entities in bulk.
type PlayerServicesCreateBulk struct {
	config
	err      error
	builders []*PlayerServicesCreate
}

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

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

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