// 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/Wei-Shaw/sub2api/ent/group"
	"github.com/Wei-Shaw/sub2api/ent/redeemcode"
	"github.com/Wei-Shaw/sub2api/ent/user"
)

// RedeemCodeCreate is the builder for creating a RedeemCode entity.
type RedeemCodeCreate struct {
	config
	mutation *RedeemCodeMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

// SetCode sets the "code" field.
func (_c *RedeemCodeCreate) SetCode(v string) *RedeemCodeCreate {
	_c.mutation.SetCode(v)
	return _c
}

// SetType sets the "type" field.
func (_c *RedeemCodeCreate) SetType(v string) *RedeemCodeCreate {
	_c.mutation.SetType(v)
	return _c
}

// SetNillableType sets the "type" field if the given value is not nil.
func (_c *RedeemCodeCreate) SetNillableType(v *string) *RedeemCodeCreate {
	if v != nil {
		_c.SetType(*v)
	}
	return _c
}

// SetValue sets the "value" field.
func (_c *RedeemCodeCreate) SetValue(v float64) *RedeemCodeCreate {
	_c.mutation.SetValue(v)
	return _c
}

// SetNillableValue sets the "value" field if the given value is not nil.
func (_c *RedeemCodeCreate) SetNillableValue(v *float64) *RedeemCodeCreate {
	if v != nil {
		_c.SetValue(*v)
	}
	return _c
}

// SetStatus sets the "status" field.
func (_c *RedeemCodeCreate) SetStatus(v string) *RedeemCodeCreate {
	_c.mutation.SetStatus(v)
	return _c
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (_c *RedeemCodeCreate) SetNillableStatus(v *string) *RedeemCodeCreate {
	if v != nil {
		_c.SetStatus(*v)
	}
	return _c
}

// SetUsedBy sets the "used_by" field.
func (_c *RedeemCodeCreate) SetUsedBy(v int64) *RedeemCodeCreate {
	_c.mutation.SetUsedBy(v)
	return _c
}

// SetNillableUsedBy sets the "used_by" field if the given value is not nil.
func (_c *RedeemCodeCreate) SetNillableUsedBy(v *int64) *RedeemCodeCreate {
	if v != nil {
		_c.SetUsedBy(*v)
	}
	return _c
}

// SetUsedAt sets the "used_at" field.
func (_c *RedeemCodeCreate) SetUsedAt(v time.Time) *RedeemCodeCreate {
	_c.mutation.SetUsedAt(v)
	return _c
}

// SetNillableUsedAt sets the "used_at" field if the given value is not nil.
func (_c *RedeemCodeCreate) SetNillableUsedAt(v *time.Time) *RedeemCodeCreate {
	if v != nil {
		_c.SetUsedAt(*v)
	}
	return _c
}

// SetNotes sets the "notes" field.
func (_c *RedeemCodeCreate) SetNotes(v string) *RedeemCodeCreate {
	_c.mutation.SetNotes(v)
	return _c
}

// SetNillableNotes sets the "notes" field if the given value is not nil.
func (_c *RedeemCodeCreate) SetNillableNotes(v *string) *RedeemCodeCreate {
	if v != nil {
		_c.SetNotes(*v)
	}
	return _c
}

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

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

// SetGroupID sets the "group_id" field.
func (_c *RedeemCodeCreate) SetGroupID(v int64) *RedeemCodeCreate {
	_c.mutation.SetGroupID(v)
	return _c
}

// SetNillableGroupID sets the "group_id" field if the given value is not nil.
func (_c *RedeemCodeCreate) SetNillableGroupID(v *int64) *RedeemCodeCreate {
	if v != nil {
		_c.SetGroupID(*v)
	}
	return _c
}

// SetValidityDays sets the "validity_days" field.
func (_c *RedeemCodeCreate) SetValidityDays(v int) *RedeemCodeCreate {
	_c.mutation.SetValidityDays(v)
	return _c
}

// SetNillableValidityDays sets the "validity_days" field if the given value is not nil.
func (_c *RedeemCodeCreate) SetNillableValidityDays(v *int) *RedeemCodeCreate {
	if v != nil {
		_c.SetValidityDays(*v)
	}
	return _c
}

// SetUserID sets the "user" edge to the User entity by ID.
func (_c *RedeemCodeCreate) SetUserID(id int64) *RedeemCodeCreate {
	_c.mutation.SetUserID(id)
	return _c
}

// SetNillableUserID sets the "user" edge to the User entity by ID if the given value is not nil.
func (_c *RedeemCodeCreate) SetNillableUserID(id *int64) *RedeemCodeCreate {
	if id != nil {
		_c = _c.SetUserID(*id)
	}
	return _c
}

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

// SetGroup sets the "group" edge to the Group entity.
func (_c *RedeemCodeCreate) SetGroup(v *Group) *RedeemCodeCreate {
	return _c.SetGroupID(v.ID)
}

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

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_c *RedeemCodeCreate) 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 *RedeemCodeCreate) defaults() {
	if _, ok := _c.mutation.GetType(); !ok {
		v := redeemcode.DefaultType
		_c.mutation.SetType(v)
	}
	if _, ok := _c.mutation.Value(); !ok {
		v := redeemcode.DefaultValue
		_c.mutation.SetValue(v)
	}
	if _, ok := _c.mutation.Status(); !ok {
		v := redeemcode.DefaultStatus
		_c.mutation.SetStatus(v)
	}
	if _, ok := _c.mutation.CreatedAt(); !ok {
		v := redeemcode.DefaultCreatedAt()
		_c.mutation.SetCreatedAt(v)
	}
	if _, ok := _c.mutation.ValidityDays(); !ok {
		v := redeemcode.DefaultValidityDays
		_c.mutation.SetValidityDays(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_c *RedeemCodeCreate) check() error {
	if _, ok := _c.mutation.Code(); !ok {
		return &ValidationError{Name: "code", err: errors.New(`ent: missing required field "RedeemCode.code"`)}
	}
	if v, ok := _c.mutation.Code(); ok {
		if err := redeemcode.CodeValidator(v); err != nil {
			return &ValidationError{Name: "code", err: fmt.Errorf(`ent: validator failed for field "RedeemCode.code": %w`, err)}
		}
	}
	if _, ok := _c.mutation.GetType(); !ok {
		return &ValidationError{Name: "type", err: errors.New(`ent: missing required field "RedeemCode.type"`)}
	}
	if v, ok := _c.mutation.GetType(); ok {
		if err := redeemcode.TypeValidator(v); err != nil {
			return &ValidationError{Name: "type", err: fmt.Errorf(`ent: validator failed for field "RedeemCode.type": %w`, err)}
		}
	}
	if _, ok := _c.mutation.Value(); !ok {
		return &ValidationError{Name: "value", err: errors.New(`ent: missing required field "RedeemCode.value"`)}
	}
	if _, ok := _c.mutation.Status(); !ok {
		return &ValidationError{Name: "status", err: errors.New(`ent: missing required field "RedeemCode.status"`)}
	}
	if v, ok := _c.mutation.Status(); ok {
		if err := redeemcode.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "RedeemCode.status": %w`, err)}
		}
	}
	if _, ok := _c.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "RedeemCode.created_at"`)}
	}
	if _, ok := _c.mutation.ValidityDays(); !ok {
		return &ValidationError{Name: "validity_days", err: errors.New(`ent: missing required field "RedeemCode.validity_days"`)}
	}
	return nil
}

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

