// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/anzhiyu-c/anheyu-app/ent/user"
	"github.com/anzhiyu-c/anheyu-app/ent/userinstalledtheme"
)

// UserInstalledThemeCreate is the builder for creating a UserInstalledTheme entity.
type UserInstalledThemeCreate struct {
	config
	mutation *UserInstalledThemeMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

// SetDeletedAt sets the "deleted_at" field.
func (_c *UserInstalledThemeCreate) SetDeletedAt(v time.Time) *UserInstalledThemeCreate {
	_c.mutation.SetDeletedAt(v)
	return _c
}

// SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
func (_c *UserInstalledThemeCreate) SetNillableDeletedAt(v *time.Time) *UserInstalledThemeCreate {
	if v != nil {
		_c.SetDeletedAt(*v)
	}
	return _c
}

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

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

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

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

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

// SetThemeName sets the "theme_name" field.
func (_c *UserInstalledThemeCreate) SetThemeName(v string) *UserInstalledThemeCreate {
	_c.mutation.SetThemeName(v)
	return _c
}

// SetThemeMarketID sets the "theme_market_id" field.
func (_c *UserInstalledThemeCreate) SetThemeMarketID(v int) *UserInstalledThemeCreate {
	_c.mutation.SetThemeMarketID(v)
	return _c
}

// SetNillableThemeMarketID sets the "theme_market_id" field if the given value is not nil.
func (_c *UserInstalledThemeCreate) SetNillableThemeMarketID(v *int) *UserInstalledThemeCreate {
	if v != nil {
		_c.SetThemeMarketID(*v)
	}
	return _c
}

// SetIsCurrent sets the "is_current" field.
func (_c *UserInstalledThemeCreate) SetIsCurrent(v bool) *UserInstalledThemeCreate {
	_c.mutation.SetIsCurrent(v)
	return _c
}

// SetNillableIsCurrent sets the "is_current" field if the given value is not nil.
func (_c *UserInstalledThemeCreate) SetNillableIsCurrent(v *bool) *UserInstalledThemeCreate {
	if v != nil {
		_c.SetIsCurrent(*v)
	}
	return _c
}

// SetInstallTime sets the "install_time" field.
func (_c *UserInstalledThemeCreate) SetInstallTime(v time.Time) *UserInstalledThemeCreate {
	_c.mutation.SetInstallTime(v)
	return _c
}

// SetNillableInstallTime sets the "install_time" field if the given value is not nil.
func (_c *UserInstalledThemeCreate) SetNillableInstallTime(v *time.Time) *UserInstalledThemeCreate {
	if v != nil {
		_c.SetInstallTime(*v)
	}
	return _c
}

// SetUserThemeConfig sets the "user_theme_config" field.
func (_c *UserInstalledThemeCreate) SetUserThemeConfig(v map[string]interface{}) *UserInstalledThemeCreate {
	_c.mutation.SetUserThemeConfig(v)
	return _c
}

// SetInstalledVersion sets the "installed_version" field.
func (_c *UserInstalledThemeCreate) SetInstalledVersion(v string) *UserInstalledThemeCreate {
	_c.mutation.SetInstalledVersion(v)
	return _c
}

// SetNillableInstalledVersion sets the "installed_version" field if the given value is not nil.
func (_c *UserInstalledThemeCreate) SetNillableInstalledVersion(v *string) *UserInstalledThemeCreate {
	if v != nil {
		_c.SetInstalledVersion(*v)
	}
	return _c
}

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

// SetUser sets the "user" edge to the User entity.
func (_c *UserInstalledThemeCreate) SetUser(v *User) *UserInstalledThemeCreate {
	return _c.SetUserID(v.ID)
}

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

// Save creates the UserInstalledTheme in the database.
func (_c *UserInstalledThemeCreate) Save(ctx context.Context) (*UserInstalledTheme, error) {
	if err := _c.defaults(); err != nil {
		return nil, err
	}
	return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
}

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_c *UserInstalledThemeCreate) 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 *UserInstalledThemeCreate) defaults() error {
	if _, ok := _c.mutation.CreatedAt(); !ok {
		if userinstalledtheme.DefaultCreatedAt == nil {
			return fmt.Errorf("ent: uninitialized userinstalledtheme.DefaultCreatedAt (forgotten import ent/runtime?)")
		}
		v := userinstalledtheme.DefaultCreatedAt()
		_c.mutation.SetCreatedAt(v)
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		if userinstalledtheme.DefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized userinstalledtheme.DefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := userinstalledtheme.DefaultUpdatedAt()
		_c.mutation.SetUpdatedAt(v)
	}
	if _, ok := _c.mutation.IsCurrent(); !ok {
		v := userinstalledtheme.DefaultIsCurrent
		_c.mutation.SetIsCurrent(v)
	}
	if _, ok := _c.mutation.InstallTime(); !ok {
		if userinstalledtheme.DefaultInstallTime == nil {
			return fmt.Errorf("ent: uninitialized userinstalledtheme.DefaultInstallTime (forgotten import ent/runtime?)")
		}
		v := userinstalledtheme.DefaultInstallTime()
		_c.mutation.SetInstallTime(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (_c *UserInstalledThemeCreate) check() error {
	if _, ok := _c.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "UserInstalledTheme.created_at"`)}
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "UserInstalledTheme.updated_at"`)}
	}
	if _, ok := _c.mutation.UserID(); !ok {
		return &ValidationError{Name: "user_id", err: errors.New(`ent: missing required field "UserInstalledTheme.user_id"`)}
	}
	if _, ok := _c.mutation.ThemeName(); !ok {
		return &ValidationError{Name: "theme_name", err: errors.New(`ent: missing required field "UserInstalledTheme.theme_name"`)}
	}
	if v, ok := _c.mutation.ThemeName(); ok {
		if err := userinstalledtheme.ThemeNameValidator(v); err != nil {
			return &ValidationError{Name: "theme_name", err: fmt.Errorf(`ent: validator failed for field "UserInstalledTheme.theme_name": %w`, err)}
		}
	}
	if _, ok := _c.mutation.IsCurrent(); !ok {
		return &ValidationError{Name: "is_current", err: errors.New(`ent: missing required field "UserInstalledTheme.is_current"`)}
	}
	if _, ok := _c.mutation.InstallTime(); !ok {
		return &ValidationError{Name: "install_time", err: errors.New(`ent: missing required field "UserInstalledTheme.install_time"`)}
	}
	if v, ok := _c.mutation.InstalledVersion(); ok {
		if err := userinstalledtheme.InstalledVersionValidator(v); err != nil {
			return &ValidationError{Name: "installed_version", err: fmt.Errorf(`ent: validator failed for field "UserInstalledTheme.installed_version": %w`, err)}
		}
	}
	if len(_c.mutation.UserIDs()) == 0 {
		return &ValidationError{Name: "user", err: errors.New(`ent: missing required edge "UserInstalledTheme.user"`)}
	}
	return nil
}

