// Code generated by ent, DO NOT EDIT.

package models

import (
	"context"
	"errors"
	"fmt"
	"juwan-backend/app/users/rpc/internal/models/userpreferences"
	"time"

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

// UserPreferencesCreate is the builder for creating a UserPreferences entity.
type UserPreferencesCreate struct {
	config
	mutation *UserPreferencesMutation
	hooks    []Hook
}

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

// SetNotificationOrder sets the "notification_order" field.
func (_c *UserPreferencesCreate) SetNotificationOrder(v bool) *UserPreferencesCreate {
	_c.mutation.SetNotificationOrder(v)
	return _c
}

// SetNillableNotificationOrder sets the "notification_order" field if the given value is not nil.
func (_c *UserPreferencesCreate) SetNillableNotificationOrder(v *bool) *UserPreferencesCreate {
	if v != nil {
		_c.SetNotificationOrder(*v)
	}
	return _c
}

// SetNotificationCommunity sets the "notification_community" field.
func (_c *UserPreferencesCreate) SetNotificationCommunity(v bool) *UserPreferencesCreate {
	_c.mutation.SetNotificationCommunity(v)
	return _c
}

// SetNillableNotificationCommunity sets the "notification_community" field if the given value is not nil.
func (_c *UserPreferencesCreate) SetNillableNotificationCommunity(v *bool) *UserPreferencesCreate {
	if v != nil {
		_c.SetNotificationCommunity(*v)
	}
	return _c
}

// SetNotificationSystem sets the "notification_system" field.
func (_c *UserPreferencesCreate) SetNotificationSystem(v bool) *UserPreferencesCreate {
	_c.mutation.SetNotificationSystem(v)
	return _c
}

// SetNillableNotificationSystem sets the "notification_system" field if the given value is not nil.
func (_c *UserPreferencesCreate) SetNillableNotificationSystem(v *bool) *UserPreferencesCreate {
	if v != nil {
		_c.SetNotificationSystem(*v)
	}
	return _c
}

// SetTheme sets the "theme" field.
func (_c *UserPreferencesCreate) SetTheme(v string) *UserPreferencesCreate {
	_c.mutation.SetTheme(v)
	return _c
}

// SetNillableTheme sets the "theme" field if the given value is not nil.
func (_c *UserPreferencesCreate) SetNillableTheme(v *string) *UserPreferencesCreate {
	if v != nil {
		_c.SetTheme(*v)
	}
	return _c
}

// SetLanguage sets the "language" field.
func (_c *UserPreferencesCreate) SetLanguage(v string) *UserPreferencesCreate {
	_c.mutation.SetLanguage(v)
	return _c
}

// SetNillableLanguage sets the "language" field if the given value is not nil.
func (_c *UserPreferencesCreate) SetNillableLanguage(v *string) *UserPreferencesCreate {
	if v != nil {
		_c.SetLanguage(*v)
	}
	return _c
}

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

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

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

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_c *UserPreferencesCreate) 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 *UserPreferencesCreate) defaults() {
	if _, ok := _c.mutation.NotificationOrder(); !ok {
		v := userpreferences.DefaultNotificationOrder
		_c.mutation.SetNotificationOrder(v)
	}
	if _, ok := _c.mutation.NotificationCommunity(); !ok {
		v := userpreferences.DefaultNotificationCommunity
		_c.mutation.SetNotificationCommunity(v)
	}
	if _, ok := _c.mutation.NotificationSystem(); !ok {
		v := userpreferences.DefaultNotificationSystem
		_c.mutation.SetNotificationSystem(v)
	}
	if _, ok := _c.mutation.Theme(); !ok {
		v := userpreferences.DefaultTheme
		_c.mutation.SetTheme(v)
	}
	if _, ok := _c.mutation.Language(); !ok {
		v := userpreferences.DefaultLanguage
		_c.mutation.SetLanguage(v)
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		v := userpreferences.DefaultUpdatedAt()
		_c.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_c *UserPreferencesCreate) check() error {
	if _, ok := _c.mutation.UserID(); !ok {
		return &ValidationError{Name: "user_id", err: errors.New(`models: missing required field "UserPreferences.user_id"`)}
	}
	if _, ok := _c.mutation.NotificationOrder(); !ok {
		return &ValidationError{Name: "notification_order", err: errors.New(`models: missing required field "UserPreferences.notification_order"`)}
	}
	if _, ok := _c.mutation.NotificationCommunity(); !ok {
		return &ValidationError{Name: "notification_community", err: errors.New(`models: missing required field "UserPreferences.notification_community"`)}
	}
	if _, ok := _c.mutation.NotificationSystem(); !ok {
		return &ValidationError{Name: "notification_system", err: errors.New(`models: missing required field "UserPreferences.notification_system"`)}
	}
	if _, ok := _c.mutation.Theme(); !ok {
		return &ValidationError{Name: "theme", err: errors.New(`models: missing required field "UserPreferences.theme"`)}
	}
	if _, ok := _c.mutation.Language(); !ok {
		return &ValidationError{Name: "language", err: errors.New(`models: missing required field "UserPreferences.language"`)}
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`models: missing required field "UserPreferences.updated_at"`)}
	}
	return nil
}