func (_c *RedeemCodeCreate) createSpec() (*RedeemCode, *sqlgraph.CreateSpec) {
	var (
		_node = &RedeemCode{config: _c.config}
		_spec = sqlgraph.NewCreateSpec(redeemcode.Table, sqlgraph.NewFieldSpec(redeemcode.FieldID, field.TypeInt64))
	)
	_spec.OnConflict = _c.conflict
	if value, ok := _c.mutation.Code(); ok {
		_spec.SetField(redeemcode.FieldCode, field.TypeString, value)
		_node.Code = value
	}
	if value, ok := _c.mutation.GetType(); ok {
		_spec.SetField(redeemcode.FieldType, field.TypeString, value)
		_node.Type = value
	}
	if value, ok := _c.mutation.Value(); ok {
		_spec.SetField(redeemcode.FieldValue, field.TypeFloat64, value)
		_node.Value = value
	}
	if value, ok := _c.mutation.Status(); ok {
		_spec.SetField(redeemcode.FieldStatus, field.TypeString, value)
		_node.Status = value
	}
	if value, ok := _c.mutation.UsedAt(); ok {
		_spec.SetField(redeemcode.FieldUsedAt, field.TypeTime, value)
		_node.UsedAt = &value
	}
	if value, ok := _c.mutation.Notes(); ok {
		_spec.SetField(redeemcode.FieldNotes, field.TypeString, value)
		_node.Notes = &value
	}
	if value, ok := _c.mutation.CreatedAt(); ok {
		_spec.SetField(redeemcode.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := _c.mutation.ValidityDays(); ok {
		_spec.SetField(redeemcode.FieldValidityDays, field.TypeInt, value)
		_node.ValidityDays = value
	}
	if nodes := _c.mutation.UserIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   redeemcode.UserTable,
			Columns: []string{redeemcode.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.UsedBy = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := _c.mutation.GroupIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   redeemcode.GroupTable,
			Columns: []string{redeemcode.GroupColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.GroupID = &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.RedeemCode.Create().
//		SetCode(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.RedeemCodeUpsert) {
//			SetCode(v+v).
//		}).
//		Exec(ctx)
func (_c *RedeemCodeCreate) OnConflict(opts ...sql.ConflictOption) *RedeemCodeUpsertOne {
	_c.conflict = opts
	return &RedeemCodeUpsertOne{
		create: _c,
	}
}

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

type (
	// RedeemCodeUpsertOne is the builder for "upsert"-ing
	//  one RedeemCode node.
	RedeemCodeUpsertOne struct {
		create *RedeemCodeCreate
	}

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

// SetCode sets the "code" field.
func (u *RedeemCodeUpsert) SetCode(v string) *RedeemCodeUpsert {
	u.Set(redeemcode.FieldCode, v)
	return u
}

// UpdateCode sets the "code" field to the value that was provided on create.
func (u *RedeemCodeUpsert) UpdateCode() *RedeemCodeUpsert {
	u.SetExcluded(redeemcode.FieldCode)
	return u
}

// SetType sets the "type" field.
func (u *RedeemCodeUpsert) SetType(v string) *RedeemCodeUpsert {
	u.Set(redeemcode.FieldType, v)
	return u
}

// UpdateType sets the "type" field to the value that was provided on create.
func (u *RedeemCodeUpsert) UpdateType() *RedeemCodeUpsert {
	u.SetExcluded(redeemcode.FieldType)
	return u
}

// SetValue sets the "value" field.
func (u *RedeemCodeUpsert) SetValue(v float64) *RedeemCodeUpsert {
	u.Set(redeemcode.FieldValue, v)
	return u
}

// UpdateValue sets the "value" field to the value that was provided on create.
func (u *RedeemCodeUpsert) UpdateValue() *RedeemCodeUpsert {
	u.SetExcluded(redeemcode.FieldValue)
	return u
}

// AddValue adds v to the "value" field.
func (u *RedeemCodeUpsert) AddValue(v float64) *RedeemCodeUpsert {
	u.Add(redeemcode.FieldValue, v)
	return u
}

// SetStatus sets the "status" field.
func (u *RedeemCodeUpsert) SetStatus(v string) *RedeemCodeUpsert {
	u.Set(redeemcode.FieldStatus, v)
	return u
}

// UpdateStatus sets the "status" field to the value that was provided on create.
func (u *RedeemCodeUpsert) UpdateStatus() *RedeemCodeUpsert {
	u.SetExcluded(redeemcode.FieldStatus)
	return u
}

// SetUsedBy sets the "used_by" field.
func (u *RedeemCodeUpsert) SetUsedBy(v int64) *RedeemCodeUpsert {
	u.Set(redeemcode.FieldUsedBy, v)
	return u
}

// UpdateUsedBy sets the "used_by" field to the value that was provided on create.
func (u *RedeemCodeUpsert) UpdateUsedBy() *RedeemCodeUpsert {
	u.SetExcluded(redeemcode.FieldUsedBy)
	return u
}

// ClearUsedBy clears the value of the "used_by" field.
func (u *RedeemCodeUpsert) ClearUsedBy() *RedeemCodeUpsert {
	u.SetNull(redeemcode.FieldUsedBy)
	return u
}

// SetUsedAt sets the "used_at" field.
func (u *RedeemCodeUpsert) SetUsedAt(v time.Time) *RedeemCodeUpsert {
	u.Set(redeemcode.FieldUsedAt, v)
	return u
}

// UpdateUsedAt sets the "used_at" field to the value that was provided on create.
func (u *RedeemCodeUpsert) UpdateUsedAt() *RedeemCodeUpsert {
	u.SetExcluded(redeemcode.FieldUsedAt)
	return u
}

// ClearUsedAt clears the value of the "used_at" field.
func (u *RedeemCodeUpsert) ClearUsedAt() *RedeemCodeUpsert {
	u.SetNull(redeemcode.FieldUsedAt)
	return u
}

// SetNotes sets the "notes" field.
func (u *RedeemCodeUpsert) SetNotes(v string) *RedeemCodeUpsert {
	u.Set(redeemcode.FieldNotes, v)
	return u
}

// UpdateNotes sets the "notes" field to the value that was provided on create.
func (u *RedeemCodeUpsert) UpdateNotes() *RedeemCodeUpsert {
	u.SetExcluded(redeemcode.FieldNotes)
	return u
}

// ClearNotes clears the value of the "notes" field.
func (u *RedeemCodeUpsert) ClearNotes() *RedeemCodeUpsert {
	u.SetNull(redeemcode.FieldNotes)
	return u
}

// SetGroupID sets the "group_id" field.
func (u *RedeemCodeUpsert) SetGroupID(v int64) *RedeemCodeUpsert {
	u.Set(redeemcode.FieldGroupID, v)
	return u
}

// UpdateGroupID sets the "group_id" field to the value that was provided on create.
func (u *RedeemCodeUpsert) UpdateGroupID() *RedeemCodeUpsert {
	u.SetExcluded(redeemcode.FieldGroupID)
	return u
}

// ClearGroupID clears the value of the "group_id" field.
func (u *RedeemCodeUpsert) ClearGroupID() *RedeemCodeUpsert {
	u.SetNull(redeemcode.FieldGroupID)
	return u
}

// SetValidityDays sets the "validity_days" field.
func (u *RedeemCodeUpsert) SetValidityDays(v int) *RedeemCodeUpsert {
	u.Set(redeemcode.FieldValidityDays, v)
	return u
}

// UpdateValidityDays sets the "validity_days" field to the value that was provided on create.
func (u *RedeemCodeUpsert) UpdateValidityDays() *RedeemCodeUpsert {
	u.SetExcluded(redeemcode.FieldValidityDays)
	return u
}

// AddValidityDays adds v to the "validity_days" field.
func (u *RedeemCodeUpsert) AddValidityDays(v int) *RedeemCodeUpsert {
	u.Add(redeemcode.FieldValidityDays, v)
	return u
}

// UpdateNewValues updates the mutable fields using the new values that were set on create.
// Using this option is equivalent to using:
//
//	client.RedeemCode.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//		).
//		Exec(ctx)
func (u *RedeemCodeUpsertOne) UpdateNewValues() *RedeemCodeUpsertOne {
	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.CreatedAt(); exists {
			s.SetIgnore(redeemcode.FieldCreatedAt)
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.RedeemCode.Create().
//	    OnConflict(sql.ResolveWithIgnore()).
//	    Exec(ctx)
func (u *RedeemCodeUpsertOne) Ignore() *RedeemCodeUpsertOne {
	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 *RedeemCodeUpsertOne) DoNothing() *RedeemCodeUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

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

// SetCode sets the "code" field.
func (u *RedeemCodeUpsertOne) SetCode(v string) *RedeemCodeUpsertOne {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.SetCode(v)
	})
}

// UpdateCode sets the "code" field to the value that was provided on create.
func (u *RedeemCodeUpsertOne) UpdateCode() *RedeemCodeUpsertOne {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.UpdateCode()
	})
}

// SetType sets the "type" field.
func (u *RedeemCodeUpsertOne) SetType(v string) *RedeemCodeUpsertOne {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.SetType(v)
	})
}

// UpdateType sets the "type" field to the value that was provided on create.
func (u *RedeemCodeUpsertOne) UpdateType() *RedeemCodeUpsertOne {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.UpdateType()
	})
}