func (_c *UserInstalledThemeCreate) sqlSave(ctx context.Context) (*UserInstalledTheme, 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 = uint(id)
	}
	_c.mutation.id = &_node.ID
	_c.mutation.done = true
	return _node, nil
}

func (_c *UserInstalledThemeCreate) createSpec() (*UserInstalledTheme, *sqlgraph.CreateSpec) {
	var (
		_node = &UserInstalledTheme{config: _c.config}
		_spec = sqlgraph.NewCreateSpec(userinstalledtheme.Table, sqlgraph.NewFieldSpec(userinstalledtheme.FieldID, field.TypeUint))
	)
	_spec.OnConflict = _c.conflict
	if id, ok := _c.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := _c.mutation.DeletedAt(); ok {
		_spec.SetField(userinstalledtheme.FieldDeletedAt, field.TypeTime, value)
		_node.DeletedAt = &value
	}
	if value, ok := _c.mutation.CreatedAt(); ok {
		_spec.SetField(userinstalledtheme.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := _c.mutation.UpdatedAt(); ok {
		_spec.SetField(userinstalledtheme.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := _c.mutation.ThemeName(); ok {
		_spec.SetField(userinstalledtheme.FieldThemeName, field.TypeString, value)
		_node.ThemeName = value
	}
	if value, ok := _c.mutation.ThemeMarketID(); ok {
		_spec.SetField(userinstalledtheme.FieldThemeMarketID, field.TypeInt, value)
		_node.ThemeMarketID = value
	}
	if value, ok := _c.mutation.IsCurrent(); ok {
		_spec.SetField(userinstalledtheme.FieldIsCurrent, field.TypeBool, value)
		_node.IsCurrent = value
	}
	if value, ok := _c.mutation.InstallTime(); ok {
		_spec.SetField(userinstalledtheme.FieldInstallTime, field.TypeTime, value)
		_node.InstallTime = value
	}
	if value, ok := _c.mutation.UserThemeConfig(); ok {
		_spec.SetField(userinstalledtheme.FieldUserThemeConfig, field.TypeJSON, value)
		_node.UserThemeConfig = value
	}
	if value, ok := _c.mutation.InstalledVersion(); ok {
		_spec.SetField(userinstalledtheme.FieldInstalledVersion, field.TypeString, value)
		_node.InstalledVersion = value
	}
	if nodes := _c.mutation.UserIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   userinstalledtheme.UserTable,
			Columns: []string{userinstalledtheme.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeUint),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.UserID = nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	return _node, _spec
}

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.UserInstalledTheme.Create().
//		SetDeletedAt(v).
//		OnConflict(
//			// Update the row with the new values
//			// the was proposed for insertion.
//			sql.ResolveWithNewValues(),
//		).
//		// Override some of the fields with custom
//		// update values.
//		Update(func(u *ent.UserInstalledThemeUpsert) {
//			SetDeletedAt(v+v).
//		}).
//		Exec(ctx)
func (_c *UserInstalledThemeCreate) OnConflict(opts ...sql.ConflictOption) *UserInstalledThemeUpsertOne {
	_c.conflict = opts
	return &UserInstalledThemeUpsertOne{
		create: _c,
	}
}

// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
//	client.UserInstalledTheme.Create().
//		OnConflict(sql.ConflictColumns(columns...)).
//		Exec(ctx)
func (_c *UserInstalledThemeCreate) OnConflictColumns(columns ...string) *UserInstalledThemeUpsertOne {
	_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
	return &UserInstalledThemeUpsertOne{
		create: _c,
	}
}

type (
	// UserInstalledThemeUpsertOne is the builder for "upsert"-ing
	//  one UserInstalledTheme node.
	UserInstalledThemeUpsertOne struct {
		create *UserInstalledThemeCreate
	}

	// UserInstalledThemeUpsert is the "OnConflict" setter.
	UserInstalledThemeUpsert struct {
		*sql.UpdateSet
	}
)

// SetDeletedAt sets the "deleted_at" field.
func (u *UserInstalledThemeUpsert) SetDeletedAt(v time.Time) *UserInstalledThemeUpsert {
	u.Set(userinstalledtheme.FieldDeletedAt, v)
	return u
}

// UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
func (u *UserInstalledThemeUpsert) UpdateDeletedAt() *UserInstalledThemeUpsert {
	u.SetExcluded(userinstalledtheme.FieldDeletedAt)
	return u
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (u *UserInstalledThemeUpsert) ClearDeletedAt() *UserInstalledThemeUpsert {
	u.SetNull(userinstalledtheme.FieldDeletedAt)
	return u
}

// SetUpdatedAt sets the "updated_at" field.
func (u *UserInstalledThemeUpsert) SetUpdatedAt(v time.Time) *UserInstalledThemeUpsert {
	u.Set(userinstalledtheme.FieldUpdatedAt, v)
	return u
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *UserInstalledThemeUpsert) UpdateUpdatedAt() *UserInstalledThemeUpsert {
	u.SetExcluded(userinstalledtheme.FieldUpdatedAt)
	return u
}

// SetUserID sets the "user_id" field.
func (u *UserInstalledThemeUpsert) SetUserID(v uint) *UserInstalledThemeUpsert {
	u.Set(userinstalledtheme.FieldUserID, v)
	return u
}

// UpdateUserID sets the "user_id" field to the value that was provided on create.
func (u *UserInstalledThemeUpsert) UpdateUserID() *UserInstalledThemeUpsert {
	u.SetExcluded(userinstalledtheme.FieldUserID)
	return u
}

// SetThemeName sets the "theme_name" field.
func (u *UserInstalledThemeUpsert) SetThemeName(v string) *UserInstalledThemeUpsert {
	u.Set(userinstalledtheme.FieldThemeName, v)
	return u
}

// UpdateThemeName sets the "theme_name" field to the value that was provided on create.
func (u *UserInstalledThemeUpsert) UpdateThemeName() *UserInstalledThemeUpsert {
	u.SetExcluded(userinstalledtheme.FieldThemeName)
	return u
}

// SetThemeMarketID sets the "theme_market_id" field.
func (u *UserInstalledThemeUpsert) SetThemeMarketID(v int) *UserInstalledThemeUpsert {
	u.Set(userinstalledtheme.FieldThemeMarketID, v)
	return u
}

// UpdateThemeMarketID sets the "theme_market_id" field to the value that was provided on create.
func (u *UserInstalledThemeUpsert) UpdateThemeMarketID() *UserInstalledThemeUpsert {
	u.SetExcluded(userinstalledtheme.FieldThemeMarketID)
	return u
}

// AddThemeMarketID adds v to the "theme_market_id" field.
func (u *UserInstalledThemeUpsert) AddThemeMarketID(v int) *UserInstalledThemeUpsert {
	u.Add(userinstalledtheme.FieldThemeMarketID, v)
	return u
}

// ClearThemeMarketID clears the value of the "theme_market_id" field.
func (u *UserInstalledThemeUpsert) ClearThemeMarketID() *UserInstalledThemeUpsert {
	u.SetNull(userinstalledtheme.FieldThemeMarketID)
	return u
}

// SetIsCurrent sets the "is_current" field.
func (u *UserInstalledThemeUpsert) SetIsCurrent(v bool) *UserInstalledThemeUpsert {
	u.Set(userinstalledtheme.FieldIsCurrent, v)
	return u
}

// UpdateIsCurrent sets the "is_current" field to the value that was provided on create.
func (u *UserInstalledThemeUpsert) UpdateIsCurrent() *UserInstalledThemeUpsert {
	u.SetExcluded(userinstalledtheme.FieldIsCurrent)
	return u
}

// SetInstallTime sets the "install_time" field.
func (u *UserInstalledThemeUpsert) SetInstallTime(v time.Time) *UserInstalledThemeUpsert {
	u.Set(userinstalledtheme.FieldInstallTime, v)
	return u
}

// UpdateInstallTime sets the "install_time" field to the value that was provided on create.
func (u *UserInstalledThemeUpsert) UpdateInstallTime() *UserInstalledThemeUpsert {
	u.SetExcluded(userinstalledtheme.FieldInstallTime)
	return u
}

// SetUserThemeConfig sets the "user_theme_config" field.
func (u *UserInstalledThemeUpsert) SetUserThemeConfig(v map[string]interface{}) *UserInstalledThemeUpsert {
	u.Set(userinstalledtheme.FieldUserThemeConfig, v)
	return u
}

// UpdateUserThemeConfig sets the "user_theme_config" field to the value that was provided on create.
func (u *UserInstalledThemeUpsert) UpdateUserThemeConfig() *UserInstalledThemeUpsert {
	u.SetExcluded(userinstalledtheme.FieldUserThemeConfig)
	return u
}

// ClearUserThemeConfig clears the value of the "user_theme_config" field.
func (u *UserInstalledThemeUpsert) ClearUserThemeConfig() *UserInstalledThemeUpsert {
	u.SetNull(userinstalledtheme.FieldUserThemeConfig)
	return u
}

// SetInstalledVersion sets the "installed_version" field.
func (u *UserInstalledThemeUpsert) SetInstalledVersion(v string) *UserInstalledThemeUpsert {
	u.Set(userinstalledtheme.FieldInstalledVersion, v)
	return u
}

// UpdateInstalledVersion sets the "installed_version" field to the value that was provided on create.
func (u *UserInstalledThemeUpsert) UpdateInstalledVersion() *UserInstalledThemeUpsert {
	u.SetExcluded(userinstalledtheme.FieldInstalledVersion)
	return u
}

// ClearInstalledVersion clears the value of the "installed_version" field.
func (u *UserInstalledThemeUpsert) ClearInstalledVersion() *UserInstalledThemeUpsert {
	u.SetNull(userinstalledtheme.FieldInstalledVersion)
	return u
}

// UpdateNewValues updates the mutable fields using the new values that were set on create except the ID field.
// Using this option is equivalent to using:
//
//	client.UserInstalledTheme.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(userinstalledtheme.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *UserInstalledThemeUpsertOne) UpdateNewValues() *UserInstalledThemeUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
		if _, exists := u.create.mutation.ID(); exists {
			s.SetIgnore(userinstalledtheme.FieldID)
		}
		if _, exists := u.create.mutation.CreatedAt(); exists {
			s.SetIgnore(userinstalledtheme.FieldCreatedAt)
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.UserInstalledTheme.Create().
//	    OnConflict(sql.ResolveWithIgnore()).
//	    Exec(ctx)
func (u *UserInstalledThemeUpsertOne) Ignore() *UserInstalledThemeUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
	return u
}

// DoNothing configures the conflict_action to `DO NOTHING`.
// Supported only by SQLite and PostgreSQL.
func (u *UserInstalledThemeUpsertOne) DoNothing() *UserInstalledThemeUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

// Update allows overriding fields `UPDATE` values. See the UserInstalledThemeCreate.OnConflict
// documentation for more info.
func (u *UserInstalledThemeUpsertOne) Update(set func(*UserInstalledThemeUpsert)) *UserInstalledThemeUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
		set(&UserInstalledThemeUpsert{UpdateSet: update})
	}))
	return u
}

// SetDeletedAt sets the "deleted_at" field.
func (u *UserInstalledThemeUpsertOne) SetDeletedAt(v time.Time) *UserInstalledThemeUpsertOne {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.SetDeletedAt(v)
	})
}

// UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
func (u *UserInstalledThemeUpsertOne) UpdateDeletedAt() *UserInstalledThemeUpsertOne {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.UpdateDeletedAt()
	})
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (u *UserInstalledThemeUpsertOne) ClearDeletedAt() *UserInstalledThemeUpsertOne {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.ClearDeletedAt()
	})
}

// SetUpdatedAt sets the "updated_at" field.
func (u *UserInstalledThemeUpsertOne) SetUpdatedAt(v time.Time) *UserInstalledThemeUpsertOne {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.SetUpdatedAt(v)
	})
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *UserInstalledThemeUpsertOne) UpdateUpdatedAt() *UserInstalledThemeUpsertOne {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.UpdateUpdatedAt()
	})
}

// SetUserID sets the "user_id" field.
func (u *UserInstalledThemeUpsertOne) SetUserID(v uint) *UserInstalledThemeUpsertOne {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.SetUserID(v)
	})
}

// UpdateUserID sets the "user_id" field to the value that was provided on create.
func (u *UserInstalledThemeUpsertOne) UpdateUserID() *UserInstalledThemeUpsertOne {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.UpdateUserID()
	})
}

// SetThemeName sets the "theme_name" field.
func (u *UserInstalledThemeUpsertOne) SetThemeName(v string) *UserInstalledThemeUpsertOne {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.SetThemeName(v)
	})
}

