// Code generated by ent, DO NOT EDIT.

package models

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

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

// GamesCreate is the builder for creating a Games entity.
type GamesCreate struct {
	config
	mutation *GamesMutation
	hooks    []Hook
}

// SetName sets the "name" field.
func (_c *GamesCreate) SetName(v string) *GamesCreate {
	_c.mutation.SetName(v)
	return _c
}

// SetIcon sets the "icon" field.
func (_c *GamesCreate) SetIcon(v string) *GamesCreate {
	_c.mutation.SetIcon(v)
	return _c
}

// SetCategory sets the "category" field.
func (_c *GamesCreate) SetCategory(v string) *GamesCreate {
	_c.mutation.SetCategory(v)
	return _c
}

// SetSortOrder sets the "sort_order" field.
func (_c *GamesCreate) SetSortOrder(v int) *GamesCreate {
	_c.mutation.SetSortOrder(v)
	return _c
}

// SetNillableSortOrder sets the "sort_order" field if the given value is not nil.
func (_c *GamesCreate) SetNillableSortOrder(v *int) *GamesCreate {
	if v != nil {
		_c.SetSortOrder(*v)
	}
	return _c
}

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

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

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

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

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

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

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

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

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_c *GamesCreate) 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 *GamesCreate) defaults() {
	if _, ok := _c.mutation.SortOrder(); !ok {
		v := games.DefaultSortOrder
		_c.mutation.SetSortOrder(v)
	}
	if _, ok := _c.mutation.IsActive(); !ok {
		v := games.DefaultIsActive
		_c.mutation.SetIsActive(v)
	}
	if _, ok := _c.mutation.CreatedAt(); !ok {
		v := games.DefaultCreatedAt()
		_c.mutation.SetCreatedAt(v)
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		v := games.DefaultUpdatedAt()
		_c.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_c *GamesCreate) check() error {
	if _, ok := _c.mutation.Name(); !ok {
		return &ValidationError{Name: "name", err: errors.New(`models: missing required field "Games.name"`)}
	}
	if v, ok := _c.mutation.Name(); ok {
		if err := games.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`models: validator failed for field "Games.name": %w`, err)}
		}
	}
	if _, ok := _c.mutation.Icon(); !ok {
		return &ValidationError{Name: "icon", err: errors.New(`models: missing required field "Games.icon"`)}
	}
	if _, ok := _c.mutation.Category(); !ok {
		return &ValidationError{Name: "category", err: errors.New(`models: missing required field "Games.category"`)}
	}
	if v, ok := _c.mutation.Category(); ok {
		if err := games.CategoryValidator(v); err != nil {
			return &ValidationError{Name: "category", err: fmt.Errorf(`models: validator failed for field "Games.category": %w`, err)}
		}
	}
	if _, ok := _c.mutation.SortOrder(); !ok {
		return &ValidationError{Name: "sort_order", err: errors.New(`models: missing required field "Games.sort_order"`)}
	}
	if _, ok := _c.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`models: missing required field "Games.created_at"`)}
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`models: missing required field "Games.updated_at"`)}
	}
	return nil
}

func (_c *GamesCreate) sqlSave(ctx context.Context) (*Games, 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 *GamesCreate) createSpec() (*Games, *sqlgraph.CreateSpec) {
	var (
		_node = &Games{config: _c.config}
		_spec = sqlgraph.NewCreateSpec(games.Table, sqlgraph.NewFieldSpec(games.FieldID, field.TypeInt64))
	)
	if id, ok := _c.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := _c.mutation.Name(); ok {
		_spec.SetField(games.FieldName, field.TypeString, value)
		_node.Name = value
	}
	if value, ok := _c.mutation.Icon(); ok {
		_spec.SetField(games.FieldIcon, field.TypeString, value)
		_node.Icon = value
	}
	if value, ok := _c.mutation.Category(); ok {
		_spec.SetField(games.FieldCategory, field.TypeString, value)
		_node.Category = value
	}
	if value, ok := _c.mutation.SortOrder(); ok {
		_spec.SetField(games.FieldSortOrder, field.TypeInt, value)
		_node.SortOrder = value
	}
	if value, ok := _c.mutation.IsActive(); ok {
		_spec.SetField(games.FieldIsActive, field.TypeBool, value)
		_node.IsActive = value
	}
	if value, ok := _c.mutation.CreatedAt(); ok {
		_spec.SetField(games.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := _c.mutation.UpdatedAt(); ok {
		_spec.SetField(games.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	return _node, _spec
}

// GamesCreateBulk is the builder for creating many Games entities in bulk.
type GamesCreateBulk struct {
	config
	err      error
	builders []*GamesCreate
}

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

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

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