// SetValue sets the "value" field.
func (u *RedeemCodeUpsertOne) SetValue(v float64) *RedeemCodeUpsertOne {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.SetValue(v)
	})
}

// AddValue adds v to the "value" field.
func (u *RedeemCodeUpsertOne) AddValue(v float64) *RedeemCodeUpsertOne {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.AddValue(v)
	})
}

// UpdateValue sets the "value" field to the value that was provided on create.
func (u *RedeemCodeUpsertOne) UpdateValue() *RedeemCodeUpsertOne {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.UpdateValue()
	})
}

// SetStatus sets the "status" field.
func (u *RedeemCodeUpsertOne) SetStatus(v string) *RedeemCodeUpsertOne {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.SetStatus(v)
	})
}

// UpdateStatus sets the "status" field to the value that was provided on create.
func (u *RedeemCodeUpsertOne) UpdateStatus() *RedeemCodeUpsertOne {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.UpdateStatus()
	})
}

// SetUsedBy sets the "used_by" field.
func (u *RedeemCodeUpsertOne) SetUsedBy(v int64) *RedeemCodeUpsertOne {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.SetUsedBy(v)
	})
}

// UpdateUsedBy sets the "used_by" field to the value that was provided on create.
func (u *RedeemCodeUpsertOne) UpdateUsedBy() *RedeemCodeUpsertOne {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.UpdateUsedBy()
	})
}