// UpdateThemeName sets the "theme_name" field to the value that was provided on create.
func (u *UserInstalledThemeUpsertOne) UpdateThemeName() *UserInstalledThemeUpsertOne {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.UpdateThemeName()
	})
}

// SetThemeMarketID sets the "theme_market_id" field.
func (u *UserInstalledThemeUpsertOne) SetThemeMarketID(v int) *UserInstalledThemeUpsertOne {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.SetThemeMarketID(v)
	})
}

// AddThemeMarketID adds v to the "theme_market_id" field.
func (u *UserInstalledThemeUpsertOne) AddThemeMarketID(v int) *UserInstalledThemeUpsertOne {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.AddThemeMarketID(v)
	})
}

// UpdateThemeMarketID sets the "theme_market_id" field to the value that was provided on create.
func (u *UserInstalledThemeUpsertOne) UpdateThemeMarketID() *UserInstalledThemeUpsertOne {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.UpdateThemeMarketID()
	})
}

// ClearThemeMarketID clears the value of the "theme_market_id" field.
func (u *UserInstalledThemeUpsertOne) ClearThemeMarketID() *UserInstalledThemeUpsertOne {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.ClearThemeMarketID()
	})
}

// SetIsCurrent sets the "is_current" field.
func (u *UserInstalledThemeUpsertOne) SetIsCurrent(v bool) *UserInstalledThemeUpsertOne {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.SetIsCurrent(v)
	})
}

