// 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/promocode"
	"github.com/Wei-Shaw/sub2api/ent/promocodeusage"
)

// PromoCodeCreate is the builder for creating a PromoCode entity.
type PromoCodeCreate struct {
	config
	mutation *PromoCodeMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

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

// SetBonusAmount sets the "bonus_amount" field.
func (_c *PromoCodeCreate) SetBonusAmount(v float64) *PromoCodeCreate {
	_c.mutation.SetBonusAmount(v)
	return _c
}

// SetNillableBonusAmount sets the "bonus_amount" field if the given value is not nil.
func (_c *PromoCodeCreate) SetNillableBonusAmount(v *float64) *PromoCodeCreate {
	if v != nil {
		_c.SetBonusAmount(*v)
	}
	return _c
}

// SetMaxUses sets the "max_uses" field.
func (_c *PromoCodeCreate) SetMaxUses(v int) *PromoCodeCreate {
	_c.mutation.SetMaxUses(v)
	return _c
}

// SetNillableMaxUses sets the "max_uses" field if the given value is not nil.
func (_c *PromoCodeCreate) SetNillableMaxUses(v *int) *PromoCodeCreate {
	if v != nil {
		_c.SetMaxUses(*v)
	}
	return _c
}

// SetUsedCount sets the "used_count" field.
func (_c *PromoCodeCreate) SetUsedCount(v int) *PromoCodeCreate {
	_c.mutation.SetUsedCount(v)
	return _c
}

// SetNillableUsedCount sets the "used_count" field if the given value is not nil.
func (_c *PromoCodeCreate) SetNillableUsedCount(v *int) *PromoCodeCreate {
	if v != nil {
		_c.SetUsedCount(*v)
	}
	return _c
}

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

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

// SetExpiresAt sets the "expires_at" field.
func (_c *PromoCodeCreate) SetExpiresAt(v time.Time) *PromoCodeCreate {
	_c.mutation.SetExpiresAt(v)
	return _c
}

// SetNillableExpiresAt sets the "expires_at" field if the given value is not nil.
func (_c *PromoCodeCreate) SetNillableExpiresAt(v *time.Time) *PromoCodeCreate {
	if v != nil {
		_c.SetExpiresAt(*v)
	}
	return _c
}

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

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

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

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

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

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

// AddUsageRecordIDs adds the "usage_records" edge to the PromoCodeUsage entity by IDs.
func (_c *PromoCodeCreate) AddUsageRecordIDs(ids ...int64) *PromoCodeCreate {
	_c.mutation.AddUsageRecordIDs(ids...)
	return _c
}

// AddUsageRecords adds the "usage_records" edges to the PromoCodeUsage entity.
func (_c *PromoCodeCreate) AddUsageRecords(v ...*PromoCodeUsage) *PromoCodeCreate {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _c.AddUsageRecordIDs(ids...)
}

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

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_c *PromoCodeCreate) 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 *PromoCodeCreate) defaults() {
	if _, ok := _c.mutation.BonusAmount(); !ok {
		v := promocode.DefaultBonusAmount
		_c.mutation.SetBonusAmount(v)
	}
	if _, ok := _c.mutation.MaxUses(); !ok {
		v := promocode.DefaultMaxUses
		_c.mutation.SetMaxUses(v)
	}
	if _, ok := _c.mutation.UsedCount(); !ok {
		v := promocode.DefaultUsedCount
		_c.mutation.SetUsedCount(v)
	}
	if _, ok := _c.mutation.Status(); !ok {
		v := promocode.DefaultStatus
		_c.mutation.SetStatus(v)
	}
	if _, ok := _c.mutation.CreatedAt(); !ok {
		v := promocode.DefaultCreatedAt()
		_c.mutation.SetCreatedAt(v)
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		v := promocode.DefaultUpdatedAt()
		_c.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_c *PromoCodeCreate) check() error {
	if _, ok := _c.mutation.Code(); !ok {
		return &ValidationError{Name: "code", err: errors.New(`ent: missing required field "PromoCode.code"`)}
	}
	if v, ok := _c.mutation.Code(); ok {
		if err := promocode.CodeValidator(v); err != nil {
			return &ValidationError{Name: "code", err: fmt.Errorf(`ent: validator failed for field "PromoCode.code": %w`, err)}
		}
	}
	if _, ok := _c.mutation.BonusAmount(); !ok {
		return &ValidationError{Name: "bonus_amount", err: errors.New(`ent: missing required field "PromoCode.bonus_amount"`)}
	}
	if _, ok := _c.mutation.MaxUses(); !ok {
		return &ValidationError{Name: "max_uses", err: errors.New(`ent: missing required field "PromoCode.max_uses"`)}
	}
	if _, ok := _c.mutation.UsedCount(); !ok {
		return &ValidationError{Name: "used_count", err: errors.New(`ent: missing required field "PromoCode.used_count"`)}
	}
	if _, ok := _c.mutation.Status(); !ok {
		return &ValidationError{Name: "status", err: errors.New(`ent: missing required field "PromoCode.status"`)}
	}
	if v, ok := _c.mutation.Status(); ok {
		if err := promocode.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "PromoCode.status": %w`, err)}
		}
	}
	if _, ok := _c.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "PromoCode.created_at"`)}
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "PromoCode.updated_at"`)}
	}
	return nil
}

func (_c *PromoCodeCreate) sqlSave(ctx context.Context) (*PromoCode, 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 *PromoCodeCreate) createSpec() (*PromoCode, *sqlgraph.CreateSpec) {
	var (
		_node = &PromoCode{config: _c.config}
		_spec = sqlgraph.NewCreateSpec(promocode.Table, sqlgraph.NewFieldSpec(promocode.FieldID, field.TypeInt64))
	)
	_spec.OnConflict = _c.conflict
	if value, ok := _c.mutation.Code(); ok {
		_spec.SetField(promocode.FieldCode, field.TypeString, value)
		_node.Code = value
	}
	if value, ok := _c.mutation.BonusAmount(); ok {
		_spec.SetField(promocode.FieldBonusAmount, field.TypeFloat64, value)
		_node.BonusAmount = value
	}
	if value, ok := _c.mutation.MaxUses(); ok {
		_spec.SetField(promocode.FieldMaxUses, field.TypeInt, value)
		_node.MaxUses = value
	}
	if value, ok := _c.mutation.UsedCount(); ok {
		_spec.SetField(promocode.FieldUsedCount, field.TypeInt, value)
		_node.UsedCount = value
	}
	if value, ok := _c.mutation.Status(); ok {
		_spec.SetField(promocode.FieldStatus, field.TypeString, value)
		_node.Status = value
	}
	if value, ok := _c.mutation.ExpiresAt(); ok {
		_spec.SetField(promocode.FieldExpiresAt, field.TypeTime, value)
		_node.ExpiresAt = &value
	}
	if value, ok := _c.mutation.Notes(); ok {
		_spec.SetField(promocode.FieldNotes, field.TypeString, value)
		_node.Notes = &value
	}
	if value, ok := _c.mutation.CreatedAt(); ok {
		_spec.SetField(promocode.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := _c.mutation.UpdatedAt(); ok {
		_spec.SetField(promocode.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if nodes := _c.mutation.UsageRecordsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   promocode.UsageRecordsTable,
			Columns: []string{promocode.UsageRecordsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(promocodeusage.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_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.PromoCode.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.PromoCodeUpsert) {
//			SetCode(v+v).
//		}).
//		Exec(ctx)
func (_c *PromoCodeCreate) OnConflict(opts ...sql.ConflictOption) *PromoCodeUpsertOne {
	_c.conflict = opts
	return &PromoCodeUpsertOne{
		create: _c,
	}
}

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

type (
	// PromoCodeUpsertOne is the builder for "upsert"-ing
	//  one PromoCode node.
	PromoCodeUpsertOne struct {
		create *PromoCodeCreate
	}

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

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

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

// SetBonusAmount sets the "bonus_amount" field.
func (u *PromoCodeUpsert) SetBonusAmount(v float64) *PromoCodeUpsert {
	u.Set(promocode.FieldBonusAmount, v)
	return u
}

// UpdateBonusAmount sets the "bonus_amount" field to the value that was provided on create.
func (u *PromoCodeUpsert) UpdateBonusAmount() *PromoCodeUpsert {
	u.SetExcluded(promocode.FieldBonusAmount)
	return u
}

// AddBonusAmount adds v to the "bonus_amount" field.
func (u *PromoCodeUpsert) AddBonusAmount(v float64) *PromoCodeUpsert {
	u.Add(promocode.FieldBonusAmount, v)
	return u
}

// SetMaxUses sets the "max_uses" field.
func (u *PromoCodeUpsert) SetMaxUses(v int) *PromoCodeUpsert {
	u.Set(promocode.FieldMaxUses, v)
	return u
}

// UpdateMaxUses sets the "max_uses" field to the value that was provided on create.
func (u *PromoCodeUpsert) UpdateMaxUses() *PromoCodeUpsert {
	u.SetExcluded(promocode.FieldMaxUses)
	return u
}

// AddMaxUses adds v to the "max_uses" field.
func (u *PromoCodeUpsert) AddMaxUses(v int) *PromoCodeUpsert {
	u.Add(promocode.FieldMaxUses, v)
	return u
}

// SetUsedCount sets the "used_count" field.
func (u *PromoCodeUpsert) SetUsedCount(v int) *PromoCodeUpsert {
	u.Set(promocode.FieldUsedCount, v)
	return u
}

// UpdateUsedCount sets the "used_count" field to the value that was provided on create.
func (u *PromoCodeUpsert) UpdateUsedCount() *PromoCodeUpsert {
	u.SetExcluded(promocode.FieldUsedCount)
	return u
}

// AddUsedCount adds v to the "used_count" field.
func (u *PromoCodeUpsert) AddUsedCount(v int) *PromoCodeUpsert {
	u.Add(promocode.FieldUsedCount, v)
	return u
}

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

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

// SetExpiresAt sets the "expires_at" field.
func (u *PromoCodeUpsert) SetExpiresAt(v time.Time) *PromoCodeUpsert {
	u.Set(promocode.FieldExpiresAt, v)
	return u
}

// UpdateExpiresAt sets the "expires_at" field to the value that was provided on create.
func (u *PromoCodeUpsert) UpdateExpiresAt() *PromoCodeUpsert {
	u.SetExcluded(promocode.FieldExpiresAt)
	return u
}

// ClearExpiresAt clears the value of the "expires_at" field.
func (u *PromoCodeUpsert) ClearExpiresAt() *PromoCodeUpsert {
	u.SetNull(promocode.FieldExpiresAt)
	return u
}

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

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

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

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

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

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

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

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

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

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

// SetBonusAmount sets the "bonus_amount" field.
func (u *PromoCodeUpsertOne) SetBonusAmount(v float64) *PromoCodeUpsertOne {
	return u.Update(func(s *PromoCodeUpsert) {
		s.SetBonusAmount(v)
	})
}

// AddBonusAmount adds v to the "bonus_amount" field.
func (u *PromoCodeUpsertOne) AddBonusAmount(v float64) *PromoCodeUpsertOne {
	return u.Update(func(s *PromoCodeUpsert) {
		s.AddBonusAmount(v)
	})
}

// UpdateBonusAmount sets the "bonus_amount" field to the value that was provided on create.
func (u *PromoCodeUpsertOne) UpdateBonusAmount() *PromoCodeUpsertOne {
	return u.Update(func(s *PromoCodeUpsert) {
		s.UpdateBonusAmount()
	})
}

// SetMaxUses sets the "max_uses" field.
func (u *PromoCodeUpsertOne) SetMaxUses(v int) *PromoCodeUpsertOne {
	return u.Update(func(s *PromoCodeUpsert) {
		s.SetMaxUses(v)
	})
}

// AddMaxUses adds v to the "max_uses" field.
func (u *PromoCodeUpsertOne) AddMaxUses(v int) *PromoCodeUpsertOne {
	return u.Update(func(s *PromoCodeUpsert) {
		s.AddMaxUses(v)
	})
}

// UpdateMaxUses sets the "max_uses" field to the value that was provided on create.
func (u *PromoCodeUpsertOne) UpdateMaxUses() *PromoCodeUpsertOne {
	return u.Update(func(s *PromoCodeUpsert) {
		s.UpdateMaxUses()
	})
}

// SetUsedCount sets the "used_count" field.
func (u *PromoCodeUpsertOne) SetUsedCount(v int) *PromoCodeUpsertOne {
	return u.Update(func(s *PromoCodeUpsert) {
		s.SetUsedCount(v)
	})
}

// AddUsedCount adds v to the "used_count" field.
func (u *PromoCodeUpsertOne) AddUsedCount(v int) *PromoCodeUpsertOne {
	return u.Update(func(s *PromoCodeUpsert) {
		s.AddUsedCount(v)
	})
}

// UpdateUsedCount sets the "used_count" field to the value that was provided on create.
func (u *PromoCodeUpsertOne) UpdateUsedCount() *PromoCodeUpsertOne {
	return u.Update(func(s *PromoCodeUpsert) {
		s.UpdateUsedCount()
	})
}

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

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

// SetExpiresAt sets the "expires_at" field.
func (u *PromoCodeUpsertOne) SetExpiresAt(v time.Time) *PromoCodeUpsertOne {
	return u.Update(func(s *PromoCodeUpsert) {
		s.SetExpiresAt(v)
	})
}

// UpdateExpiresAt sets the "expires_at" field to the value that was provided on create.
func (u *PromoCodeUpsertOne) UpdateExpiresAt() *PromoCodeUpsertOne {
	return u.Update(func(s *PromoCodeUpsert) {
		s.UpdateExpiresAt()
	})
}

// ClearExpiresAt clears the value of the "expires_at" field.
func (u *PromoCodeUpsertOne) ClearExpiresAt() *PromoCodeUpsertOne {
	return u.Update(func(s *PromoCodeUpsert) {
		s.ClearExpiresAt()
	})
}

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

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

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

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

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

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

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

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

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

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

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

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

// PromoCodeUpsertBulk is the builder for "upsert"-ing
// a bulk of PromoCode nodes.
type PromoCodeUpsertBulk struct {
	create *PromoCodeCreateBulk
}

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

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

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

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

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

// SetBonusAmount sets the "bonus_amount" field.
func (u *PromoCodeUpsertBulk) SetBonusAmount(v float64) *PromoCodeUpsertBulk {
	return u.Update(func(s *PromoCodeUpsert) {
		s.SetBonusAmount(v)
	})
}

// AddBonusAmount adds v to the "bonus_amount" field.
func (u *PromoCodeUpsertBulk) AddBonusAmount(v float64) *PromoCodeUpsertBulk {
	return u.Update(func(s *PromoCodeUpsert) {
		s.AddBonusAmount(v)
	})
}

// UpdateBonusAmount sets the "bonus_amount" field to the value that was provided on create.
func (u *PromoCodeUpsertBulk) UpdateBonusAmount() *PromoCodeUpsertBulk {
	return u.Update(func(s *PromoCodeUpsert) {
		s.UpdateBonusAmount()
	})
}

// SetMaxUses sets the "max_uses" field.
func (u *PromoCodeUpsertBulk) SetMaxUses(v int) *PromoCodeUpsertBulk {
	return u.Update(func(s *PromoCodeUpsert) {
		s.SetMaxUses(v)
	})
}

// AddMaxUses adds v to the "max_uses" field.
func (u *PromoCodeUpsertBulk) AddMaxUses(v int) *PromoCodeUpsertBulk {
	return u.Update(func(s *PromoCodeUpsert) {
		s.AddMaxUses(v)
	})
}

// UpdateMaxUses sets the "max_uses" field to the value that was provided on create.
func (u *PromoCodeUpsertBulk) UpdateMaxUses() *PromoCodeUpsertBulk {
	return u.Update(func(s *PromoCodeUpsert) {
		s.UpdateMaxUses()
	})
}

// SetUsedCount sets the "used_count" field.
func (u *PromoCodeUpsertBulk) SetUsedCount(v int) *PromoCodeUpsertBulk {
	return u.Update(func(s *PromoCodeUpsert) {
		s.SetUsedCount(v)
	})
}

// AddUsedCount adds v to the "used_count" field.
func (u *PromoCodeUpsertBulk) AddUsedCount(v int) *PromoCodeUpsertBulk {
	return u.Update(func(s *PromoCodeUpsert) {
		s.AddUsedCount(v)
	})
}

// UpdateUsedCount sets the "used_count" field to the value that was provided on create.
func (u *PromoCodeUpsertBulk) UpdateUsedCount() *PromoCodeUpsertBulk {
	return u.Update(func(s *PromoCodeUpsert) {
		s.UpdateUsedCount()
	})
}

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

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

// SetExpiresAt sets the "expires_at" field.
func (u *PromoCodeUpsertBulk) SetExpiresAt(v time.Time) *PromoCodeUpsertBulk {
	return u.Update(func(s *PromoCodeUpsert) {
		s.SetExpiresAt(v)
	})
}

// UpdateExpiresAt sets the "expires_at" field to the value that was provided on create.
func (u *PromoCodeUpsertBulk) UpdateExpiresAt() *PromoCodeUpsertBulk {
	return u.Update(func(s *PromoCodeUpsert) {
		s.UpdateExpiresAt()
	})
}

// ClearExpiresAt clears the value of the "expires_at" field.
func (u *PromoCodeUpsertBulk) ClearExpiresAt() *PromoCodeUpsertBulk {
	return u.Update(func(s *PromoCodeUpsert) {
		s.ClearExpiresAt()
	})
}

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

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

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

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

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

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

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