// ClearUsedBy clears the value of the "used_by" field.
func (u *RedeemCodeUpsertOne) ClearUsedBy() *RedeemCodeUpsertOne {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.ClearUsedBy()
	})
}

// SetUsedAt sets the "used_at" field.
func (u *RedeemCodeUpsertOne) SetUsedAt(v time.Time) *RedeemCodeUpsertOne {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.SetUsedAt(v)
	})
}

// UpdateUsedAt sets the "used_at" field to the value that was provided on create.
func (u *RedeemCodeUpsertOne) UpdateUsedAt() *RedeemCodeUpsertOne {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.UpdateUsedAt()
	})
}

// ClearUsedAt clears the value of the "used_at" field.
func (u *RedeemCodeUpsertOne) ClearUsedAt() *RedeemCodeUpsertOne {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.ClearUsedAt()
	})
}

// SetNotes sets the "notes" field.
func (u *RedeemCodeUpsertOne) SetNotes(v string) *RedeemCodeUpsertOne {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.SetNotes(v)
	})
}

// UpdateNotes sets the "notes" field to the value that was provided on create.
func (u *RedeemCodeUpsertOne) UpdateNotes() *RedeemCodeUpsertOne {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.UpdateNotes()
	})
}

// ClearNotes clears the value of the "notes" field.
func (u *RedeemCodeUpsertOne) ClearNotes() *RedeemCodeUpsertOne {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.ClearNotes()
	})
}