// UpdateIsCurrent sets the "is_current" field to the value that was provided on create.
func (u *UserInstalledThemeUpsertOne) UpdateIsCurrent() *UserInstalledThemeUpsertOne {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.UpdateIsCurrent()
	})
}

// SetInstallTime sets the "install_time" field.
func (u *UserInstalledThemeUpsertOne) SetInstallTime(v time.Time) *UserInstalledThemeUpsertOne {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.SetInstallTime(v)
	})
}

// UpdateInstallTime sets the "install_time" field to the value that was provided on create.
func (u *UserInstalledThemeUpsertOne) UpdateInstallTime() *UserInstalledThemeUpsertOne {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.UpdateInstallTime()
	})
}

// SetUserThemeConfig sets the "user_theme_config" field.
func (u *UserInstalledThemeUpsertOne) SetUserThemeConfig(v map[string]interface{}) *UserInstalledThemeUpsertOne {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.SetUserThemeConfig(v)
	})
}

// UpdateUserThemeConfig sets the "user_theme_config" field to the value that was provided on create.
func (u *UserInstalledThemeUpsertOne) UpdateUserThemeConfig() *UserInstalledThemeUpsertOne {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.UpdateUserThemeConfig()
	})
}

// ClearUserThemeConfig clears the value of the "user_theme_config" field.
func (u *UserInstalledThemeUpsertOne) ClearUserThemeConfig() *UserInstalledThemeUpsertOne {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.ClearUserThemeConfig()
	})
}