func (_c *UserPreferencesCreate) sqlSave(ctx context.Context) (*UserPreferences, 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
	}
	id := _spec.ID.Value.(int64)
	_node.ID = int(id)
	_c.mutation.id = &_node.ID
	_c.mutation.done = true
	return _node, nil
}

func (_c *UserPreferencesCreate) createSpec() (*UserPreferences, *sqlgraph.CreateSpec) {
	var (
		_node = &UserPreferences{config: _c.config}
		_spec = sqlgraph.NewCreateSpec(userpreferences.Table, sqlgraph.NewFieldSpec(userpreferences.FieldID, field.TypeInt))
	)
	if value, ok := _c.mutation.UserID(); ok {
		_spec.SetField(userpreferences.FieldUserID, field.TypeInt64, value)
		_node.UserID = value
	}
	if value, ok := _c.mutation.NotificationOrder(); ok {
		_spec.SetField(userpreferences.FieldNotificationOrder, field.TypeBool, value)
		_node.NotificationOrder = value
	}
	if value, ok := _c.mutation.NotificationCommunity(); ok {
		_spec.SetField(userpreferences.FieldNotificationCommunity, field.TypeBool, value)
		_node.NotificationCommunity = value
	}
	if value, ok := _c.mutation.NotificationSystem(); ok {
		_spec.SetField(userpreferences.FieldNotificationSystem, field.TypeBool, value)
		_node.NotificationSystem = value
	}
	if value, ok := _c.mutation.Theme(); ok {
		_spec.SetField(userpreferences.FieldTheme, field.TypeString, value)
		_node.Theme = value
	}
	if value, ok := _c.mutation.Language(); ok {
		_spec.SetField(userpreferences.FieldLanguage, field.TypeString, value)
		_node.Language = value
	}
	if value, ok := _c.mutation.UpdatedAt(); ok {
		_spec.SetField(userpreferences.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	return _node, _spec
}

// UserPreferencesCreateBulk is the builder for creating many UserPreferences entities in bulk.
type UserPreferencesCreateBulk struct {
	config
	err      error
	builders []*UserPreferencesCreate
}

// Save creates the UserPreferences entities in the database.
func (_c *UserPreferencesCreateBulk) Save(ctx context.Context) ([]*UserPreferences, error) {
	if _c.err != nil {
		return nil, _c.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
	nodes := make([]*UserPreferences, 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.(*UserPreferencesMutation)
				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 {
					id := specs[i].ID.Value.(int64)
					nodes[i].ID = int(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 *UserPreferencesCreateBulk) SaveX(ctx context.Context) []*UserPreferences {
	v, err := _c.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

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

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