// SetGroupID sets the "group_id" field.
func (u *RedeemCodeUpsertOne) SetGroupID(v int64) *RedeemCodeUpsertOne {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.SetGroupID(v)
	})
}

// UpdateGroupID sets the "group_id" field to the value that was provided on create.
func (u *RedeemCodeUpsertOne) UpdateGroupID() *RedeemCodeUpsertOne {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.UpdateGroupID()
	})
}

// ClearGroupID clears the value of the "group_id" field.
func (u *RedeemCodeUpsertOne) ClearGroupID() *RedeemCodeUpsertOne {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.ClearGroupID()
	})
}

// SetValidityDays sets the "validity_days" field.
func (u *RedeemCodeUpsertOne) SetValidityDays(v int) *RedeemCodeUpsertOne {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.SetValidityDays(v)
	})
}

// AddValidityDays adds v to the "validity_days" field.
func (u *RedeemCodeUpsertOne) AddValidityDays(v int) *RedeemCodeUpsertOne {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.AddValidityDays(v)
	})
}

// UpdateValidityDays sets the "validity_days" field to the value that was provided on create.
func (u *RedeemCodeUpsertOne) UpdateValidityDays() *RedeemCodeUpsertOne {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.UpdateValidityDays()
	})
}

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

// ExecX is like Exec, but panics if an error occurs.
func (u *RedeemCodeUpsertOne) 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 *RedeemCodeUpsertOne) ID(ctx context.Context) (id int64, 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 *RedeemCodeUpsertOne) IDX(ctx context.Context) int64 {
	id, err := u.ID(ctx)
	if err != nil {
		panic(err)
	}
	return id
}

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_c *RedeemCodeCreateBulk) 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.RedeemCode.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.RedeemCodeUpsert) {
//			SetCode(v+v).
//		}).
//		Exec(ctx)
func (_c *RedeemCodeCreateBulk) OnConflict(opts ...sql.ConflictOption) *RedeemCodeUpsertBulk {
	_c.conflict = opts
	return &RedeemCodeUpsertBulk{
		create: _c,
	}
}

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

// RedeemCodeUpsertBulk is the builder for "upsert"-ing
// a bulk of RedeemCode nodes.
type RedeemCodeUpsertBulk struct {
	create *RedeemCodeCreateBulk
}

// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
//	client.RedeemCode.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//		).
//		Exec(ctx)
func (u *RedeemCodeUpsertBulk) UpdateNewValues() *RedeemCodeUpsertBulk {
	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.CreatedAt(); exists {
				s.SetIgnore(redeemcode.FieldCreatedAt)
			}
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.RedeemCode.Create().
//		OnConflict(sql.ResolveWithIgnore()).
//		Exec(ctx)
func (u *RedeemCodeUpsertBulk) Ignore() *RedeemCodeUpsertBulk {
	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 *RedeemCodeUpsertBulk) DoNothing() *RedeemCodeUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

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

// SetCode sets the "code" field.
func (u *RedeemCodeUpsertBulk) SetCode(v string) *RedeemCodeUpsertBulk {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.SetCode(v)
	})
}