// SetInstalledVersion sets the "installed_version" field.
func (u *UserInstalledThemeUpsertOne) SetInstalledVersion(v string) *UserInstalledThemeUpsertOne {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.SetInstalledVersion(v)
	})
}

// UpdateInstalledVersion sets the "installed_version" field to the value that was provided on create.
func (u *UserInstalledThemeUpsertOne) UpdateInstalledVersion() *UserInstalledThemeUpsertOne {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.UpdateInstalledVersion()
	})
}

// ClearInstalledVersion clears the value of the "installed_version" field.
func (u *UserInstalledThemeUpsertOne) ClearInstalledVersion() *UserInstalledThemeUpsertOne {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.ClearInstalledVersion()
	})
}

// Exec executes the query.
func (u *UserInstalledThemeUpsertOne) Exec(ctx context.Context) error {
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for UserInstalledThemeCreate.OnConflict")
	}
	return u.create.Exec(ctx)
}

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

// Exec executes the UPSERT query and returns the inserted/updated ID.
func (u *UserInstalledThemeUpsertOne) ID(ctx context.Context) (id uint, err error) {
	node, err := u.create.Save(ctx)
	if err != nil {
		return id, err
	}
	return node.ID, nil
}

// IDX is like ID, but panics if an error occurs.
func (u *UserInstalledThemeUpsertOne) IDX(ctx context.Context) uint {
	id, err := u.ID(ctx)
	if err != nil {
		panic(err)
	}
	return id
}

// UserInstalledThemeCreateBulk is the builder for creating many UserInstalledTheme entities in bulk.
type UserInstalledThemeCreateBulk struct {
	config
	err      error
	builders []*UserInstalledThemeCreate
	conflict []sql.ConflictOption
}

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

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

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

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.UserInstalledTheme.CreateBulk(builders...).
//		OnConflict(
//			// Update the row with the new values
//			// the was proposed for insertion.
//			sql.ResolveWithNewValues(),
//		).
//		// Override some of the fields with custom
//		// update values.
//		Update(func(u *ent.UserInstalledThemeUpsert) {
//			SetDeletedAt(v+v).
//		}).
//		Exec(ctx)
func (_c *UserInstalledThemeCreateBulk) OnConflict(opts ...sql.ConflictOption) *UserInstalledThemeUpsertBulk {
	_c.conflict = opts
	return &UserInstalledThemeUpsertBulk{
		create: _c,
	}
}

// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
//	client.UserInstalledTheme.Create().
//		OnConflict(sql.ConflictColumns(columns...)).
//		Exec(ctx)
func (_c *UserInstalledThemeCreateBulk) OnConflictColumns(columns ...string) *UserInstalledThemeUpsertBulk {
	_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
	return &UserInstalledThemeUpsertBulk{
		create: _c,
	}
}

// UserInstalledThemeUpsertBulk is the builder for "upsert"-ing
// a bulk of UserInstalledTheme nodes.
type UserInstalledThemeUpsertBulk struct {
	create *UserInstalledThemeCreateBulk
}

// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
//	client.UserInstalledTheme.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(userinstalledtheme.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *UserInstalledThemeUpsertBulk) UpdateNewValues() *UserInstalledThemeUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
		for _, b := range u.create.builders {
			if _, exists := b.mutation.ID(); exists {
				s.SetIgnore(userinstalledtheme.FieldID)
			}
			if _, exists := b.mutation.CreatedAt(); exists {
				s.SetIgnore(userinstalledtheme.FieldCreatedAt)
			}
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.UserInstalledTheme.Create().
//		OnConflict(sql.ResolveWithIgnore()).
//		Exec(ctx)
func (u *UserInstalledThemeUpsertBulk) Ignore() *UserInstalledThemeUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
	return u
}

// DoNothing configures the conflict_action to `DO NOTHING`.
// Supported only by SQLite and PostgreSQL.
func (u *UserInstalledThemeUpsertBulk) DoNothing() *UserInstalledThemeUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

// Update allows overriding fields `UPDATE` values. See the UserInstalledThemeCreateBulk.OnConflict
// documentation for more info.
func (u *UserInstalledThemeUpsertBulk) Update(set func(*UserInstalledThemeUpsert)) *UserInstalledThemeUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
		set(&UserInstalledThemeUpsert{UpdateSet: update})
	}))
	return u
}

// SetDeletedAt sets the "deleted_at" field.
func (u *UserInstalledThemeUpsertBulk) SetDeletedAt(v time.Time) *UserInstalledThemeUpsertBulk {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.SetDeletedAt(v)
	})
}

// UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
func (u *UserInstalledThemeUpsertBulk) UpdateDeletedAt() *UserInstalledThemeUpsertBulk {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.UpdateDeletedAt()
	})
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (u *UserInstalledThemeUpsertBulk) ClearDeletedAt() *UserInstalledThemeUpsertBulk {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.ClearDeletedAt()
	})
}

// SetUpdatedAt sets the "updated_at" field.
func (u *UserInstalledThemeUpsertBulk) SetUpdatedAt(v time.Time) *UserInstalledThemeUpsertBulk {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.SetUpdatedAt(v)
	})
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *UserInstalledThemeUpsertBulk) UpdateUpdatedAt() *UserInstalledThemeUpsertBulk {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.UpdateUpdatedAt()
	})
}

// SetUserID sets the "user_id" field.
func (u *UserInstalledThemeUpsertBulk) SetUserID(v uint) *UserInstalledThemeUpsertBulk {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.SetUserID(v)
	})
}

// UpdateUserID sets the "user_id" field to the value that was provided on create.
func (u *UserInstalledThemeUpsertBulk) UpdateUserID() *UserInstalledThemeUpsertBulk {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.UpdateUserID()
	})
}

// SetThemeName sets the "theme_name" field.
func (u *UserInstalledThemeUpsertBulk) SetThemeName(v string) *UserInstalledThemeUpsertBulk {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.SetThemeName(v)
	})
}

// UpdateThemeName sets the "theme_name" field to the value that was provided on create.
func (u *UserInstalledThemeUpsertBulk) UpdateThemeName() *UserInstalledThemeUpsertBulk {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.UpdateThemeName()
	})
}

