// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"
	"wechat-api/ent/chatsession"

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

// ChatSessionCreate is the builder for creating a ChatSession entity.
type ChatSessionCreate struct {
	config
	mutation *ChatSessionMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

// SetCreatedAt sets the "created_at" field.
func (csc *ChatSessionCreate) SetCreatedAt(t time.Time) *ChatSessionCreate {
	csc.mutation.SetCreatedAt(t)
	return csc
}

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

// SetUpdatedAt sets the "updated_at" field.
func (csc *ChatSessionCreate) SetUpdatedAt(t time.Time) *ChatSessionCreate {
	csc.mutation.SetUpdatedAt(t)
	return csc
}

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

// SetDeletedAt sets the "deleted_at" field.
func (csc *ChatSessionCreate) SetDeletedAt(t time.Time) *ChatSessionCreate {
	csc.mutation.SetDeletedAt(t)
	return csc
}

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

// SetName sets the "name" field.
func (csc *ChatSessionCreate) SetName(s string) *ChatSessionCreate {
	csc.mutation.SetName(s)
	return csc
}

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

// SetUserID sets the "user_id" field.
func (csc *ChatSessionCreate) SetUserID(u uint64) *ChatSessionCreate {
	csc.mutation.SetUserID(u)
	return csc
}

// SetNillableUserID sets the "user_id" field if the given value is not nil.
func (csc *ChatSessionCreate) SetNillableUserID(u *uint64) *ChatSessionCreate {
	if u != nil {
		csc.SetUserID(*u)
	}
	return csc
}

// SetBotID sets the "bot_id" field.
func (csc *ChatSessionCreate) SetBotID(u uint64) *ChatSessionCreate {
	csc.mutation.SetBotID(u)
	return csc
}

// SetNillableBotID sets the "bot_id" field if the given value is not nil.
func (csc *ChatSessionCreate) SetNillableBotID(u *uint64) *ChatSessionCreate {
	if u != nil {
		csc.SetBotID(*u)
	}
	return csc
}

// SetBotType sets the "bot_type" field.
func (csc *ChatSessionCreate) SetBotType(u uint8) *ChatSessionCreate {
	csc.mutation.SetBotType(u)
	return csc
}

// SetNillableBotType sets the "bot_type" field if the given value is not nil.
func (csc *ChatSessionCreate) SetNillableBotType(u *uint8) *ChatSessionCreate {
	if u != nil {
		csc.SetBotType(*u)
	}
	return csc
}

// SetID sets the "id" field.
func (csc *ChatSessionCreate) SetID(u uint64) *ChatSessionCreate {
	csc.mutation.SetID(u)
	return csc
}

// Mutation returns the ChatSessionMutation object of the builder.
func (csc *ChatSessionCreate) Mutation() *ChatSessionMutation {
	return csc.mutation
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (csc *ChatSessionCreate) defaults() error {
	if _, ok := csc.mutation.CreatedAt(); !ok {
		if chatsession.DefaultCreatedAt == nil {
			return fmt.Errorf("ent: uninitialized chatsession.DefaultCreatedAt (forgotten import ent/runtime?)")
		}
		v := chatsession.DefaultCreatedAt()
		csc.mutation.SetCreatedAt(v)
	}
	if _, ok := csc.mutation.UpdatedAt(); !ok {
		if chatsession.DefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized chatsession.DefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := chatsession.DefaultUpdatedAt()
		csc.mutation.SetUpdatedAt(v)
	}
	if _, ok := csc.mutation.Name(); !ok {
		v := chatsession.DefaultName
		csc.mutation.SetName(v)
	}
	if _, ok := csc.mutation.UserID(); !ok {
		v := chatsession.DefaultUserID
		csc.mutation.SetUserID(v)
	}
	if _, ok := csc.mutation.BotID(); !ok {
		v := chatsession.DefaultBotID
		csc.mutation.SetBotID(v)
	}
	if _, ok := csc.mutation.BotType(); !ok {
		v := chatsession.DefaultBotType
		csc.mutation.SetBotType(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (csc *ChatSessionCreate) check() error {
	if _, ok := csc.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "ChatSession.created_at"`)}
	}
	if _, ok := csc.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "ChatSession.updated_at"`)}
	}
	if _, ok := csc.mutation.Name(); !ok {
		return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "ChatSession.name"`)}
	}
	if _, ok := csc.mutation.UserID(); !ok {
		return &ValidationError{Name: "user_id", err: errors.New(`ent: missing required field "ChatSession.user_id"`)}
	}
	if _, ok := csc.mutation.BotID(); !ok {
		return &ValidationError{Name: "bot_id", err: errors.New(`ent: missing required field "ChatSession.bot_id"`)}
	}
	if _, ok := csc.mutation.BotType(); !ok {
		return &ValidationError{Name: "bot_type", err: errors.New(`ent: missing required field "ChatSession.bot_type"`)}
	}
	return nil
}

func (csc *ChatSessionCreate) sqlSave(ctx context.Context) (*ChatSession, error) {
	if err := csc.check(); err != nil {
		return nil, err
	}
	_node, _spec := csc.createSpec()
	if err := sqlgraph.CreateNode(ctx, csc.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 = uint64(id)
	}
	csc.mutation.id = &_node.ID
	csc.mutation.done = true
	return _node, nil
}

func (csc *ChatSessionCreate) createSpec() (*ChatSession, *sqlgraph.CreateSpec) {
	var (
		_node = &ChatSession{config: csc.config}
		_spec = sqlgraph.NewCreateSpec(chatsession.Table, sqlgraph.NewFieldSpec(chatsession.FieldID, field.TypeUint64))
	)
	_spec.OnConflict = csc.conflict
	if id, ok := csc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := csc.mutation.CreatedAt(); ok {
		_spec.SetField(chatsession.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := csc.mutation.UpdatedAt(); ok {
		_spec.SetField(chatsession.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := csc.mutation.DeletedAt(); ok {
		_spec.SetField(chatsession.FieldDeletedAt, field.TypeTime, value)
		_node.DeletedAt = value
	}
	if value, ok := csc.mutation.Name(); ok {
		_spec.SetField(chatsession.FieldName, field.TypeString, value)
		_node.Name = value
	}
	if value, ok := csc.mutation.UserID(); ok {
		_spec.SetField(chatsession.FieldUserID, field.TypeUint64, value)
		_node.UserID = value
	}
	if value, ok := csc.mutation.BotID(); ok {
		_spec.SetField(chatsession.FieldBotID, field.TypeUint64, value)
		_node.BotID = value
	}
	if value, ok := csc.mutation.BotType(); ok {
		_spec.SetField(chatsession.FieldBotType, field.TypeUint8, value)
		_node.BotType = value
	}
	return _node, _spec
}

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.ChatSession.Create().
//		SetCreatedAt(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.ChatSessionUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (csc *ChatSessionCreate) OnConflict(opts ...sql.ConflictOption) *ChatSessionUpsertOne {
	csc.conflict = opts
	return &ChatSessionUpsertOne{
		create: csc,
	}
}

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

type (
	// ChatSessionUpsertOne is the builder for "upsert"-ing
	//  one ChatSession node.
	ChatSessionUpsertOne struct {
		create *ChatSessionCreate
	}

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

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

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

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

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

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

// SetName sets the "name" field.
func (u *ChatSessionUpsert) SetName(v string) *ChatSessionUpsert {
	u.Set(chatsession.FieldName, v)
	return u
}

// UpdateName sets the "name" field to the value that was provided on create.
func (u *ChatSessionUpsert) UpdateName() *ChatSessionUpsert {
	u.SetExcluded(chatsession.FieldName)
	return u
}

// SetUserID sets the "user_id" field.
func (u *ChatSessionUpsert) SetUserID(v uint64) *ChatSessionUpsert {
	u.Set(chatsession.FieldUserID, v)
	return u
}

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

// AddUserID adds v to the "user_id" field.
func (u *ChatSessionUpsert) AddUserID(v uint64) *ChatSessionUpsert {
	u.Add(chatsession.FieldUserID, v)
	return u
}

// SetBotID sets the "bot_id" field.
func (u *ChatSessionUpsert) SetBotID(v uint64) *ChatSessionUpsert {
	u.Set(chatsession.FieldBotID, v)
	return u
}

// UpdateBotID sets the "bot_id" field to the value that was provided on create.
func (u *ChatSessionUpsert) UpdateBotID() *ChatSessionUpsert {
	u.SetExcluded(chatsession.FieldBotID)
	return u
}

// AddBotID adds v to the "bot_id" field.
func (u *ChatSessionUpsert) AddBotID(v uint64) *ChatSessionUpsert {
	u.Add(chatsession.FieldBotID, v)
	return u
}

// SetBotType sets the "bot_type" field.
func (u *ChatSessionUpsert) SetBotType(v uint8) *ChatSessionUpsert {
	u.Set(chatsession.FieldBotType, v)
	return u
}

// UpdateBotType sets the "bot_type" field to the value that was provided on create.
func (u *ChatSessionUpsert) UpdateBotType() *ChatSessionUpsert {
	u.SetExcluded(chatsession.FieldBotType)
	return u
}

// AddBotType adds v to the "bot_type" field.
func (u *ChatSessionUpsert) AddBotType(v uint8) *ChatSessionUpsert {
	u.Add(chatsession.FieldBotType, v)
	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.ChatSession.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(chatsession.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *ChatSessionUpsertOne) UpdateNewValues() *ChatSessionUpsertOne {
	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(chatsession.FieldID)
		}
		if _, exists := u.create.mutation.CreatedAt(); exists {
			s.SetIgnore(chatsession.FieldCreatedAt)
		}
	}))
	return u
}

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

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

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

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

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

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

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

// SetName sets the "name" field.
func (u *ChatSessionUpsertOne) SetName(v string) *ChatSessionUpsertOne {
	return u.Update(func(s *ChatSessionUpsert) {
		s.SetName(v)
	})
}

// UpdateName sets the "name" field to the value that was provided on create.
func (u *ChatSessionUpsertOne) UpdateName() *ChatSessionUpsertOne {
	return u.Update(func(s *ChatSessionUpsert) {
		s.UpdateName()
	})
}

// SetUserID sets the "user_id" field.
func (u *ChatSessionUpsertOne) SetUserID(v uint64) *ChatSessionUpsertOne {
	return u.Update(func(s *ChatSessionUpsert) {
		s.SetUserID(v)
	})
}

// AddUserID adds v to the "user_id" field.
func (u *ChatSessionUpsertOne) AddUserID(v uint64) *ChatSessionUpsertOne {
	return u.Update(func(s *ChatSessionUpsert) {
		s.AddUserID(v)
	})
}

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

// SetBotID sets the "bot_id" field.
func (u *ChatSessionUpsertOne) SetBotID(v uint64) *ChatSessionUpsertOne {
	return u.Update(func(s *ChatSessionUpsert) {
		s.SetBotID(v)
	})
}

// AddBotID adds v to the "bot_id" field.
func (u *ChatSessionUpsertOne) AddBotID(v uint64) *ChatSessionUpsertOne {
	return u.Update(func(s *ChatSessionUpsert) {
		s.AddBotID(v)
	})
}

// UpdateBotID sets the "bot_id" field to the value that was provided on create.
func (u *ChatSessionUpsertOne) UpdateBotID() *ChatSessionUpsertOne {
	return u.Update(func(s *ChatSessionUpsert) {
		s.UpdateBotID()
	})
}

// SetBotType sets the "bot_type" field.
func (u *ChatSessionUpsertOne) SetBotType(v uint8) *ChatSessionUpsertOne {
	return u.Update(func(s *ChatSessionUpsert) {
		s.SetBotType(v)
	})
}

// AddBotType adds v to the "bot_type" field.
func (u *ChatSessionUpsertOne) AddBotType(v uint8) *ChatSessionUpsertOne {
	return u.Update(func(s *ChatSessionUpsert) {
		s.AddBotType(v)
	})
}

// UpdateBotType sets the "bot_type" field to the value that was provided on create.
func (u *ChatSessionUpsertOne) UpdateBotType() *ChatSessionUpsertOne {
	return u.Update(func(s *ChatSessionUpsert) {
		s.UpdateBotType()
	})
}

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

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

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

// Save creates the ChatSession entities in the database.
func (cscb *ChatSessionCreateBulk) Save(ctx context.Context) ([]*ChatSession, error) {
	if cscb.err != nil {
		return nil, cscb.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(cscb.builders))
	nodes := make([]*ChatSession, len(cscb.builders))
	mutators := make([]Mutator, len(cscb.builders))
	for i := range cscb.builders {
		func(i int, root context.Context) {
			builder := cscb.builders[i]
			builder.defaults()
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*ChatSessionMutation)
				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, cscb.builders[i+1].mutation)
				} else {
					spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
					spec.OnConflict = cscb.conflict
					// Invoke the actual operation on the latest mutation in the chain.
					if err = sqlgraph.BatchCreate(ctx, cscb.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 = uint64(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, cscb.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

// SaveX is like Save, but panics if an error occurs.
func (cscb *ChatSessionCreateBulk) SaveX(ctx context.Context) []*ChatSession {
	v, err := cscb.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

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

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

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.ChatSession.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.ChatSessionUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (cscb *ChatSessionCreateBulk) OnConflict(opts ...sql.ConflictOption) *ChatSessionUpsertBulk {
	cscb.conflict = opts
	return &ChatSessionUpsertBulk{
		create: cscb,
	}
}

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

// ChatSessionUpsertBulk is the builder for "upsert"-ing
// a bulk of ChatSession nodes.
type ChatSessionUpsertBulk struct {
	create *ChatSessionCreateBulk
}

// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
//	client.ChatSession.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(chatsession.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *ChatSessionUpsertBulk) UpdateNewValues() *ChatSessionUpsertBulk {
	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(chatsession.FieldID)
			}
			if _, exists := b.mutation.CreatedAt(); exists {
				s.SetIgnore(chatsession.FieldCreatedAt)
			}
		}
	}))
	return u
}

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

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

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

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

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

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

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

// SetName sets the "name" field.
func (u *ChatSessionUpsertBulk) SetName(v string) *ChatSessionUpsertBulk {
	return u.Update(func(s *ChatSessionUpsert) {
		s.SetName(v)
	})
}

// UpdateName sets the "name" field to the value that was provided on create.
func (u *ChatSessionUpsertBulk) UpdateName() *ChatSessionUpsertBulk {
	return u.Update(func(s *ChatSessionUpsert) {
		s.UpdateName()
	})
}

// SetUserID sets the "user_id" field.
func (u *ChatSessionUpsertBulk) SetUserID(v uint64) *ChatSessionUpsertBulk {
	return u.Update(func(s *ChatSessionUpsert) {
		s.SetUserID(v)
	})
}

// AddUserID adds v to the "user_id" field.
func (u *ChatSessionUpsertBulk) AddUserID(v uint64) *ChatSessionUpsertBulk {
	return u.Update(func(s *ChatSessionUpsert) {
		s.AddUserID(v)
	})
}

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

// SetBotID sets the "bot_id" field.
func (u *ChatSessionUpsertBulk) SetBotID(v uint64) *ChatSessionUpsertBulk {
	return u.Update(func(s *ChatSessionUpsert) {
		s.SetBotID(v)
	})
}

// AddBotID adds v to the "bot_id" field.
func (u *ChatSessionUpsertBulk) AddBotID(v uint64) *ChatSessionUpsertBulk {
	return u.Update(func(s *ChatSessionUpsert) {
		s.AddBotID(v)
	})
}

// UpdateBotID sets the "bot_id" field to the value that was provided on create.
func (u *ChatSessionUpsertBulk) UpdateBotID() *ChatSessionUpsertBulk {
	return u.Update(func(s *ChatSessionUpsert) {
		s.UpdateBotID()
	})
}

// SetBotType sets the "bot_type" field.
func (u *ChatSessionUpsertBulk) SetBotType(v uint8) *ChatSessionUpsertBulk {
	return u.Update(func(s *ChatSessionUpsert) {
		s.SetBotType(v)
	})
}

// AddBotType adds v to the "bot_type" field.
func (u *ChatSessionUpsertBulk) AddBotType(v uint8) *ChatSessionUpsertBulk {
	return u.Update(func(s *ChatSessionUpsert) {
		s.AddBotType(v)
	})
}

// UpdateBotType sets the "bot_type" field to the value that was provided on create.
func (u *ChatSessionUpsertBulk) UpdateBotType() *ChatSessionUpsertBulk {
	return u.Update(func(s *ChatSessionUpsert) {
		s.UpdateBotType()
	})
}

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

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