// UpdateCode sets the "code" field to the value that was provided on create.
func (u *RedeemCodeUpsertBulk) UpdateCode() *RedeemCodeUpsertBulk {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.UpdateCode()
	})
}

// SetType sets the "type" field.
func (u *RedeemCodeUpsertBulk) SetType(v string) *RedeemCodeUpsertBulk {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.SetType(v)
	})
}

// UpdateType sets the "type" field to the value that was provided on create.
func (u *RedeemCodeUpsertBulk) UpdateType() *RedeemCodeUpsertBulk {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.UpdateType()
	})
}

// SetValue sets the "value" field.
func (u *RedeemCodeUpsertBulk) SetValue(v float64) *RedeemCodeUpsertBulk {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.SetValue(v)
	})
}

// AddValue adds v to the "value" field.
func (u *RedeemCodeUpsertBulk) AddValue(v float64) *RedeemCodeUpsertBulk {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.AddValue(v)
	})
}

// UpdateValue sets the "value" field to the value that was provided on create.
func (u *RedeemCodeUpsertBulk) UpdateValue() *RedeemCodeUpsertBulk {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.UpdateValue()
	})
}

// SetStatus sets the "status" field.
func (u *RedeemCodeUpsertBulk) SetStatus(v string) *RedeemCodeUpsertBulk {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.SetStatus(v)
	})
}