// SetThemeMarketID sets the "theme_market_id" field.
func (u *UserInstalledThemeUpsertBulk) SetThemeMarketID(v int) *UserInstalledThemeUpsertBulk {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.SetThemeMarketID(v)
	})
}

// AddThemeMarketID adds v to the "theme_market_id" field.
func (u *UserInstalledThemeUpsertBulk) AddThemeMarketID(v int) *UserInstalledThemeUpsertBulk {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.AddThemeMarketID(v)
	})
}

// UpdateThemeMarketID sets the "theme_market_id" field to the value that was provided on create.
func (u *UserInstalledThemeUpsertBulk) UpdateThemeMarketID() *UserInstalledThemeUpsertBulk {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.UpdateThemeMarketID()
	})
}

// ClearThemeMarketID clears the value of the "theme_market_id" field.
func (u *UserInstalledThemeUpsertBulk) ClearThemeMarketID() *UserInstalledThemeUpsertBulk {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.ClearThemeMarketID()
	})
}

// SetIsCurrent sets the "is_current" field.
func (u *UserInstalledThemeUpsertBulk) SetIsCurrent(v bool) *UserInstalledThemeUpsertBulk {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.SetIsCurrent(v)
	})
}

// UpdateIsCurrent sets the "is_current" field to the value that was provided on create.
func (u *UserInstalledThemeUpsertBulk) UpdateIsCurrent() *UserInstalledThemeUpsertBulk {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.UpdateIsCurrent()
	})
}

// SetInstallTime sets the "install_time" field.
func (u *UserInstalledThemeUpsertBulk) SetInstallTime(v time.Time) *UserInstalledThemeUpsertBulk {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.SetInstallTime(v)
	})
}

// UpdateInstallTime sets the "install_time" field to the value that was provided on create.
func (u *UserInstalledThemeUpsertBulk) UpdateInstallTime() *UserInstalledThemeUpsertBulk {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.UpdateInstallTime()
	})
}

// SetUserThemeConfig sets the "user_theme_config" field.
func (u *UserInstalledThemeUpsertBulk) SetUserThemeConfig(v map[string]interface{}) *UserInstalledThemeUpsertBulk {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.SetUserThemeConfig(v)
	})
}

// UpdateUserThemeConfig sets the "user_theme_config" field to the value that was provided on create.
func (u *UserInstalledThemeUpsertBulk) UpdateUserThemeConfig() *UserInstalledThemeUpsertBulk {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.UpdateUserThemeConfig()
	})
}

// ClearUserThemeConfig clears the value of the "user_theme_config" field.
func (u *UserInstalledThemeUpsertBulk) ClearUserThemeConfig() *UserInstalledThemeUpsertBulk {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.ClearUserThemeConfig()
	})
}

// SetInstalledVersion sets the "installed_version" field.
func (u *UserInstalledThemeUpsertBulk) SetInstalledVersion(v string) *UserInstalledThemeUpsertBulk {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.SetInstalledVersion(v)
	})
}

// UpdateInstalledVersion sets the "installed_version" field to the value that was provided on create.
func (u *UserInstalledThemeUpsertBulk) UpdateInstalledVersion() *UserInstalledThemeUpsertBulk {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.UpdateInstalledVersion()
	})
}

// ClearInstalledVersion clears the value of the "installed_version" field.
func (u *UserInstalledThemeUpsertBulk) ClearInstalledVersion() *UserInstalledThemeUpsertBulk {
	return u.Update(func(s *UserInstalledThemeUpsert) {
		s.ClearInstalledVersion()
	})
}

// Exec executes the query.
func (u *UserInstalledThemeUpsertBulk) Exec(ctx context.Context) error {
	if u.create.err != nil {
		return u.create.err
	}
	for i, b := range u.create.builders {
		if len(b.conflict) != 0 {
			return fmt.Errorf("ent: OnConflict was set for builder %d. Set it on the UserInstalledThemeCreateBulk instead", i)
		}
	}
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for UserInstalledThemeCreateBulk.OnConflict")
	}
	return u.create.Exec(ctx)
}

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