// Code generated by ent, DO NOT EDIT.

package models

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

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

// GamesUpdate is the builder for updating Games entities.
type GamesUpdate struct {
	config
	hooks    []Hook
	mutation *GamesMutation
}

// Where appends a list predicates to the GamesUpdate builder.
func (_u *GamesUpdate) Where(ps ...predicate.Games) *GamesUpdate {
	_u.mutation.Where(ps...)
	return _u
}

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

// SetNillableName sets the "name" field if the given value is not nil.
func (_u *GamesUpdate) SetNillableName(v *string) *GamesUpdate {
	if v != nil {
		_u.SetName(*v)
	}
	return _u
}

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

// SetNillableIcon sets the "icon" field if the given value is not nil.
func (_u *GamesUpdate) SetNillableIcon(v *string) *GamesUpdate {
	if v != nil {
		_u.SetIcon(*v)
	}
	return _u
}

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

// SetNillableCategory sets the "category" field if the given value is not nil.
func (_u *GamesUpdate) SetNillableCategory(v *string) *GamesUpdate {
	if v != nil {
		_u.SetCategory(*v)
	}
	return _u
}

// SetSortOrder sets the "sort_order" field.
func (_u *GamesUpdate) SetSortOrder(v int) *GamesUpdate {
	_u.mutation.ResetSortOrder()
	_u.mutation.SetSortOrder(v)
	return _u
}

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

// AddSortOrder adds value to the "sort_order" field.
func (_u *GamesUpdate) AddSortOrder(v int) *GamesUpdate {
	_u.mutation.AddSortOrder(v)
	return _u
}

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

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

// ClearIsActive clears the value of the "is_active" field.
func (_u *GamesUpdate) ClearIsActive() *GamesUpdate {
	_u.mutation.ClearIsActive()
	return _u
}

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

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

// Save executes the query and returns the number of nodes affected by the update operation.
func (_u *GamesUpdate) Save(ctx context.Context) (int, error) {
	_u.defaults()
	return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (_u *GamesUpdate) SaveX(ctx context.Context) int {
	affected, err := _u.Save(ctx)
	if err != nil {
		panic(err)
	}
	return affected
}

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

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

// defaults sets the default values of the builder before save.
func (_u *GamesUpdate) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := games.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_u *GamesUpdate) check() error {
	if v, ok := _u.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 v, ok := _u.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)}
		}
	}
	return nil
}

func (_u *GamesUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(games.Table, games.Columns, sqlgraph.NewFieldSpec(games.FieldID, field.TypeInt64))
	if ps := _u.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := _u.mutation.Name(); ok {
		_spec.SetField(games.FieldName, field.TypeString, value)
	}
	if value, ok := _u.mutation.Icon(); ok {
		_spec.SetField(games.FieldIcon, field.TypeString, value)
	}
	if value, ok := _u.mutation.Category(); ok {
		_spec.SetField(games.FieldCategory, field.TypeString, value)
	}
	if value, ok := _u.mutation.SortOrder(); ok {
		_spec.SetField(games.FieldSortOrder, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedSortOrder(); ok {
		_spec.AddField(games.FieldSortOrder, field.TypeInt, value)
	}
	if value, ok := _u.mutation.IsActive(); ok {
		_spec.SetField(games.FieldIsActive, field.TypeBool, value)
	}
	if _u.mutation.IsActiveCleared() {
		_spec.ClearField(games.FieldIsActive, field.TypeBool)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(games.FieldUpdatedAt, field.TypeTime, value)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{games.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// GamesUpdateOne is the builder for updating a single Games entity.
type GamesUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *GamesMutation
}

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

// SetNillableName sets the "name" field if the given value is not nil.
func (_u *GamesUpdateOne) SetNillableName(v *string) *GamesUpdateOne {
	if v != nil {
		_u.SetName(*v)
	}
	return _u
}

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

// SetNillableIcon sets the "icon" field if the given value is not nil.
func (_u *GamesUpdateOne) SetNillableIcon(v *string) *GamesUpdateOne {
	if v != nil {
		_u.SetIcon(*v)
	}
	return _u
}

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

// SetNillableCategory sets the "category" field if the given value is not nil.
func (_u *GamesUpdateOne) SetNillableCategory(v *string) *GamesUpdateOne {
	if v != nil {
		_u.SetCategory(*v)
	}
	return _u
}

// SetSortOrder sets the "sort_order" field.
func (_u *GamesUpdateOne) SetSortOrder(v int) *GamesUpdateOne {
	_u.mutation.ResetSortOrder()
	_u.mutation.SetSortOrder(v)
	return _u
}

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

// AddSortOrder adds value to the "sort_order" field.
func (_u *GamesUpdateOne) AddSortOrder(v int) *GamesUpdateOne {
	_u.mutation.AddSortOrder(v)
	return _u
}

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

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

// ClearIsActive clears the value of the "is_active" field.
func (_u *GamesUpdateOne) ClearIsActive() *GamesUpdateOne {
	_u.mutation.ClearIsActive()
	return _u
}

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

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

// Where appends a list predicates to the GamesUpdate builder.
func (_u *GamesUpdateOne) Where(ps ...predicate.Games) *GamesUpdateOne {
	_u.mutation.Where(ps...)
	return _u
}

// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (_u *GamesUpdateOne) Select(field string, fields ...string) *GamesUpdateOne {
	_u.fields = append([]string{field}, fields...)
	return _u
}

// Save executes the query and returns the updated Games entity.
func (_u *GamesUpdateOne) Save(ctx context.Context) (*Games, error) {
	_u.defaults()
	return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (_u *GamesUpdateOne) SaveX(ctx context.Context) *Games {
	node, err := _u.Save(ctx)
	if err != nil {
		panic(err)
	}
	return node
}

// Exec executes the query on the entity.
func (_u *GamesUpdateOne) Exec(ctx context.Context) error {
	_, err := _u.Save(ctx)
	return err
}

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

// defaults sets the default values of the builder before save.
func (_u *GamesUpdateOne) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := games.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_u *GamesUpdateOne) check() error {
	if v, ok := _u.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 v, ok := _u.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)}
		}
	}
	return nil
}

func (_u *GamesUpdateOne) sqlSave(ctx context.Context) (_node *Games, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(games.Table, games.Columns, sqlgraph.NewFieldSpec(games.FieldID, field.TypeInt64))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`models: missing "Games.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := _u.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, games.FieldID)
		for _, f := range fields {
			if !games.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("models: invalid field %q for query", f)}
			}
			if f != games.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := _u.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := _u.mutation.Name(); ok {
		_spec.SetField(games.FieldName, field.TypeString, value)
	}
	if value, ok := _u.mutation.Icon(); ok {
		_spec.SetField(games.FieldIcon, field.TypeString, value)
	}
	if value, ok := _u.mutation.Category(); ok {
		_spec.SetField(games.FieldCategory, field.TypeString, value)
	}
	if value, ok := _u.mutation.SortOrder(); ok {
		_spec.SetField(games.FieldSortOrder, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedSortOrder(); ok {
		_spec.AddField(games.FieldSortOrder, field.TypeInt, value)
	}
	if value, ok := _u.mutation.IsActive(); ok {
		_spec.SetField(games.FieldIsActive, field.TypeBool, value)
	}
	if _u.mutation.IsActiveCleared() {
		_spec.ClearField(games.FieldIsActive, field.TypeBool)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(games.FieldUpdatedAt, field.TypeTime, value)
	}
	_node = &Games{config: _u.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{games.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