// UpdateStatus sets the "status" field to the value that was provided on create.
func (u *RedeemCodeUpsertBulk) UpdateStatus() *RedeemCodeUpsertBulk {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.UpdateStatus()
	})
}

// SetUsedBy sets the "used_by" field.
func (u *RedeemCodeUpsertBulk) SetUsedBy(v int64) *RedeemCodeUpsertBulk {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.SetUsedBy(v)
	})
}

// UpdateUsedBy sets the "used_by" field to the value that was provided on create.
func (u *RedeemCodeUpsertBulk) UpdateUsedBy() *RedeemCodeUpsertBulk {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.UpdateUsedBy()
	})
}

// ClearUsedBy clears the value of the "used_by" field.
func (u *RedeemCodeUpsertBulk) ClearUsedBy() *RedeemCodeUpsertBulk {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.ClearUsedBy()
	})
}

// SetUsedAt sets the "used_at" field.
func (u *RedeemCodeUpsertBulk) SetUsedAt(v time.Time) *RedeemCodeUpsertBulk {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.SetUsedAt(v)
	})
}

// UpdateUsedAt sets the "used_at" field to the value that was provided on create.
func (u *RedeemCodeUpsertBulk) UpdateUsedAt() *RedeemCodeUpsertBulk {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.UpdateUsedAt()
	})
}

// ClearUsedAt clears the value of the "used_at" field.
func (u *RedeemCodeUpsertBulk) ClearUsedAt() *RedeemCodeUpsertBulk {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.ClearUsedAt()
	})
}

// SetNotes sets the "notes" field.
func (u *RedeemCodeUpsertBulk) SetNotes(v string) *RedeemCodeUpsertBulk {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.SetNotes(v)
	})
}

// UpdateNotes sets the "notes" field to the value that was provided on create.
func (u *RedeemCodeUpsertBulk) UpdateNotes() *RedeemCodeUpsertBulk {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.UpdateNotes()
	})
}

// ClearNotes clears the value of the "notes" field.
func (u *RedeemCodeUpsertBulk) ClearNotes() *RedeemCodeUpsertBulk {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.ClearNotes()
	})
}

// SetGroupID sets the "group_id" field.
func (u *RedeemCodeUpsertBulk) SetGroupID(v int64) *RedeemCodeUpsertBulk {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.SetGroupID(v)
	})
}

// UpdateGroupID sets the "group_id" field to the value that was provided on create.
func (u *RedeemCodeUpsertBulk) UpdateGroupID() *RedeemCodeUpsertBulk {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.UpdateGroupID()
	})
}

// ClearGroupID clears the value of the "group_id" field.
func (u *RedeemCodeUpsertBulk) ClearGroupID() *RedeemCodeUpsertBulk {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.ClearGroupID()
	})
}

// SetValidityDays sets the "validity_days" field.
func (u *RedeemCodeUpsertBulk) SetValidityDays(v int) *RedeemCodeUpsertBulk {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.SetValidityDays(v)
	})
}

// AddValidityDays adds v to the "validity_days" field.
func (u *RedeemCodeUpsertBulk) AddValidityDays(v int) *RedeemCodeUpsertBulk {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.AddValidityDays(v)
	})
}

// UpdateValidityDays sets the "validity_days" field to the value that was provided on create.
func (u *RedeemCodeUpsertBulk) UpdateValidityDays() *RedeemCodeUpsertBulk {
	return u.Update(func(s *RedeemCodeUpsert) {
		s.UpdateValidityDays()
	})
}

// Exec executes the query.
func (u *RedeemCodeUpsertBulk) 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 RedeemCodeCreateBulk instead", i)
		}
	}
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for RedeemCodeCreateBulk.OnConflict")
	}
	return u.create.Exec(ctx)
}

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